2014-09-29 233 views
0

我一直在通過藍牙打印機打印圖像。當我測試它爲 文本打印它完美的作品。但是當涉及到圖像時,只打印字符串字符。 我已將佈局轉換爲位圖。並將其保存到SD卡中。我是否需要將位圖轉換爲支持打印機的東西 。我的應用程序使用「ZEBRA EZ320」打印機。 我用下面的代碼佈局轉換成位圖,打印圖像通過藍牙打印機打印字符串

View rootView = findViewById(android.R.id.content).getRootView(); 
rootView.setDrawingCacheEnabled(true); 
return rootView.getDrawingCache(); 

回答

0

我找到解決我的問題。

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(); 
+0

但我有一些對齊問題。我上面提到的代碼在圖紙的左側打印圖像。我需要將其與中心對齊。建議我任何想法。 – ajantha 2014-10-08 04:55:32

0

在上述答覆中提到的代碼使用ZebraPrinterLegacyDelegator,其被棄用。

使用下面的代碼,

InputStream inputStream = assetManager.open("printing/ic_launcher.png"); 

     ZebraImageI zebraImageI = ZebraImageFactory.getImage(BitmapFactory.decodeStream(inputStream)); 
     zebraPrinter.printImage(zebraImageI, 250, 0, 0, -1, false); 

Zebra打印機實例可以如下創建,

zebraPrinter = ZebraPrinterFactory.getInstance(printerConnection); 

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). 

而且還解決您的對齊問題會更改x值以將圖像移動到您方便的位置。

+0

感謝您的回覆@Gokul。 – ajantha 2015-04-02 06:13:45