Monday, April 16, 2012

Printing bitmaps using CPCL in Android


http://mrpmorris.blogspot.com/2007/08/printing-bitmaps-using-cpcl.html

Thanks a lot. It was really helpful. Zebra documentation is pathetic, no doubt.

I have done a port of this code in Android. I first understood it then, done port for Android.

Hopefully, It will help someone. First convert your image to Black & White(I used Photoshop) which is 8 bits.

ParseBitmap.java
===========

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.util.Log;
import android.widget.Toast;

public class ParseBitmap {

private static final String TAG = "ParseBitmap";
private String m_data;
private Bitmap m_bmp;

public ParseBitmap(Bitmap _bmp){
try{
m_bmp = _bmp;
Log.d(TAG,"Height:"+Integer.toString(m_bmp.getHeight()));
Log.d(TAG,"Widht:"+Integer.toString(m_bmp.getWidth()));

}catch(Exception e){
Log.d(TAG, e.getMessage());
}
}

public String ExtractGraphicsDataForCPCL(int _xpos,int _ypos){
m_data = "";
int color = 0,bit = 0,currentValue = 0,redValue = 0, blueValue = 0, greenValue = 0;

try{
            //Make sure the width is divisible by 8
            int loopWidth = 8 - (m_bmp.getWidth() % 8);
            if (loopWidth == 8)
                loopWidth = m_bmp.getWidth();
            else
                loopWidth += m_bmp.getWidth();
           
m_data = "EG" + " " +
Integer.toString((loopWidth / 8)) + " " +
Integer.toString(m_bmp.getHeight()) + " " +
Integer.toString(_xpos) + " " +
Integer.toString(_ypos) + " ";

            for (int y = 0; y < m_bmp.getHeight(); y++)
            {
                bit = 128;
                currentValue = 0;
                for (int x = 0; x < loopWidth; x++)
                {
                    int intensity = 0;

                    if (x < m_bmp.getWidth())
                    {
                        color = m_bmp.getPixel(x, y);
                       
                        redValue = Color.red(color);
                        blueValue = Color.blue(color);
                        greenValue = Color.green(color);
                       
                        intensity = 255 - ((redValue + greenValue + blueValue) / 3);
                    }
                    else
                        intensity = 0;
                   
                   
                    if (intensity >= 128)
                        currentValue |= bit;
                    bit = bit >> 1;
                    if (bit == 0)
                    {
                    String hex = Integer.toHexString(currentValue);
                    hex = LeftPad(hex);
                    m_data = m_data + hex.toUpperCase();
                        bit = 128;
                        currentValue = 0;
                     
                        /****
                        String dbg = "x,y" + "-"+ Integer.toString(x) + "," + Integer.toString(y) + "-" +
                        "Col:" + Integer.toString(color) + "-" +
                        "Red: " +  Integer.toString(redValue) + "-" +
                        "Blue: " +  Integer.toString(blueValue) + "-" +
                        "Green: " +  Integer.toString(greenValue) + "-" +
                        "Hex: " + hex;
                       
                        Log.d(TAG,dbg);
                        *****/
                     
                    }
                }//x
            }//y
            m_data = m_data + "\r\n";
           
}catch(Exception e){
m_data = e.getMessage();
return m_data;
}

return m_data;
}

private String LeftPad(String _num){
   
    String str = _num;
   
    if (_num.length() == 1)
    {
    str = "0" + _num;
    }
 
    return str;
   }
================================
ZebraDemoActivity.java
==============
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;

import com.zebra.android.comm.ZebraPrinterConnection;
import com.zebra.android.comm.ZebraPrinterConnectionException;
import com.zebra.android.comm.BluetoothPrinterConnection;
import com.zebra.android.printer.PrinterLanguage;
import com.zebra.android.printer.ZebraPrinter;
import com.zebra.android.printer.ZebraPrinterFactory;

public class ZebraDemoActivity extends Activity {

protected ZebraPrinter printer;
private Bitmap m_bmp;
private ParseBitmap m_BmpParser;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
options.inDither = false;
m_bmp = BitmapFactory.decodeResource(getResources(),R.drawable.image,options);
m_BmpParser = new ParseBitmap(m_bmp);

        sendCPCLOverBluetooth("00:22:58:36:5C:25");
        //sendCpclOverBluetooth("00:22:58:36:5C:25");
    }
   
   
    private void sendCPCLOverBluetooth(final String theBtMacAddress) {
               try {
                    // Instantiate connection for given Bluetooth® MAC Address.
                    ZebraPrinterConnection thePrinterConn = new BluetoothPrinterConnection(theBtMacAddress);
     
                    // Initialize
                    //Looper.prepare();
     
                    // Open the connection - physical connection is established here.
                    thePrinterConn.open();
     
                    printer = ZebraPrinterFactory.getInstance(thePrinterConn);
                    PrinterLanguage pl = printer.getPrinterControlLanguage();
                    Toast.makeText(this, pl.toString(), Toast.LENGTH_LONG).show();
                    if (pl == PrinterLanguage.ZPL) {
                    Toast.makeText(this, "ZPL", Toast.LENGTH_LONG).show();
                    } else if (pl == PrinterLanguage.CPCL) {
                    Toast.makeText(this, "CPCL", Toast.LENGTH_LONG).show();
                    }
     
                    String TicketID = "23948234";
                    // This example prints "This is a ZPL test." near the top of the label.
                    //String zplData = "^XA^FO20,20^A0N,25,25^FDThis is a ZPL test.^FS^XZ";
                    //It is taking 50 units for each line so forumula is
                    //(1 blank line on top + number of lines in middle +1 blank line on bottom)* 1st argument of ML command = result
                    //put result as 4th argumenet of ! command"
                   
            String str = m_BmpParser.ExtractGraphicsDataForCPCL(0,0);
                    String zplData = "! 0 200 200 230 1\r\n"
                    + str
                    +"PRINT\r\n";
       
                    // Send the data to printer as a byte array.
                    thePrinterConn.write(zplData.getBytes());
                    //Make sure the data got to the printer before closing the connection
                    //Thread.sleep(5000);
     
                    // Close the connection to release resources.
                    thePrinterConn.close();
     
                    //Looper.myLooper().quit();
                } catch (Exception e) {
                    // Handle communications error here.
                    //e.printStackTrace();
                    Toast.makeText(this, e.getMessage() , Toast.LENGTH_LONG).show();
                }
    }

}