在android上打印zebra打印机上的二维码

时间:2013-04-26 09:39:02

标签: android zebra-printers

我有Zebra MZ 220打印机,我需要通过蓝牙从我的Android应用程序打印QR码。我能够打印文本和图像,但不能打印QR码。

我发现了这个:https://km.zebra.com/kb/index?page=content&id=SO7133&actp=LIST_POPULAR

所以,这是我的代码:

new Thread(new Runnable() {
public void run() {
    try {

        // Instantiate connection for given Bluetooth® MAC Address.
        ZebraPrinterConnection thePrinterConn = new BluetoothPrinterConnection("XX:XX:XX:XX:XX:XX");

        // Initialize 
        Looper.prepare();

        // Open the connection - physical connection is established here.
        thePrinterConn.open();



        // SO THIS SHOULD PRINT THE QR CODE BUT DOESN'T :(
        thePrinterConn.write("! 0 200 200 500 1\r\nB QR 10 100 M 2 U 10\r\nMA,QR code ABC123\r\nENDQR\r\nFORM\r\nPRINT".getBytes());



        //Make sure the data got to the printer before closing the connection
        Thread.sleep(500);

        // Close the connection to release resources.
        thePrinterConn.close();

        Looper.myLooper().quit();

    } catch (Exception e) {
        // Handle communications error here
        e.printStackTrace();
    }
}
}).start();
它不会工作。所以......任何帮助都表示赞赏:)

2 个答案:

答案 0 :(得分:5)

你似乎非常非常接近。在CPCL(RW的本机语言)中,所有命令都必须以新行和回车符结束。在您的代码中,这与每个CPCL命令之后的“\ r \ n”相关。看起来您忘记在CPCL链中输入最后的PRINT命令后输入“\ r \ n”。

希望此信息有助于将来,而不是切换到另一个框架。使用Zebra SDK将纯CPCL命令发送到打印机将具有明显更小的带宽,并且应该比生成QR条形码位图并将整个事件发送得更快。使用本机CPCL时,它甚至可以以更高的质量打印(因此更容易扫描)。而且您不必在应用程序中捆绑另一个JAR。

参考:CPCL手册(第2页第1页注释):http://www.zebra.com/content/dam/zebra/manuals/en-us/printer/cpcl-pm-en.pdf

答案 1 :(得分:0)

嗯......回答我自己的问题....:D

首先,我放弃尝试使用这种集成的Zebra东西并决定使用ZXing(“Zebra Crossing”)(http://code.google.com/p/zxing/)。 我找到了这个教程http://www.mysamplecode.com/2012/09/android-generate-qr-code-using-zxing.html并且能够成功。

首先我下载了​​ZXing并包含了'zxing-2.1>核心> core.jar'到我的项目。 我在上面的教程中添加了“GenerateQRCodeActivity.java”,“QRCodeEncoder.java”和“Contents.java”。

这是我的代码:

// Value for encoding
String encode_value = "http://www.google.com/";

// Size of the QR code
int qr_size = 200 * 3/4;

QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(encode_value,
        null,
        Contents.Type.TEXT, 
        BarcodeFormat.QR_CODE.toString(),
        qr_size);

// Get QR code as bitmap
final Bitmap bitmap = qrCodeEncoder.encodeAsBitmap();

new Thread(new Runnable() {
    public void run() {
        try {

            // Instantiate connection for given Bluetooth® MAC Address.
            ZebraPrinterConnection thePrinterConn = new BluetoothPrinterConnection("XX:XX:XX:XX:XX:XX");

            // Initialize 
            Looper.prepare();

            // Open the connection - physical connection is established here.
            thePrinterConn.open();


            // Print QR code
            ZebraPrinter printer = ZebraPrinterFactory.getInstance(thePrinterConn);
            printer.getGraphicsUtil().printImage(bitmap, 75, 0, 250, 250, false);


            //Make sure the data got to the printer before closing the connection
            Thread.sleep(500);

            // Close the connection to release resources.
            thePrinterConn.close();

            Looper.myLooper().quit();

        } catch (Exception e) {

            // Handle communications error here
            e.printStackTrace();

        }
    }
}).start();

所以,希望它有助于某人...

相关问题