蓝牙打印机打印QR图像android studio

时间:2018-11-07 02:07:59

标签: android android-bluetooth printers thermal-printer

蓝牙打印机可以打印QR图像吗?

我无法打印图像,只能打印文字。你们有打印QR图像的代码吗?在此先感谢,谢谢。

1 个答案:

答案 0 :(得分:0)

我在旧项目中使用了此位图-> byteArray转换。 因此,生成QRcode位图,然后执行outPutStream.write(printDraw(youQRBitmap))

fun printDraw(bmp: Bitmap): ByteArray {

            var mBitBuff: ByteArray? = null

            val mWidth = bmp.width
            val mHeight = bmp.height

            val nbm = Bitmap.createBitmap(bmp, 0, 0, mWidth, mHeight)
            val imgbuf = ByteArray(mWidth / 8 * mHeight + 8)

            imgbuf[0] = 29
            imgbuf[1] = 118
            imgbuf[2] = 48
            imgbuf[3] = 0
            imgbuf[4] = (this.width / 8).toByte()
            imgbuf[5] = 0
            imgbuf[6] = (this.getLength() % 256).toByte()
            imgbuf[7] = (this.getLength() / 256).toByte()
            var ext = 7

            for (i in 0 until mHeight) {
                var t = 0
                while (t < mWidth / 8) {
                    val c0 = nbm.getPixel(t * 8 + 0, i)
                    val p0: Byte = if (c0 == -1) {
                        0
                    } else {
                        1
                    }

                    val c1 = nbm.getPixel(t * 8 + 1, i)
                    val p1: Byte = if (c1 == -1) {
                        0
                    } else {
                        1
                    }

                    val c2 = nbm.getPixel(t * 8 + 2, i)
                    val p2: Byte = if (c2 == -1) {
                        0
                    } else {
                        1
                    }

                    val c3 = nbm.getPixel(t * 8 + 3, i)
                    val p3: Byte = if (c3 == -1) {
                        0
                    } else {
                        1
                    }

                    val c4 = nbm.getPixel(t * 8 + 4, i)
                    val p4: Byte = if (c4 == -1) {
                        0
                    } else {
                        1
                    }

                    val c5 = nbm.getPixel(t * 8 + 5, i)
                    val p5: Byte = if (c5 == -1) {
                        0
                    } else {
                        1
                    }

                    val c6 = nbm.getPixel(t * 8 + 6, i)
                    val p6: Byte = if (c6 == -1) {
                        0
                    } else {
                        1
                    }

                    val c7 = nbm.getPixel(t * 8 + 7, i)
                    val p7: Byte = if (c7 == -1) {
                        0
                    } else {
                        1
                    }

                    val value = p0 * 128 + p1 * 64 + p2 * 32 + p3 * 16 + p4 * 8 + p5 * 4 + p6 * 2 + p7.toInt()
                    mBitBuff?.set(t, value.toByte())
                    ++t
                }

                t = 0
                while (t < this.width / 8) {
                    ++ext
                    imgbuf[ext] = mBitBuff?.get(t) ?: 0
                    ++t
                }
            }

            return imgbuf
        }