Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Max Width and Height #5

Open
Tabicho opened this issue Aug 2, 2017 · 12 comments
Open

Max Width and Height #5

Tabicho opened this issue Aug 2, 2017 · 12 comments
Labels

Comments

@Tabicho
Copy link

Tabicho commented Aug 2, 2017

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;

@aldyrpl
Copy link

aldyrpl commented Jan 11, 2018

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

@Tabicho
Copy link
Author

Tabicho commented Jan 11, 2018

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.

@aldyrpl
Copy link

aldyrpl commented Jan 11, 2018

please can you share the code? i am very stuck

@Tabicho
Copy link
Author

Tabicho commented Jan 11, 2018

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

@adelemam
Copy link

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

@aldyrpl
Copy link

aldyrpl commented Apr 23, 2018

i found the answer wait i will check my project

@aldyrpl
Copy link

aldyrpl commented Apr 23, 2018 via email

@chukimmuoi
Copy link

chukimmuoi commented Jun 26, 2018

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

@bwijayap
Copy link

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

@jaydeep43
Copy link

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

@chukimmuoi
Copy link

chukimmuoi commented Sep 29, 2019

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

@ghost
Copy link

ghost commented Jun 20, 2020

this documentation will be useful
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants