我做了一个320*320px的测试bmp,添加了红绿蓝区域。
在文本编辑器中打开它并在开始时删除元数据。
当我使用这段代码时
NSString *imagePath = [[NSBundle mainBundle] pathForResource: @"test3" ofType: @"bmp"];
NSData *data = [NSData dataWithContentsOfFile:imagePath];
CGFloat width = 320.0;
CGFloat height = 320.0;
NSUInteger bitPerByte = 8;
NSUInteger bytePerColor = 3;
NSUInteger len = [data length];
Byte *byteData = (Byte*)malloc(len);
memcpy(byteData, [data bytes], len);
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL,
byteData,
width*height*bytePerColor,
NULL);
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGImageRef imageRef = CGImageCreate(width,
height,
bitPerByte,
bytePerColor*bitPerByte,
bytePerColor*width,colorSpaceRef,
bitmapInfo,
provider,NULL,NO,0);
红色和蓝色在哪里切换
数据看起来像这样
<… 00ff0000 ff0000ff 0000ff00 00ff0000 ff0000ff 0000ff00 00ff0000 ff0000ff 0000ff00 00ff0000
ff0000ff 0000ff00 00ff0000 ff0000ff 0000ff00 00ff0000 ff0000ff 0000ff00 00ff0000 ff0000ff
0000ff00 00ff0000 ff0000ff 0000ff00 00ff0000 ff0000ff 0000ff00 00ff0000 ff0000ff 0000ff00
00ff0000 ff0000ff 0000ff00 00ff0000 ff0000ff 0000ff00 00ff0000 ff0000ff 0000ff00 00ff0000
ff0000ff 0000ff00 00ff0000 ff0000ff 0000ff00 00ff0000 ff0000ff 0000ff00 00ff0000 ff0000ff
0000ff00 00ff0000 ff0000ff 0000ff00 00ff0000 ff0000ff 0000ff00 00ff0000 ff0000ff 0000ff00
00ff0000 ff0000ff 0000ff00 00ff0000 ff0000ff 0000ff00 00ff0000 ff0000ff 0000ff00 00ff0000
ff0000ff 0000ff00 00ff0000 ff0000ff 0000ff00 00ff0000 ff0000ff 0000ff00 00ff0000 ff0000ff
0000ff00 00ff0000 ff0000ff 0000ff00 00ff0000 ff0000ff 0000ff00 00ff0000 ffffffff ffffffff
ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff
ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff
ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff
ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff
ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff
ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff
ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff
ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff
ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff
ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff …>
BTW:8个字节的摸索与图像中颜色分量的个数无关。 NSData 不知道它包含哪些数据。这只是它的描述方法显示数据的方式。
一个解决方案是创建一个 BGR 颜色空间,但我没有这个选项,因为我决定创建一个 iOS 应用程序,因为我对 Mac Cocoa 不是很熟悉。
相反,我手动交换了红色和蓝色值
// on ios i cannot create a BGR colorspace, there for I swap red and blue values
for (NSUInteger i =2; i < len; i+=3) {
Byte l = byteData[i-2];
Byte r = byteData[i];
byteData[i] = l;
byteData[i-2] = r;
}
给我以下代码
NSString *imagePath = [[NSBundle mainBundle] pathForResource: @"test3" ofType: @"bmp"];
NSData *data = [NSData dataWithContentsOfFile:imagePath];
CGFloat width = 320.0;
CGFloat height = 320.0;
NSUInteger bitPerByte = 8;
NSUInteger bytePerColor = 3;
NSUInteger len = [data length];
Byte *byteData = (Byte*)malloc(len);
memcpy(byteData, [data bytes], len);
// on ios i cannot create a BGR colorspace, there for I swap red and blue values
for (NSUInteger i =2; i < len; i+=3) {
Byte l = byteData[i-2];
Byte r = byteData[i];
byteData[i] = l;
byteData[i-2] = r;
}
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL,
byteData,
width*height*bytePerColor,
NULL);
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGImageRef imageRef = CGImageCreate(width,
height,
bitPerByte,
bytePerColor*bitPerByte,
bytePerColor*width,colorSpaceRef,
bitmapInfo,
provider,NULL,NO,0);
这个结果:
所以由于您还没有提供样本数据集,您可能需要调整某些内容,主要是bitPerByte 和bytePerColor。您还必须检查扫描仪是否添加了一些元数据。
我不知道为什么,但是原始数据数组的字节数正好是元数据的两倍,89352 而不是 44676。
使用此代码
NSData *data = [NSData dataWithContentsOfFile:imagePath];
CGFloat width = 146.0;
CGFloat height = 102.0;
NSUInteger bitPerByte = 8;
NSUInteger bytePerColor = 2*3;
NSUInteger len = [data length];
Byte *byteData = (Byte*)malloc(len);
memcpy(byteData, [data bytes], len);
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL,
byteData,
width*height*bytePerColor,
NULL);
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrder16Big;
CGImageRef imageRef = CGImageCreate(width,
height,
bitPerByte * 2,
bytePerColor*bitPerByte ,
bytePerColor*width ,
colorSpaceRef,
bitmapInfo,
provider,NULL,NO,0);
我可以显示