[英]print image via bluetooth printer prints string
I have been working in printing an image via bluetooth printer. 我一直在通过蓝牙打印机打印图像。 When I test it for text printing it works perfectly.
当我对其进行文本打印测试时,它可以完美工作。 But when it comes to image, prints only string characters.
但是当涉及到图像时,仅打印字符串字符。 I have converted the layout into bitmap.
我已经将布局转换为位图。 And saved it into sd card.
并将其保存到SD卡中。 Do I need to convert the bitmap into something that supports for printer.
我是否需要将位图转换为支持打印机的内容。 Am using "ZEBRA EZ320" printer for my application.
我正在为我的应用程序使用“ ZEBRA EZ320”打印机。 I have used the following code to convert the layout into bitmap,
我使用以下代码将布局转换为位图,
View rootView = findViewById(android.R.id.content).getRootView();
rootView.setDrawingCacheEnabled(true);
return rootView.getDrawingCache();
I found solution to my problem. 我找到了解决问题的办法。
Bitmap localBitmap=BitmapFactory.decodeResource(getResources(), image);
BluetoothConnection myConn = new BluetoothConnection(macaddr);
ZebraPrinter myPrinter = new ZebraPrinterCpcl(myConn);
myConn.open();
new ZebraPrinterLegacyDelegator(myPrinter).getGraphicsUtil().printImage(localBitmap, 0, 0, -1, -1, false);
// to reduce extra space
myConn.write("! UTILITIES\r\nIN-MILLIMETERS\r\nSETFF 10 2\r\nPRINT\r\n".getBytes());
myConn.close();
The code mentioned in the above answer uses ZebraPrinterLegacyDelegator, which is deprecated. 上面答案中提到的代码使用了ZebraPrinterLegacyDelegator(已弃用)。
Use the following code, 使用以下代码,
InputStream inputStream = assetManager.open("printing/ic_launcher.png");
ZebraImageI zebraImageI = ZebraImageFactory.getImage(BitmapFactory.decodeStream(inputStream));
zebraPrinter.printImage(zebraImageI, 250, 0, 0, -1, false);
Zebra Printer instance can be created as follow, 可以如下创建Zebra打印机实例,
zebraPrinter = ZebraPrinterFactory.getInstance(printerConnection);
printImage arguments are as follows, printImage参数如下,
image - the image to be printed.
x - horizontal starting position in dots.
y - vertical starting position in dots.
width - desired width of the printed image. Passing a value less than 1 will preserve original width.
height - desired height of the printed image. Passing a value less than 1 will preserve original height.
insideFormat - boolean value indicating whether this image should be printed by itself (false), or is part of a format being written to the connection (true).
And also to address your alignment problems change the x value to move the image to your convenient place. 并且为了解决对齐问题,请更改x值以将图像移至您方便的位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.