如何在iphone中将图像转换为蓝牙打印的位图代码 [英] How to convert image to bitmap code for bluetooth print in iphone

查看:107
本文介绍了如何在iphone中将图像转换为蓝牙打印的位图代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有兴趣使用蓝牙打印机设备以编程方式从iPhone打印图像。

I am interested to print image from iPhone programmatically using bluetooth printer device.

在其中一个示例代码中,我实现了文本打印的代码但在该示例中我我没有得到如何将图像转换为格式,如

In one of the sample code I have implemented code for Text print but in that sample I am not getting how to convert image into format, like

        unsigned char buffer3[796]={
        0x55 ,  0x66 ,  0x77 ,  0x88 ,  0x44 ,  0x1B ,  0x58 ,  0x31 ,  
        0x19, 0x20, 
        0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 
        ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00
        ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 
        ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00
        ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 
        ,0x00 ,0x00 ,0x00 ,0x80 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 
        ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x01 ,0xC0 ,0x00
        ,0x00 ,0x00 ,0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        ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 
        ,0x00 ,0x00 ,0x00   
    };


    //[self addLabel:@"buffer3 is selected"];
    [[session outputStream] write:(const uint8_t *)buffer3 maxLength:796];//send print package*/

这是图像打印的示例代码我无法打印任何其他图像,因为我不知道如何将UIImage或NSData转换为下面提到的位代码。如果有任何正确的步骤方法来进行图像到位图的转换,请告诉我这个。

This is sample code for Image print I am not able to print any other image because I am not having idea how to convert UIImage or NSData to the below mentioned Bit code. If any proper step by step method to do Image to bitmap conversion, then please let me know this.

提前致谢。

推荐答案

你可以看看这个问题,这个问题基本上与我的问题和你要问的问题相同:如何将十六进制数组转换为UIImage?

You can have a look at this question, which is basically the same as the one I have and what you are asking: How do I convert a hex array into a UIImage?

查看p25打印机的开发人员指南(pdf附在答案的上述链接中)
[特别是在第20 - 24页中挣扎; p。某些示例为48 - 53,示例代码中显示的iPhone特定打印命令为p.56
p.22显示了图像是如何由像素组成的。我正在考虑将.png图像转换为.bmp或一些简单的黑白图像格式,我还不知道。如果您有任何成功,您可以分享。在其他六个问题中给出的解决方案与您的解决方案完全相同,但我没有帮助我完全解决问题(虽然我还没有放弃)

Have a look at Developer guide for the p25 printer (the pdf is attached at the above link in the answer) [Specifically struggle through p.20 - 24;p. 48 - 53 for some examples and p.56 for iPhone specific print commands which are seen in the sample code] p.22 shows how the images are made up from pixels. I'm thinking in the line of converting a .png image to .bmp or some simple black and white image format, which I don't know yet. You can share if you have any success. The solutions that have been given in the other six questions exactly like yours, haven't help me solve the problem completely yet (though I haven't given up)

好好运!

更新

到目前为止,我已成功转换了一个。通过将像素值设置为黑色或白色,将图像png为黑白图像,具体取决于RGB值的平均值。我的代码是来自不同帖子的代码的组合,我没有在这里发布链接。

Thus far I've managed to convert a .png image to a black and white image by setting the pixel values to black or white, depending on the average of the RGB values. My code is a combination of code from different posts, the links of which I haven't posted here.

每个像素由4个字节表示。一旦我将像素的颜色值更改为黑色或白色,我只需要查看每个像素的第一个字节以进行剩余的计算。如果它的值为0(r,g,b都是== 0,因此像素的颜色为黑色)我将2 ^ 7存储在 int var pixelCount 中,表示二进制的 10000000 ,因此根据打印机为1个像素。然后我继续下一个像素,如果该4字节组的第一个 char 的值为0,则将<^>(二进制为01000000)添加到 pixelCount (像素是黑色)。添加8个像素的2 ^值并将其存储为 unsigned char ,表示8个像素(1个字节)。我已经将这些char值的缓冲区数组发送到打印机,但是它打印了ASCII字符,这可能意味着我的打印图像命令输入不正确。

