我有兴趣使用蓝牙打印机设备以编程方式从iPhone打印图像。
在其中一个示例代码中,我实现了Text打印的代码,但在该示例中,我没有得到如何将图像转换为格式,如
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 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00
,0x00 ,0x00 ,0x00 ,0x00 ,0x01 ,0x80 ,0x00 ,0x03 ,0x60 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00
,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0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,0x00 ,0x00 ,0x00 ,0x03 ,0xE0 ,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 ,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 ,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
};
//[self addLabel:@"buffer3 is selected"];
[[session outputStream] write:(const uint8_t *)buffer3 maxLength:796];//send print package*/
这是图像打印的示例代码我无法打印任何其他图像,因为我不知道如何将UIImage或NSData转换为下面提到的位代码。如果有任何正确的逐步方法来进行图像到位图的转换,那么请让我知道这一点。
提前致谢。
答案 0 :(得分:1)
你可以看一下这个问题,这个问题基本上和我提出的问题相同:How do I convert a hex array into a UIImage?
查看p25打印机的开发者指南(pdf附在答案中的上述链接中) [特别是在第20-24页中挣扎; p。某些示例为48 - 53,示例代码中显示的iPhone特定打印命令为p.56 第22页显示了图像是如何由像素组成的。我正在考虑将.png图像转换为.bmp或一些简单的黑白图像格式,我还不知道。如果您有任何成功,您可以分享。在其他六个问题中给出的解决方案与您的解决方案完全相同,但我没有完全解决问题(尽管我还没有放弃)
祝你好运!<强>更新强>
到目前为止,我已经设法通过将像素值设置为黑色或白色来将.png图像转换为黑白图像,具体取决于RGB值的平均值。我的代码是来自不同帖子的代码的组合,我没有在这里发布链接。
每个像素由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字符,这可能意味着我的打印图像命令输入错误。
有关我的代码的任何评论将不胜感激。希望这会有所帮助。
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