我尝试将 UIImage 转换为字节(NSData),然后将其转换为十六进制字符串,但没有用。它只打印一个黑条而不是图像。我想将 UIImage 转换为 PCX 格式,但找不到好的教程。让我知道一种用斑马打印机打印 UIImage 的方法。
注意:仅 CPCL 语言
尝试以下方法
方法一:
-(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
}
方法二:
-(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;
}