Introduction
First, I already have a receipt printer connected to Android device. As it is connected to the serial port already, I don't need to think about the connection. All I have to is only send the byte array to the printer. But this printer cannot print the Korean font since there is no Korean font in it. Furthermore, there is no time, so I needed a sample source for printing image data to receipt printer. If I could find that source, I thought I would be able to print out the Korean string to the printer.
Background
I found a source that I almost wanted by following the link below:
As you would know, if you click, you can analyze the source. And also, you have to understand that sample source code at first before learning how to work on my source below. Actually, you don't have to know all sample source. What I used is the code in functions in 'Utils
' class because I will print all the text as images. You should start off from the function, 'decodeBitmap
'.
You will know that this 'printPhoto
' function does not print images above 250 pixels. So, I am just going to call this function per string per line.
Using the Code
The following code is the point where I start to print.
Val list: Mutablelist<bytearray> = Arraylist()
Val Cmd_init = Bytearrayof(27, 64)
list.add(CMD_INIT)
list.add(byteArrayOf(0x1B, 0x21, 0x03))
Var Command = Getimagedata
(logger, "Abcdefghijkl\n안녕하세요\nmnopqrstuvwxyg", Paint.Align.CENTER)
If(command != null) list.add((command))
list.add("------------------------------------------\n".toByteArray())
Val Now = System.currentTimeMillis()
Val Date = Date(now)
Val Sdfnow = Simpledateformat("시간: Yyyy/mm/dd Hh:mm:ss a")
Val Formatdate = sdfNow.format(date)
Command = Getimagedata(logger, Formatdate)
If(command != null) list.add((command))
Val Cmd_cut = Bytearrayof(0x1d, 0x56, 0)
list.add(CMD_CUT)
Var prt: Printer? = Null
Try {
Prt = Printer.newInstance()
if (!prt!!.ready()) {
logger.error("Printer Is Not Ready Before printing.")
Return
}
For (I in list.indices) {
prt.outputStream.write(list.get(i))
}
if (!prt.ready()) {
logger.error("Printer Is Not Ready After printing.")
Return
}
} Catch (e: Throwable) {
logger.error("An Exception Issued in Printing. $e")
} Finally {
prt?.close()
}
The following function is 'getImageData
'. This is the main code.
fun getImageData(logger:Logger, stringData:String,
align:Paint.Align = Paint.Align.LEFT): ByteArray? {
var command:ByteArray? = null
try {
val pnt: Paint = Paint()
pnt.setAntiAlias(true)
pnt.setColor(Color.BLACK)
pnt.setTextSize(23f)
var xWidth = 385
var xHeight = 30
var xPos = 0f
var yPos = 27f
var finalStringDatas:MutableList<PrintData> = ArrayList()
var tmpSplitList:List<String> = stringData.split('\n')
for(i in 0..tmpSplitList.count()-1)
{
val tmpString = tmpSplitList[i]
var fWidthOfString = pnt.measureText(tmpString)
if (fWidthOfString > xWidth)
{
var lastString = tmpString
while(!lastString.isEmpty()) {
var tmpSubString = ""
while(fWidthOfString > xWidth)
{
if (tmpSubString.isEmpty())
tmpSubString = lastString.substring(0, lastString.length-1)
else
tmpSubString = tmpSubString.substring(0, tmpSubString.length-1)
fWidthOfString = pnt.measureText(tmpSubString)
}
if (tmpSubString.isEmpty())
{
if(align == Paint.Align.CENTER)
{
if(fWidthOfString < xWidth) {
xPos = ((xWidth - fWidthOfString) / 2)
}
}
else if(align == Paint.Align.RIGHT)
{
if(fWidthOfString < xWidth) {
xPos = xWidth - fWidthOfString
}
}
finalStringDatas.add(PrintData(xPos, yPos, lastString))
lastString = ""
}
else
{
finalStringDatas.add(PrintData(0f, yPos, tmpSubString))
yPos += 27
xHeight += 30
lastString = lastString.replaceFirst(tmpSubString, "")
fWidthOfString = pnt.measureText(lastString)
}
}
}
else
{
if(align == Paint.Align.CENTER)
{
if(fWidthOfString < xWidth) {
xPos = ((xWidth - fWidthOfString) / 2)
}
}
else if(align == Paint.Align.RIGHT)
{
if(fWidthOfString < xWidth) {
xPos = xWidth - fWidthOfString
}
}
finalStringDatas.add(PrintData(xPos, yPos, tmpString))
}
if (i != tmpSplitList.count()-1)
{
yPos += 27
xHeight += 30
}
}
val bm:Bitmap = Bitmap.createBitmap(xWidth, xHeight, Bitmap.Config.ARGB_8888)
val canvas: Canvas = Canvas(bm)
canvas.drawColor(Color.WHITE)
for(tmpItem in finalStringDatas)
canvas.drawText(tmpItem.strData, tmpItem.xPos, tmpItem.yPos, pnt)
command = Utils.decodeBitmap(bm)
}
catch (e: Exception) {
logger.error(e.printStackTrace())
}
return command
}
The following class is 'PrintData
'.
class PrintData (var xPos:Float, var yPos:Float, var strData:String)
History
- 11/14/2019: First update
- 11/15/2019: '
PrintData
' class is added. Style changed.