蓝牙打印机打印QR图像安卓工作室



蓝牙打印机是否可以打印QR图像?

我不能打印图像,只能打印文字。你们有打印二维码的代码吗?提前谢谢,谢谢。

我在旧项目中使用了这个位图->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
}
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;
}

最新更新