How do I convert a hex array into a UIImage?

前端 未结 2 492
醉梦人生
醉梦人生 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:24

    Its easiest to start with a black and white image (I used .png) 4 bytes are allocated to each pixel, the data of which you can get by using CgContextRef and CgContextDrawImage (I used the CGBitmapContextCreate method to populate an array of pixel data as well as create a black and white image from the edited data array. I averaged the rgb values for each pixel and then changed all pixel values to black or white (alpha == 255) depending on the average of rgb's.

    Then you need to package every 8 pixels into 1 print byte (thus for a pixel of 4 bytes, look at a set of 32 bytes in the data array and set the binary values of the print byte on or off depending on whether a pixel is black (add pow(2, 7 - (position of pixel in 8 pixel sequence) ) or white (do nothing).

    I converted a .png image to black and white .png and then to binary and printed the values on my screen. Try to keep the width of your .png in multiples of 8 or you'll have trouble (since you need to convert 8 bytes into 1 byte.

    0 讨论(0)
  • 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);
    
    0 讨论(0)
提交回复
热议问题