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

Printing stuck issue #29

Open
ramana-chirag opened this issue Dec 20, 2024 · 0 comments
Open

Printing stuck issue #29

ramana-chirag opened this issue Dec 20, 2024 · 0 comments

Comments

@ramana-chirag
Copy link

Printing gets stuck after 2-3 print jobs. I tried using the reset command and Thread.sleep(100) but the issue persists. I noticed that the printer specifically gets stuck while printing barcodes, whereas normal printing works without any problems.

this is my code

class PrintLabelTask(private val context: Context) {
    var printama: Printama? =null

    fun printLabel(view:ConstraintLayout){
        if (printama != null && printama!!.isConnected){
            print()
        }else{
            Log.e("ConnectBluetooth","::::")
            Printama.with(context).connect{
                printama = it
                print()
            }
        }
    }

    private fun print(){
        if (printama != null && printama?.isConnected == true){
            Log.e("threadName",Thread.currentThread().name)

            printama?.printText("\u001B\u0040")
            printama?.setBold()
            printama?.printTextlnWideBold("REDUCED", Printama.CENTER)
            printama?.printTextln("\u001B\u0061\u0001"+"WARBURTON HALF & HALF")

            val totalPageLength = 32
            val txtFrom = "From "
            val txtTo = "|To "
            var fromValue = "1.99"
            var toValue = "90p"
            val currencySign = "$"
            val totalTextLength = txtFrom.length+(fromValue.length+1) + txtTo.length + toValue.length
            if (totalTextLength < totalPageLength){
                val availableLength = totalPageLength - totalTextLength
                val extraSpace = (availableLength/10).toInt()
                fromValue = " ".repeat(extraSpace-1) + fromValue + " ".repeat(extraSpace)
                toValue = "".repeat(extraSpace) + toValue + " ".repeat(extraSpace)
            }

            printama?.printTextBold(txtFrom)
            printama?.setDeleteLine()
            printama?.printText("\u001B\u0021\u0020"+currencySign+fromValue)
            printama?.printTextBold(txtTo)
            printama?.printTextln("\u001B\u0021\u0020"+toValue+"\u001B\u0021\u0000")
            printama?.printLine()
            generateBarcode("2090023000900").let { printama?.printImage(it) }
            printama?.feedPaper()
            Thread.sleep(100)
        }

    }

    private fun generateBarcode(text:String): Bitmap? {
        return try {
            val height = 72
            val width = 424
            val bitMatrix = MultiFormatWriter().encode(text, BarcodeFormat.CODE_128, width, height)
            val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565)

            for (x in 0 until width) {
                for (y in 0 until height) {
                    bitmap.setPixel(x, y, if (bitMatrix[x, y]) android.graphics.Color.BLACK else android.graphics.Color.WHITE)
                }
            }
            bitmap
        }catch (e:Exception){
            Log.e("BarcodeException", e.printStackTrace().toString())
            null
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant