Thermal-Printer-in-Android
Thermal-Printer-in-Android copied to clipboard
Max Width and Height
Hello @imrankst1221 , how can I modify the max width and height for image printing, in Utils.decodeBitmap?
if (widthHexString.length() > 2) { Log.e("decodeBitmap error", " width is too large"); return null;
if (heightHexString.length() > 2) { Log.e("decodeBitmap error", " height is too large"); return null;
hey sir do you found the answer? because i am stuck in this code for 1 week if you found the answer i hope you share to me thanks
Hello, yes What I have to did was divide the image in two images of height no larguer than de allowed then iterate the print process by each part of the image.
please can you share the code? i am very stuck
Now I dont have the code, when I found it I will post it.
hi , did any one solved this issue , as i am stuck into it too
i found the answer wait i will check my project
i found the answers wait i will check my project
Pada tanggal 23 Apr 2018 12.29 AM, "adelemam" [email protected] menulis:
hi , did any one solved this issue , as i am stuck into it too
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/imrankst1221/Thermal-Printer-in-Android/issues/5#issuecomment-383398077, or mute the thread https://github.com/notifications/unsubscribe-auth/AhsGG_kVoX2vDghciUsTn1-lMCeqqSyzks5trL3ygaJpZM4Oqmbs .
You can try the code later: `private fun splitImage(bitmap: Bitmap, rows: Int, cols: Int) { try { // For height and width of the small image chunks. val chunkHeight = bitmap.height / rows val chunkWidth = bitmap.width / cols
// Getting the scaled bitmap of the source image.
val scaledBitmap = Bitmap.createScaledBitmap(bitmap, bitmap.width, bitmap.height, true)
// xCoord and yCoord are the pixel positions of the image chunks.
var yCoord = 0
for (x in 0 until rows) {
var xCoord = 0
for (y in 0 until cols) {
val bmp = Bitmap.createBitmap(scaledBitmap, xCoord, yCoord, chunkWidth, chunkHeight)
if (bmp != null) {
val command = EscPosUtils.decodeBitmap(bmp)
outputStream.write(EscPosCommands.ESC_ALIGN_CENTER)
printText(command, false)
} else {
Timber.e("Print Photo error - The file isn't exists")
}
xCoord += chunkWidth
}
yCoord += chunkHeight
}
} catch (e: Exception) {
e.printStackTrace()
}
}`
Hi @chukimmuoi how can i define the right rows and cols? And what is unit of measure for that? Can you help me to describe it? Please help
hi.. @chukimmuoi How Can we Define the Right Rows And Cols ?
@jaydeep43 @BayuWijayaPermanaPutra Sorry, cols = 1 and row = bmpScale.height / 32 + 1. This is full code, no need to care typePrint:
private fun printText(msg: ByteArray, isNewLine: Boolean = true) {
outputStream?.let {
try {
it.write(msg) // Print normal text.
} catch (e: IOException) {
e.printStackTrace()
}
}
}```
```// Print bill.
private fun printWebView(bmpScale: Bitmap, typePrint: Int) {
try {
val isPrintCook = typePrint == QueueDataPrint.VALUE_PRINT_COOK
val isOpenCashBox = typePrint == QueueDataPrint.VALUE_PRINT_CASHIER
fun innerHandler(rows: Int) {
splitImage(bmpScale, rows, 1) {
printText(it, false)
}
autoCut(isOpenCashBox)
}
if (bmpScale != null) {
val rows = bmpScale.height / 32 + 1
outputStream?.write(EscPosCommands.ESC_ALIGN_CENTER)
innerHandler(rows)
if (preferencesHelper.getPrintTwoForInvoice() && !isPrintCook)
innerHandler(rows)
} else {
Timber.e("Print Photo error - the file isn't exists")
}
} catch (e: Exception) {
e.printStackTrace()
Timber.e("PrintTools - the file isn't exists")
}
}```
```private fun splitImage(bitmap: Bitmap, rows: Int, cols: Int, doAction: (ByteArray) -> Unit) {
synchronized(this) {
try {
// For height and width of the small image chunks.
var chunkHeight = bitmap.height / rows + 1
val chunkWidth = bitmap.width / cols
// Getting the scaled bitmap of the source image.
val scaledBitmap = Bitmap.createScaledBitmap(bitmap, bitmap.width, bitmap.height, true)
// xCoord and yCoord are the pixel positions of the image chunks.
var yCoord = 0
for (x in 0 until rows) {
var xCoord = 0
for (y in 0 until cols) {
if (yCoord + chunkHeight > bitmap.height) {
chunkHeight = bitmap.height - yCoord
}
val bmp = Bitmap.createBitmap(scaledBitmap, xCoord, yCoord, chunkWidth, chunkHeight)
if (bmp != null) {
val command = EscPosUtils.decodeBitmap(bmp)
doAction(command)
} else {
Timber.e("Print Photo error - The file isn't exists")
}
xCoord += chunkWidth
}
yCoord += chunkHeight
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}```
this documentation will be useful
