yte.. 2022-06-10 16:45 采纳率: 80%
浏览 60
已结题

ios jpg图片转16位bmp图片时,能转成16位,但是图片结果不一样。

ios jpg图片转16位bmp图片时,能转成16位,但是图片结果不一样。

#import "UIImage+BitmapData.h"
# pragma pack(push, 1)
typedef struct s_bitmap_header
{
    UInt16 fileType;
    UInt32 fileSize;
    UInt16 reserved1;
    UInt16 reserved2;
    UInt32 bitmapOffset;
    UInt32 headerSize;
    UInt32 width;
    UInt32 height;
    UInt16 colorPlanes;
    UInt16 bitsPerPixel;
    UInt32 compression;
    UInt32 bitmapSize;
    UInt32 horizontalResolution;
    UInt32 verticalResolution;
    UInt32 colorsUsed;
    UInt32 colorsImportant;
} t_bitmap_header;
#pragma pack(pop)
@implementation UIImage (BitmapData)

- (NSData *) bitmapData :(UIImage *) img
{
    NSData          *bitmapData = nil;
 //   CGImageRef      image = self.CGImage;
    CGImageRef      image = img.CGImage;
    CGContextRef    context = NULL;
    CGColorSpaceRef colorSpace;
    UInt8           *rawData =NULL;
    size_t bitsPerPixel = 16;
    size_t bitsPerComponent = 5;
    size_t bytesPerPixel = bitsPerPixel / bitsPerComponent;

    size_t width = CGImageGetWidth(image);
    size_t height = CGImageGetHeight(image);
    size_t bytesPerRow = width * bytesPerPixel;
    size_t bufferLength = bytesPerRow * height;

    colorSpace = CGColorSpaceCreateDeviceRGB();

    if (colorSpace)
    {
       rawData = (UInt8 *)calloc(bufferLength, sizeof(UInt8));
        if (rawData)
        {
           
            CGBitmapInfo bitmapInfo = kCGImageByteOrder16Little | kCGImageAlphaNoneSkipFirst;
            context = CGBitmapContextCreate(rawData,
                                            width,
                                            height,
                                            bitsPerComponent,
                                            bytesPerRow,
                                            colorSpace,
                                            bitmapInfo);

            if (context)
            {
                CGRect rect = CGRectMake(0, 0, width, height);

                CGContextTranslateCTM(context, 0, height);
                CGContextScaleCTM(context, 1.0, -1.0);
                CGContextDrawImage(context, rect, image);
                bitmapData = [NSData dataWithBytes:rawData length:bufferLength];
                CGContextRelease(context);
            }
            free(rawData);
        }
        CGColorSpaceRelease(colorSpace);
    }
    return bitmapData;
}

- (NSData *)bitmapFileHeaderData :(UIImage *) img
{
   //CGImageRef image = self.CGImage;
    CGImageRef image = img.CGImage;
    UInt32     width = (UInt32)CGImageGetWidth(image);
    UInt32     height = (UInt32)CGImageGetHeight(image);
    t_bitmap_header header;
    //bitmapfileheader
    header.fileType = 0x4D42;//0x4D42
    //header.fileSize = (height * width * 3) + 54;
    header.fileSize = (height * width * 3) + 54;
    header.reserved1 = 0x0000;//0
    header.reserved2 = 0x0000;//0
    header.bitmapOffset = 0x00000036;
    //从文件开始到位图数据开始之间的数据(bitmap data)之间的偏移量
    //bitmapinfoheader
    header.headerSize = 0x00000028;// infoHeader这个头文件的大小
    header.width = width;//图像宽度,单位为像素
    header.height = height;//图像高度,单位为像素
    header.colorPlanes = 0x0001;//默认1
    header.bitsPerPixel = 0x0010;//每个像素比特数
    header.compression = 0x00000000;
    //压缩方式,0表示不压缩,1表示RLE8压缩,2表示RLE4压缩,3表示每个像素值由指定的掩码决定
    header.bitmapSize = width * height * 3;//图片大小
    header.horizontalResolution = 0x00000B13;
    //0x00000000
    //水平分辨率,单位像素/m
    header.verticalResolution = 0x00000B13;
    //垂直分辨率,单位像素/m   //0x00000000
    header.colorsUsed = 0x00000000;
    //BMP图像使用的颜色,0表示使用全部颜色
    header.colorsImportant = 0x00000000;
    //重要的颜色数,此值为0时所有颜色都重要
    NSLog(@"----bitmapFileHeaderData-----%@",[NSData dataWithBytes:&header length:sizeof(t_bitmap_header)]);
    return [NSData dataWithBytes:&header length:sizeof(t_bitmap_header)];
}
- (NSData *)bitmapDataWithFileHeader :(UIImage *) img
{
    NSMutableData *data = [NSMutableData dataWithData:[self bitmapFileHeaderData: img]];
    [data appendData:[self bitmapData:img]];
    int value = arc4random() % 1000;
    NSString* fileName = [NSString stringWithFormat:@"%d%@",value,@".bmp"];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *completePath = [documentsDirectory stringByAppendingPathComponent:fileName];
    NSLog(@"----completePath-----%@",completePath);
    [[NSData dataWithData:data] writeToFile:completePath atomically:YES];
    NSLog(@"--bitmapdata--%@",[NSData dataWithData:data]);
    return [NSData dataWithData:data];
}
@end

#import "UIImage+BitmapData.h"
int main(int argc, char *argv[]) {
    @autoreleasepool {
   
        UIImage *image = [UIImage imageNamed:@"11.png"];
        UIImage *ub  = [[UIImage alloc] init];
        [ub bitmapDataWithFileHeader:image];
    }
   return 0;
}

转前:

img


转后:

img

  • 写回答

1条回答 默认 最新

  • 中考之前不改名 2022-06-10 17:57
    关注

    应该是分辨率太低了,可以看看改下这段里的值,可以多试几次不同值😅

    size_t bitsPerPixel = 16;
        size_t bitsPerComponent = 5;
        size_t bytesPerPixel = bitsPerPixel / bitsPerComponent;
     
        size_t width = CGImageGetWidth(image);
        size_t height = CGImageGetHeight(image);
        size_t bytesPerRow = width * bytesPerPixel;
        size_t bufferLength = bytesPerRow * height;
     
    
    
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 6月16日
  • 创建了问题 6月10日

悬赏问题

  • ¥15 镍氢电池充电器设计实物使用原理
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号