问题
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. Do I need to convert the bitmap into something that supports for printer. Am using "ZEBRA EZ320" printer for my application. 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();
回答1:
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();
回答2:
The code mentioned in the above answer uses ZebraPrinterLegacyDelegator, which is deprecated.
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,
zebraPrinter = ZebraPrinterFactory.getInstance(printerConnection);
printImage arguments are as follows,
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.
来源:https://stackoverflow.com/questions/26100430/print-image-via-bluetooth-printer-prints-string