How do I convert a hex array into a UIImage?

前端 未结 2 494
醉梦人生
醉梦人生 2021-01-16 15:52

There are several unanswered questions related to printing images dynamically with P25mi, none with accepted answers. A couple links below.

How to convert image to

2条回答
  •  礼貌的吻别
    2021-01-16 16:34

    Have a look at the P25 Development Guide V3.4.1.

    I don't understand what the first five characters are for. But the following characters are an esacpe sequence:

     0x1B,  0x58,  0x31,  0x19, 0x20
    

    They are for printing a bit-image in horizontal mode. 0x19 and 0x20 are the horizontal dimension (times 8) and the vertical dimension, respectively. So the image is 200 by 32 pixel. The following 25 x 32 bytes then contain the pixels (black and white, 1 bit per pixel).

    I would expect the whole thing to be 810 bytes long (5 bytes unidentified, 5 bytes escape sequence, 25 x 32 bytes of pixel data). I don't quite understand why it works with 796.

    Update:

    To convert a UIImage into the raw black and white data (without the escape sequence), the following code should help. I hope 1 bit per pixel bitmaps are still supported.

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
    size_t bytesPerRow = (uiImage.size.width + 7) / 8;
    size_t imageDataSize = uiImage.size.height * bytesPerRow;
    unsigned char* imgData = (unsigned char*) malloc(imageDataSize);
    CGContextRef context =  CGBitmapContextCreate(imgData,
        uiImage.size.width, uiImage.size.height, 
        1, bytesPerRow, 
        colorSpace, kCGImageAlphaNone);
    
    UIGraphicsPushContext(context);
    [uiImage drawInRect: CGRectMake(0.0, 0.0, uiImage.size.width, uiImage.size.height)];
    UIGraphicsPopContext();
    
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    
    for (int i = 0; i < imageDataSize; i++)
        imgData[i] = ~imgData[i];
    
    ... send the image (imgData) to the printer ...
    
    free(imgData);
    

提交回复
热议问题