0

I tried to convert UIImage to bytes(NSData) and then converted it to hex string but no use. it prints only a black bar instead of image.I thought of converting UIImage to PCX format but unable to find good tutorials. Let me know a way to print UIImage with zebra printers.

Note: ONLY CPCL Language

Tried Below Methods

Method 1:

-(void)PrintImage
{
    NSData *data = UIImagePNGRepresentation(image);
    NSString* hex = [self hexRepresentationWithSpaces_AS:NO data:data];
    NSMutableString * str = [NSMutableString new];
    [str appendString:@"! 0 200 200 210 1\r\nEG 40 80 0 0\n"];
    [str appendString:hex];
    [str appendString:@"\r\nPRINT\r\n"];
    //Sending this command to Zebra Printer
}

Method 2:

-(void)PrintImage
{
    id<ZebraPrinter,NSObject> printer = [ZebraPrinterFactory getInstance:connection error:&error];

    id<GraphicsUtil, NSObject> graphicsUtil = [printer getGraphicsUtil];
    [graphicsUtil storeImage:@"1234.jpg" withImage:[image CGIImage] withWidth:-1 andWithHeight:-1 error:&error];

    //What ever the format I send it stores in GRF file but the CPCL command accepts only .PCX file to print stored image

    NSString str = @"\n! 0 200 200 500 1 \nPCX 0 30 !<1234.PCX \nPRINT\n";
    //Sending this command to Zebra Printer
}

Other Methods

-(NSString*)hexRepresentationWithSpaces_AS:(BOOL)spaces data:(NSData *)data
{
const unsigned char* bytes = (const unsigned char*)[data bytes];
NSUInteger nbBytes = [data length];
//If spaces is true, insert a space every this many input bytes (twice this many output characters).
static const NSUInteger spaceEveryThisManyBytes = 4UL;
//If spaces is true, insert a line-break instead of a space every this many spaces.
static const NSUInteger lineBreakEveryThisManySpaces = 4UL;
const NSUInteger lineBreakEveryThisManyBytes = spaceEveryThisManyBytes * lineBreakEveryThisManySpaces;
NSUInteger strLen = 2*nbBytes + (spaces ? nbBytes/spaceEveryThisManyBytes : 0);

NSMutableString* hex = [[NSMutableString alloc] initWithCapacity:strLen];
for(NSUInteger i=0; i<nbBytes; ) {
    [hex appendFormat:@"%02X", bytes[i]];
    //We need to increment here so that the every-n-bytes computations are right.
    ++i;

    if (spaces) {
        if (i % lineBreakEveryThisManyBytes == 0) [hex appendString:@"\n"];
        else if (i % spaceEveryThisManyBytes == 0) [hex appendString:@" "];
    }
}
return hex;
}
Mahesh
  • 29
  • 1
  • 7

1 Answers1

0

You can use Zebra's iOS SDK to print images. It supports the iMZ320. You would use the same logic of extracting the data from a UIImage (or specifically the CGImageRef from a UIImage) and sending it to the printer via the printImage command.

SDK: http://www.zebra.com/us/en/products-services/software/link-os/link-os-sdk.html

If you cannot use the SDK, you will instead need to parse the image data from the UIImage yourself and wrap it with the CPCL command EG (or one of its variants). You can find CPCL graphics commands on section 7 page 7 here: http://www.zebra.com/content/dam/zebra/manuals/en-us/printer/cpcl-pm-en.pdf. If you have already done this much, perhaps you can post your code and someone can show you where you went wrong.

Update July 27th, 2014

I have a couple of thoughts now that you have posted some code.

  1. Try using the SDK method 'printImage' after you have stored the image. There is no reason to send the CPCL command yourself since the SDK should take care of it for you. The SDK should manage the whole PCX vs JPG thing for you. Note: You should only store the image once once on the printer, no need to call storeImage multiple times. While storing extra times doesn't break anything, it is unnecessary and slows down your routine!

  2. The printer supports multiple languages (ZPL, CPCL, Line print, etc.). If I recall correctly, the printers may always accept CPCL commands but still be in ZPL mode. Not sure. Anyway, it is worth checking what language the printer thinks it is in. You can ask with the following query:

    ! U1 getvar "device.languages"

    [Notice there should be a newline or carriage return after that command]

jason.zissman
  • 2,750
  • 2
  • 19
  • 24