문제

i have Zebra MZ 220 printer, and i need to print the QR code from my android app via bluetooth. I am able to print text and images but not QR code.

I found this: https://km.zebra.com/kb/index?page=content&id=SO7133&actp=LIST_POPULAR

So, here's my code:

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

it dosen't work. So .... any help is appreciated :)

도움이 되었습니까?

해결책

You appear to have been very, very close. In CPCL (the RW's native language), all commands must end with both a new line and carriage return character. In your code, this correlates to an "\r\n" after each and every CPCL command. It looks like you forgot to put an "\r\n" after your final PRINT command in your CPCL chain.

Hopefully this information helps in the future instead of switching over to another framework. Using the Zebra SDK to send pure CPCL commands to the printer would have a significantly smaller bandwidth and should print faster than generating the QR barcode bitmap and sending the whole thing over. It may even print at a higher quality (and therefore be easier to scan) when using the native CPCL. And you wouldn't have to bundle another JAR in your app.

Reference: CPCL manual (section 2 page 1 note): http://www.zebra.com/content/dam/zebra/manuals/en-us/printer/cpcl-pm-en.pdf

다른 팁

Well... to answer my own question.... :D

First of all i gave up on trying with this integrated Zebra stuff and decided to go with ZXing ("Zebra Crossing") (http://code.google.com/p/zxing/). I found this tutorial http://www.mysamplecode.com/2012/09/android-generate-qr-code-using-zxing.html and was able to make it.

So first i downloaded ZXing and included 'zxing-2.1 > core > core.jar' to my project. Than, i added 'GenerateQRCodeActivity.java', 'QRCodeEncoder.java' and 'Contents.java' from the tutorial above.

And here's my code:

// 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();

so, hope it helps someone...

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top