gpt4 book ai didi

iphone - 如何将图像转换为位图代码以便在 iPhone 中进行蓝牙打印

转载 作者:行者123 更新时间:2023-12-03 21:02:31 26 4
gpt4 key购买 nike

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

在一个示例代码中,我实现了文本打印代码,但在该示例中,我不知道如何将图像转换为格式,例如

        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 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x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00
,0x00 ,0x00 ,0x00 ,0x00 ,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 转换为下面提到的位代码。如果有任何正确的分步方法来进行图像到位图的转换,请告诉我。

提前致谢。

最佳答案

你可以看一下这个问题,和我的问题和你问的基本一样:How do I convert a hex array into a UIImage?

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

祝你好运!

更新

到目前为止,我已成功将 .png 图像转换为黑白图像,方法是根据 RGB 值的平均值将像素值设置为黑色或白色。我的代码是来自不同帖子的代码的组合,我没有在此处发布其链接。

每个像素由 4 个字节表示。将像素的颜色值更改为黑色或白色后,我只需查看每 4 个像素的第一个字节即可进行其余的计算。如果它的值为 0(r、g、b 都 == 0,因此像素的颜色为黑色),我将 2^7 存储在 int var pixelCount 中,用二进制表示 10000000,因此根据打印机的说法是 1 个像素。然后我继续处理下一个像素,如果该组 4 字节的第一个 char 的值为 0,则将 2^6(二进制为 01000000)添加到 pixelCount (像素为黑色)。 8 个像素的 2^ 值相加并存储为表示 8 个像素(1 个字节)的无符号字符。我已将这些字符值的缓冲区数组发送到打印机,但它打印 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

关于iphone - 如何将图像转换为位图代码以便在 iPhone 中进行蓝牙打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13838283/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com