Thermal-Printer-in-Android icon indicating copy to clipboard operation
Thermal-Printer-in-Android copied to clipboard

Max Width and Height

Open Tabicho opened this issue 8 years ago • 12 comments

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;

Tabicho avatar Aug 02 '17 04:08 Tabicho

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

aldyrpl avatar Jan 11 '18 06:01 aldyrpl

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.

Tabicho avatar Jan 11 '18 13:01 Tabicho

please can you share the code? i am very stuck

aldyrpl avatar Jan 11 '18 13:01 aldyrpl

Now I dont have the code, when I found it I will post it.

Tabicho avatar Jan 11 '18 17:01 Tabicho

hi , did any one solved this issue , as i am stuck into it too

adelemam avatar Apr 22 '18 17:04 adelemam

i found the answer wait i will check my project

aldyrpl avatar Apr 23 '18 01:04 aldyrpl

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 .

aldyrpl avatar Apr 23 '18 02:04 aldyrpl

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()
    }
}`

chukimmuoi avatar Jun 26 '18 10:06 chukimmuoi

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

bayuwijdev avatar Oct 12 '18 03:10 bayuwijdev

hi.. @chukimmuoi How Can we Define the Right Rows And Cols ?

jaydeep43 avatar Sep 26 '19 08:09 jaydeep43

@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()
            }
        }
    }```

chukimmuoi avatar Sep 29 '19 14:09 chukimmuoi

this documentation will be useful image

ghost avatar Jun 20 '20 01:06 ghost