用 zebra imz220 打印 png - android
print png with zebra imz220 - android
我在将 png 资产打印到 zebra imz220 打印机时遇到问题,它将用于从 android 平板电脑打印收据,并且需要在打印输出的顶部打印徽标.打印图像时,我得到一个字符串输出而不是图像。我尝试了以下 Whosebug post,因为我似乎找不到任何关于如何操作的文档 - print image via bluetooth printer prints string
package com.example.gareth.myzebraprinter;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.zebra.sdk.comm.BluetoothConnection;
import com.zebra.sdk.comm.Connection;
import com.zebra.sdk.comm.ConnectionException;
import com.zebra.sdk.graphics.ZebraImageFactory;
import com.zebra.sdk.graphics.ZebraImageI;
import com.zebra.sdk.printer.PrinterLanguage;
import com.zebra.sdk.printer.ZebraPrinter;
import com.zebra.sdk.printer.ZebraPrinterFactory;
import com.zebra.sdk.printer.ZebraPrinterLanguageUnknownException;
import com.zebra.sdk.util.internal.Base64;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class MainActivity extends AppCompatActivity {
private ZebraPrinter zebraPrinter;
private Connection connection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
connect();
printTest();
}
private void printTest(){
if(zebraPrinter != null){
try {
InputStream inputStream = getAssets().open("ic_launcher.png");
ZebraImageI zebraImageI = ZebraImageFactory.getImage(BitmapFactory.decodeStream(inputStream));
zebraPrinter.printImage(zebraImageI,250,0,0,-1,false);
} catch (IOException e) {
e.printStackTrace();
} catch (ConnectionException e) {
e.printStackTrace();
}
// byte[] rcpt = "------------\n\r test test test".getBytes();
// try {
// connection.write(rcpt);
// } catch (ConnectionException e) {
// Log.e("printTest",e.getMessage());
// }
}
}
private void connect(){
connection = new BluetoothConnection("AC3FA41EF22E");
try
{
connection.open();
}
catch (ConnectionException ex)
{
Log.e("connect","ConnectionException " + ex.getMessage());
mSleeper.sleep(1000);
closeConnection();
}
catch(Exception ex)
{
Log.e("connect","Exception "+ex.getMessage());
}
try
{
zebraPrinter = ZebraPrinterFactory.getInstance(connection);
PrinterLanguage pl = zebraPrinter.getPrinterControlLanguage();
Log.i("PrinterPanguage",pl.toString());
}
catch (ConnectionException ex)
{
Log.e("connect","ConnectionException " + ex.getMessage());
zebraPrinter = null;
mSleeper.sleep(1000);
closeConnection();
} catch(Exception ex)
{
Log.e("connect","Exception " + ex.getMessage());
zebraPrinter = null;
mSleeper.sleep(1000);
closeConnection();
}
}
private void closeConnection(){
if(connection != null){
try
{
connection.close();
}
catch (ConnectionException exx)
{
Log.e("closeConnection", exx.getMessage());
}
}
}
}
我在 RW420 Zebra 打印机中使用以下代码进行打印
Connection connection = new BluetoothConnection("MAC_ADDRESS_HERE");
connection.open();
//reset margin
//ref https://km.zebra.com/kb/index?page=forums&topic=021407fb4efb3012e55595f77007e8a
connection.write("! U1 JOURNAL\r\n! U1 SETFF 100 2\r\n".getBytes());
Bitmap bitmapToPrint = /*GET_YOUR_BITMAP*/
ZebraPrinter printer = ZebraPrinterFactory.getInstance(connection);
ZebraImageAndroid zebraImageToPrint = new ZebraImageAndroid(bitmapToPrint);
printer.printImage(zebraImageToPrint, 0, 0, -1, -1, false);
检查你的打印方法,使用-1 -1到width和height参数来保持Bitmap的原始宽度和高度
还有一个方法:
if(conZebra.isConnected()) {
int widthOfImageInBytes = (bitmap.getWidth() + 7) / 8;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(("! 0 200 200 "
+ bitmap.getHeight()
+ " 1\r\nCG "
+ String.valueOf(widthOfImageInBytes)
+ String.valueOf(" ")
+ String.valueOf(bitmap.getHeight())
+ String.valueOf(" ")
+ String.valueOf(0)
+ String.valueOf(" ")
+ String.valueOf(0)
+ String.valueOf(" ")).getBytes());
conZebra.write(baos.toByteArray());
OutputStream compressedBitmapOutputStreamCpcl = new CompressedBitmapOutputStreamCpcl(conZebra);
DitheredImageProvider.getDitheredImage(bitmap, compressedBitmapOutputStreamCpcl);
compressedBitmapOutputStreamCpcl.close();
conZebra.write("\r\nFORM\r\nPRINT\r\n".getBytes());
//printer.getGraphicsUtil().printImage(bitmap, 0, 0, -1, -1, false);
Utils.DebugLog(Utils.TAG_PRINTERMODULE, "Bitmap enviado...");
} else {
Utils.DebugLog(Utils.TAG_PRINTERMODULE, "La impresora no est� conectada, imprimirBitmap() fall�");
}
我在将 png 资产打印到 zebra imz220 打印机时遇到问题,它将用于从 android 平板电脑打印收据,并且需要在打印输出的顶部打印徽标.打印图像时,我得到一个字符串输出而不是图像。我尝试了以下 Whosebug post,因为我似乎找不到任何关于如何操作的文档 - print image via bluetooth printer prints string
package com.example.gareth.myzebraprinter;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.zebra.sdk.comm.BluetoothConnection;
import com.zebra.sdk.comm.Connection;
import com.zebra.sdk.comm.ConnectionException;
import com.zebra.sdk.graphics.ZebraImageFactory;
import com.zebra.sdk.graphics.ZebraImageI;
import com.zebra.sdk.printer.PrinterLanguage;
import com.zebra.sdk.printer.ZebraPrinter;
import com.zebra.sdk.printer.ZebraPrinterFactory;
import com.zebra.sdk.printer.ZebraPrinterLanguageUnknownException;
import com.zebra.sdk.util.internal.Base64;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class MainActivity extends AppCompatActivity {
private ZebraPrinter zebraPrinter;
private Connection connection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
connect();
printTest();
}
private void printTest(){
if(zebraPrinter != null){
try {
InputStream inputStream = getAssets().open("ic_launcher.png");
ZebraImageI zebraImageI = ZebraImageFactory.getImage(BitmapFactory.decodeStream(inputStream));
zebraPrinter.printImage(zebraImageI,250,0,0,-1,false);
} catch (IOException e) {
e.printStackTrace();
} catch (ConnectionException e) {
e.printStackTrace();
}
// byte[] rcpt = "------------\n\r test test test".getBytes();
// try {
// connection.write(rcpt);
// } catch (ConnectionException e) {
// Log.e("printTest",e.getMessage());
// }
}
}
private void connect(){
connection = new BluetoothConnection("AC3FA41EF22E");
try
{
connection.open();
}
catch (ConnectionException ex)
{
Log.e("connect","ConnectionException " + ex.getMessage());
mSleeper.sleep(1000);
closeConnection();
}
catch(Exception ex)
{
Log.e("connect","Exception "+ex.getMessage());
}
try
{
zebraPrinter = ZebraPrinterFactory.getInstance(connection);
PrinterLanguage pl = zebraPrinter.getPrinterControlLanguage();
Log.i("PrinterPanguage",pl.toString());
}
catch (ConnectionException ex)
{
Log.e("connect","ConnectionException " + ex.getMessage());
zebraPrinter = null;
mSleeper.sleep(1000);
closeConnection();
} catch(Exception ex)
{
Log.e("connect","Exception " + ex.getMessage());
zebraPrinter = null;
mSleeper.sleep(1000);
closeConnection();
}
}
private void closeConnection(){
if(connection != null){
try
{
connection.close();
}
catch (ConnectionException exx)
{
Log.e("closeConnection", exx.getMessage());
}
}
}
}
我在 RW420 Zebra 打印机中使用以下代码进行打印
Connection connection = new BluetoothConnection("MAC_ADDRESS_HERE");
connection.open();
//reset margin
//ref https://km.zebra.com/kb/index?page=forums&topic=021407fb4efb3012e55595f77007e8a
connection.write("! U1 JOURNAL\r\n! U1 SETFF 100 2\r\n".getBytes());
Bitmap bitmapToPrint = /*GET_YOUR_BITMAP*/
ZebraPrinter printer = ZebraPrinterFactory.getInstance(connection);
ZebraImageAndroid zebraImageToPrint = new ZebraImageAndroid(bitmapToPrint);
printer.printImage(zebraImageToPrint, 0, 0, -1, -1, false);
检查你的打印方法,使用-1 -1到width和height参数来保持Bitmap的原始宽度和高度
还有一个方法:
if(conZebra.isConnected()) {
int widthOfImageInBytes = (bitmap.getWidth() + 7) / 8;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(("! 0 200 200 "
+ bitmap.getHeight()
+ " 1\r\nCG "
+ String.valueOf(widthOfImageInBytes)
+ String.valueOf(" ")
+ String.valueOf(bitmap.getHeight())
+ String.valueOf(" ")
+ String.valueOf(0)
+ String.valueOf(" ")
+ String.valueOf(0)
+ String.valueOf(" ")).getBytes());
conZebra.write(baos.toByteArray());
OutputStream compressedBitmapOutputStreamCpcl = new CompressedBitmapOutputStreamCpcl(conZebra);
DitheredImageProvider.getDitheredImage(bitmap, compressedBitmapOutputStreamCpcl);
compressedBitmapOutputStreamCpcl.close();
conZebra.write("\r\nFORM\r\nPRINT\r\n".getBytes());
//printer.getGraphicsUtil().printImage(bitmap, 0, 0, -1, -1, false);
Utils.DebugLog(Utils.TAG_PRINTERMODULE, "Bitmap enviado...");
} else {
Utils.DebugLog(Utils.TAG_PRINTERMODULE, "La impresora no est� conectada, imprimirBitmap() fall�");
}