【问题标题】:Print QR code on zebra printer from android从 android 在斑马打印机上打印 QR 码
【发布时间】:2013-04-26 09:39:01
【问题描述】:

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

我发现了这个: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();

它不起作用。所以....任何帮助表示赞赏:)

【问题讨论】:

    标签: android zebra-printers


    【解决方案1】:

    你似乎已经非常非常接近了。在 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

    【讨论】:

    • 讨厌这样的事情发生-.-
    【解决方案2】:

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

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

    首先我下载了​​ ZXing 并将“zxing-2.1 > core > 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();
    

    所以,希望它对某人有所帮助......

    【讨论】:

    • 但是使用这种方式后你能扫描那个二维码吗?
    • @Sam_k 是的,我能够扫描它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多