Each pixel is represented by 4 bytes. Once I've changed the color value of pixel to black or white I only need to look at the first byte of every 4 pixels for the rest of my calculation. If its value is 0 (r, g, b are all == 0 and thus the color of the pixel is black) I store 2^7 in an int var pixelCount, which represents 10000000 in binary, thus 1 pixel according to the printer. I then I continue on to the next pixel, adding 2^6 (01000000 in binary) to pixelCount if the value of the first char for that group of 4 bytes is 0 (pixel is black). The 2^ values for 8 pixels are added and stored as a unsigned char which represents 8 pixels (1 byte). I have sent a buffer array of these char values to the printer, but it prints ASCII characters which might mean my print image command is typed incorrectly.

关于我的代码的任何评论不胜感激。希望这会有所帮助。

Any comments about my code would be appreciated. Hope this helps.

UIImage *image = [UIImage imageNamed:@"label.png"];
CGImageRef imageRef = [image CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

unsigned char *rawData = (unsigned char*) calloc(height*width*4, sizeof(unsigned char));
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel* width;
NSUInteger bitsPerComponent = 8;
//this methid returns the pixel data in rawData
CGContextRef context = CGBitmapContextCreate(rawData,
                                             width,
                                             height,
                                             bitsPerComponent,
                                             bytesPerRow,
                                             colorSpace,
                                             kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Big);

CGContextDrawImage(context, CGRectMake(0,0, width,height), imageRef);
CGContextRelease(context);

size_t length = image.size.height*image.size.width*4;
CGFloat intensity;

//black or white
int bw;

//initialize print buffer
size_t bufferLength = (image.size.height*image.size.width) / 8+10;
unsigned char buffer[bufferLength];

buffer[0] = 0x55; buffer[1] = 0x66; buffer[2] = 0x77; buffer[3] = 0x88; //esc sequence
buffer[4] = 0x44; //print command
buffer[5] = 0x1B; buffer[6] = 0x58; buffer[7] = 0x31;buffer[8] = (char) 200/8 ; buffer[9] = 30 ;  //and iOS specific print image command

int pixelCount = 0;
int byteCount = 10;


//check pixel values and average to black or white (4 bytes per pixel)
for(int index = 0; index<length;index+=4){
    intensity = (float)(rawData[index]+rawData [index+1] + rawData[index+2])/3.;

    if(intensity >= 128 ){
        bw = 255;
        //if pixel white, bit is 0

    }
    else {
        bw = 0;
                    //if black pixel, set one bit of byte on
        buffer[byteCount] += (char) pow(2, 7 - pixelCount);

    }

    //next pixel
    pixelCount++;

    //every 8 pixels, move to next char of print buffer
    if(pixelCount % 8 == 0)
        byteCount++;



    //      NSLog(@"Pixel %i : r: %i g: %i b: %i a: %i", counter++, rawData[index], rawData[index+1], rawData[index+2], rawData[index+3]);

    //save pixel values as black or white for new image
    rawData[index]   = bw;
    rawData[index+1] = bw;
    rawData[index+2] = bw;
    rawData[index+3] = 255;
}


CGContextRef bitmapContext=CGBitmapContextCreate(rawData, image.size.width, image.size.height, 8, 4*image.size.width, colorSpace,  kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

CFRelease(colorSpace);

free(rawData);
CGImageRef cgImage=CGBitmapContextCreateImage(bitmapContext);
CGContextRelease(bitmapContext);

UIImage *bwImage = [UIImage imageWithCGImage:cgImage];
[imageView setImage:bwImage];


//          NSLog(@"2011printing imagelog");
//      //[self addLabel:@"buffer3 is selected"];
//      [[session outputStream] writ

这篇关于如何在iphone中将图像转换为蓝牙打印的位图代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