苏飞论坛

标题: 求助 RGB转Bitmap [打印本页]

作者: yya5119    时间: 2017-12-11 15:08
标题: 求助 RGB转Bitmap
求助 RGB转Bitmap 谢谢

作者: 站长苏飞    时间: 2017-12-11 16:58
可以看看这个文章基本上就是上面写的方法
https://www.cnblogs.com/pengyingh/articles/2416332.html
[C#] 纯文本查看 复制代码
char* rgba = (char*)malloc(width*height*4);
for(int i=0; i < width*height; ++i) {
    rgba[4*i] = myBuffer[3*i];
    rgba[4*i+1] = myBuffer[3*i+1];
    rgba[4*i+2] = myBuffer[3*i+2];
    rgba[4*i+3] = 0;
}
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef bitmapContext = CGBitmapContextCreate(
    rgba,
    width,
    height,
    8, // bitsPerComponent
    4*width, // bytesPerRow
    colorSpace,
    kCGImageAlphaNoneSkipLast);

CFRelease(colorSpace);

CGImageRef cgImage = CGBitmapContextCreateImage(bitmapContext);
CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, CFSTR("image.png"), kCFURLPOSIXPathStyle, false);

CFStringRef type = kUTTypePNG; // or kUTTypeBMP if you like
CGImageDestinationRef dest = CGImageDestinationCreateWithURL(url, type, 1, 0);

CGImageDestinationAddImage(dest, cgImage, 0);

CFRelease(cgImage);
CFRelease(bitmapContext);
CGImageDestinationFinalize(dest);
free(rgba);




[C#] 纯文本查看 复制代码
int width = 11;
int height = 8;

Byte r[8][11]={
        {000,000,255,000,000,000,000,000,255,000,000},
        {000,000,000,255,000,000,000,255,000,000,000},  
        {000,000,255,255,255,255,255,255,255,000,000},
        {000,255,255,255,255,255,255,255,255,255,000},
        {255,255,255,255,255,255,255,255,255,255,255},
        {255,000,255,255,255,255,255,255,255,000,255},
        {255,000,255,000,000,000,000,000,255,000,255},
        {000,000,000,255,255,000,255,255,000,000,000}};

Byte g[8][11]={
        {000,000,255,000,000,000,000,000,255,000,000},
        {000,000,000,255,000,000,000,255,000,000,000},  
        {000,000,255,255,255,255,255,255,255,000,000},
        {000,255,255,000,255,255,255,000,255,255,000},
        {255,255,255,255,255,255,255,255,255,255,255},
        {255,000,255,255,255,255,255,255,255,000,255},
        {255,000,255,000,000,000,000,000,255,000,255},
        {000,000,000,255,255,000,255,255,000,000,000}};

Byte b[8][11]={
        {000,000,255,000,000,000,000,000,255,000,000},
        {000,000,000,255,000,000,000,255,000,000,000},  
        {000,000,255,255,255,255,255,255,255,000,000},
        {000,255,255,000,255,255,255,000,255,255,000},
        {255,255,255,255,255,255,255,255,255,255,255},
        {255,000,255,255,255,255,255,255,255,000,255},
        {255,000,255,000,000,000,000,000,255,000,255},
        {000,000,000,255,255,000,255,255,000,000,000}};

char* rgba = (char*)malloc(width*height*4);
int offset=0;
for(int i=0; i < height; ++i)
{
        for (int j=0; j < width; j++)
        {
                rgba[4*offset]   = r[j];
                rgba[4*offset+1] = g[j];
                rgba[4*offset+2] = b[j];
                rgba[4*offset+3] = 0;
                offset ++;
        }
}


CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef bitmapContext = CGBitmapContextCreate(
    rgba,
    width,
    height,
    8, // bitsPerComponent
    4*width, // bytesPerRow
    colorSpace,
    kCGImageAlphaNoneSkipLast                    
);

CFRelease(colorSpace);

CGImageRef cgImage = CGBitmapContextCreateImage(bitmapContext);

free(rgba);

UIImage *newUIImage = [UIImage imageWithCGImage:cgImage];

UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 11,8)];

[iv setImage:newUIImage];


作者: 范范    时间: 2017-12-14 21:35
路过打个酱油




欢迎光临 苏飞论坛 (http://www.sufeinet.com/) Powered by Discuz! X3.4