简体   繁体   中英

Bluetooth printer to print QR Image android studio

Is Bluetooth printer possible to print QR Image?

I couldn't print Image but only words. Do you guys have the code of printing QR Image? Thanks in advance, Appreciate.

I used this bitmap -> byteArray conversion in my old projects. So generate the QRcode bitmap then do 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
        }
public static byte[] printDraw(Bitmap bmp) {

    byte[] mBitBuff = null;

    int mWidth = bmp.getWidth();
    int mHeight = bmp.getHeight();

    Bitmap nbm = Bitmap.createBitmap(bmp, 0, 0, mWidth, mHeight);
    byte[] imgbuf = new byte[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();
    int ext = 7;

    for (int i=0; i<mHeight; i++) {
        int t = 0;
        while (t < mWidth / 8) {
            int c0 = nbm.getPixel(t * 8 + 0, i);
            int p0 = (c0 == -1) ? 0 : 1;

            int c1 = nbm.getPixel(t * 8 + 1, i);
            int p1 = (c1 == -1) ? 0 : 1;

            int c2 = nbm.getPixel(t * 8 + 2, i);
            int p2 = (c2 == -1) ? 0 : 1;

            int c3 = nbm.getPixel(t * 8 + 3, i);
            int p3 = (c3 == -1) ? 0 : 1;

            int c4 = nbm.getPixel(t * 8 + 4, i);
            int p4 = (c4 == -1) ? 0 : 1;

            int c5 = nbm.getPixel(t * 8 + 5, i);
            int p5 = (c5 == -1) ? 0 : 1;

            int c6 = nbm.getPixel(t * 8 + 6, i);
            int p6 = (c6 == -1) ? 0 : 1;

            int c7 = nbm.getPixel(t * 8 + 7, i);
            int p7 = (c7 == -1) ? 0 : 1;

            int value = p0 * 128 + p1 * 64 + p2 * 32 + p3 * 16 + p4 * 8 + p5 * 4 + p6 * 2 + p7;
            mBitBuff[t] = (byte) value;
            ++t;
        }

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

    return imgbuf;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM