DantSu / ESCPOS-ThermalPrinter-Android

Useful library to help Android developpers to print with (Bluetooth, TCP, USB) ESC/POS thermal printer.
MIT License
1.18k stars 359 forks source link

Blank Page showing when print bitmap while base64 string showing original image on online editor #171

Closed Aashir1111 closed 3 years ago

Aashir1111 commented 3 years ago
            val printer =  AsyncEscPosPrinter(printerConnection, 203, 70f, 48)
            try {
                val receiptBitmap: Bitmap = receipt
                val width = receiptBitmap.width
                val height = receiptBitmap.height

                val textToPrint = StringBuilder()
                var y = 0
                while (y < height) {
                    val bitmap = Bitmap.createBitmap(
                        receiptBitmap,
                        0,
                        y,
                        width,
                        if (y + 256 >= height) height - y else 256
                    )
                    textToPrint.append(
                        "[C]<img>" + PrinterTextParserImg.bitmapToHexadecimalString(
                            printer,
                            bitmap
                        ) + "</img>\n"
                    )
                    y += 256
                }
                textToPrint.append("[C]Printed!!!\n")

                databas?.child("StringAppend")?.setValue(textToPrint.toString())

                return printer.setTextToPrint(textToPrint.toString())

            } catch (e: Exception) {
}

generate HexString is: "[C]1d7630004700790000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...........0000000000000000\n[C]Printed!!!\n"

otashjumaev commented 3 years ago

@DantSu i have the same problem, can you check it??

DantSu commented 3 years ago

I can't reproduce your problem, can't help you, you have to find the solution by yourself

DantSu commented 3 years ago

Write here the solution when you find it.

otashjumaev commented 3 years ago

I found the solution the problem was my bitmaps width was too large and I resized it

val width = (printerDPI / 25.4 * printerWidth) // printeraWidth 58mm , 80mm ...
val ratio = bitmap.width * 1.0 / width
val height = bitmap.height / ratio
val finalBitmap = Bitmap.createScaledBitmap(bitmap, width.toInt(), height.toInt(), false)