蓝牙打印机打印 QR Image android studio
Posted
技术标签:
【中文标题】蓝牙打印机打印 QR Image android studio【英文标题】:Bluetooth printer to print QR Image android studio 【发布时间】:2018-11-07 02:07:59 【问题描述】:蓝牙打印机可以打印二维码吗?
我无法打印图片,只能打印文字。你们有打印二维码的代码吗?提前致谢,不胜感激。
【问题讨论】:
是热敏蓝牙打印机吗? 是的。我找不到解决方案...所以我需要帮助...请帮助@rubberdont 【参考方案1】:我在我的旧项目中使用了这个位图 -> byteArray 转换。
所以生成二维码位图然后做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
【讨论】:
这是 Kotlin 吗?我无法理解...你有 Java 吗? @rubberdont 我在java中进行了更改,但是出现了一些错误,例如this.getLength(),this.width等。请帮助解决。【参考方案2】: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;
【讨论】:
您没有阅读上面问题中的评论吗?重新格式化不会使这个问题成为答案。以上是关于蓝牙打印机打印 QR Image android studio的主要内容,如果未能解决你的问题,请参考以下文章
Android pad 连接蓝牙打印机Gprinter---实现蓝牙打印功能