如何从unsigned char创建图像* [英] How to create Image from unsigned char *

查看:178
本文介绍了如何从unsigned char创建图像*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用unsigned char *中的原始数据创建一个新图像。我从存储的数据中得到每像素的位数。
我尝试将原始数据转换为NSData,但结果图像为nil,

I am trying to create a new Image from raw data in unsigned char *. I have the bits per pixel from the data stored. I tried converting raw data into NSData, But result image was nil,


unsigned char * bitmap = myData;

unsigned char *bitmap = myData;

NSUInteger myImageDataLength = strlen((const char *)bitmap);

NSUInteger myImageDataLength = strlen( (const char*)bitmap);

NSData * d = [NSData dataWithBytes:bitmap length:myImageDataLength];

NSData *d = [NSData dataWithBytes:bitmap length:myImageDataLength];

imgView.image = [UIImage imageWithData:d];

imgView.image = [UIImage imageWithData:d];

然后我尝试使用上下文来创建一个图像,如下所示:

Then I tried using context to create an image as follows:

CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
int width = mWidth;
int height = mHeight;
size_t bpp = mBpp;
size_t bpc = 8;
size_t bpr = (width * 4);
CGContextRef context = CGBitmapContextCreate(bitmap, width, height, 8, bpr, colorspace, kCGImageAlphaNone);
CGImageRef cgImage = nil;
if (context != nil) {
    cgImage = CGBitmapContextCreateImage (context);
    CGContextRelease(context);
}
CGColorSpaceRelease(colorspace);
UIImage *newImage = [UIImage imageWithCGImage:cgImage];

我收到一条错误消息,例如
< ;错误>:CGBitmapContextCreate:不支持的参数组合:8个整数位/组件; 24位/像素; 3组分色彩空间; kCGImageAlphaNone; 64字节/行。

I am getting an error message like <Error>: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 24 bits/pixel; 3-component color space; kCGImageAlphaNone; 64 bytes/row.

我尝试通过设置多个值来解决它,但无法找到任何结果。我在网上搜索了这个解决方案,但所有其他人都是从现有图像创建一个图像,所以他们得到每个组件的比特值和所有...我怎样才能从这个数据创建图像???

I tried solving it by setting many values but cant find any result. I googled on internet to find this solution but all others are creating an image from existing images so they get the values of bits per components and all that... How can I create image from this data???

原始图片:

产生的图像:

生成的图像应该变形。 (与fatBooth和其他应用程序一样扭曲)

Resulting image should be warped. (Warped same as fatBooth and other application)

推荐答案

以下代码创建 UIImage 来自原始像素数据的实例假设你有:

The following code creates a UIImage instance from raw pixel data assuming you have:


  • pixelData :原始像素数据

  • imageWidth imageHeight :图片尺寸

  • scanWidth :每条扫描线的字节数

  • bytesPerPixel :每个像素使用的字节数,通常为4

  • pixelData: the raw pixel data
  • imageWidth, imageHeight: the image dimensions
  • scanWidth: the number of bytes per scanline
  • bytesPerPixel: the number of bytes used per pixel, usually 4

结果 UIImage 被分配给变量 uiImage

CGDataProviderRef provider = CGDataProviderCreateWithData(
    NULL, 
    pixelData, 
    imageHeight * scanWidth, 
    NULL);

CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

CGImageRef imageRef = CGImageCreate(imageWidth,
    imageHeight,
    8,
    bytesPerPixel * 8,
    scanWidth,
    colorSpaceRef,
    bitmapInfo,
    provider,
    NULL,
    NO,
    renderingIntent);

UIImage *uiImage = [UIImage imageWithCGImage:imageRef];
CGDataProviderRelease(provider);
CGColorSpaceRelease(colorSpaceRef);
CGImageRelease(imageRef);

这篇关于如何从unsigned char创建图像*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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