0

UIImage を bytes(NSData) に変換しようとしましたが、それを 16 進文字列に変換しましたが、使用できませんでした。画像の代わりに黒いバーのみを出力します。UIImage を PCX 形式に変換することを考えましたが、適切なチュートリアルが見つかりません。ゼブラプリンターで UIImage を印刷する方法を教えてください。

注: CPCL 言語のみ

以下の方法を試しました

方法 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
}

方法 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
}

その他の方法

-(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;
}
4

1 に答える 1