从位图数据创建 NSImage

发布于 2024-09-13 05:09:32 字数 1014 浏览 2 评论 0原文

好的,看来我正在创建一个 PDFDocument,其中我创建的图像中的像素宽度不正确。所以问题就变成了:如何获得图像的正确分辨率?

我从扫描仪的位图数据开始。我正在这样做:

CGDataProviderRef provider= CGDataProviderCreateWithData(NULL (UInt8*)data, bytesPerRow * length, NULL);
CGImageRef cgImg =  CGImageCreate (
    width,
    length,
    bitsPerComponent,
    bitsPerPixel,
    bytesPerRow,
    colorspace,
    bitmapinfo, // ?        CGBitmapInfo bitmapInfo,
    provider,   //? CGDataProviderRef provider,
    NULL, //const CGFloat decode[],
    true, //bool shouldInterpolate,
    kCGRenderingIntentDefault // CGColorRenderingIntent intent
    );
/*  CGColorSpaceRelease(colorspace); */

NSData* imgData = [NSMutableData data];
CGImageDestinationRef dest = CGImageDestinationCreateWithData
    (imgData, kUTTypeTIFF, 1, NULL);
CGImageDestinationAddImage(dest, cgImg, NULL);
CGImageDestinationFinalize(dest);
NSImage* img = [[NSImage alloc] initWithData: imgData];

那里似乎没有任何地方可以包含以英寸或点为单位的实际宽度/高度,也没有包含实际分辨率,这是我目前确实知道的......我该怎么做?

Ok, it appears that the I'm creating a PDFDocument where pixelWidth is incorrect in the images that I created. So the question becomes: How do I get the correct resolution into the image?

I start with bitmap data from a scanner. I'm doing this:

CGDataProviderRef provider= CGDataProviderCreateWithData(NULL (UInt8*)data, bytesPerRow * length, NULL);
CGImageRef cgImg =  CGImageCreate (
    width,
    length,
    bitsPerComponent,
    bitsPerPixel,
    bytesPerRow,
    colorspace,
    bitmapinfo, // ?        CGBitmapInfo bitmapInfo,
    provider,   //? CGDataProviderRef provider,
    NULL, //const CGFloat decode[],
    true, //bool shouldInterpolate,
    kCGRenderingIntentDefault // CGColorRenderingIntent intent
    );
/*  CGColorSpaceRelease(colorspace); */

NSData* imgData = [NSMutableData data];
CGImageDestinationRef dest = CGImageDestinationCreateWithData
    (imgData, kUTTypeTIFF, 1, NULL);
CGImageDestinationAddImage(dest, cgImg, NULL);
CGImageDestinationFinalize(dest);
NSImage* img = [[NSImage alloc] initWithData: imgData];

there doesn't appear to be anywhere in there to include the actual width/height in inches or points, nor the actual resolution, which I DO know at this point... how am I supposed to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

铜锣湾横着走 2024-09-20 05:09:33

如果您有大量数据,将其转换为 NSImage 的最简单方法是使用 NSBitmapImageRep。具体来说是这样的:

NSData * byteData = [NSData dataWithBytes:data length:length];
NSBitmapImageRep * imageRep = [NSBitmapImageRep imageRepWithData:byteData];
NSSize imageSize = NSMakeSize(CGImageGetWidth([imageRep CGImage]), CGImageGetHeight([imageRep CGImage]));

NSImage * image = [[NSImage alloc] initWithSize:imageSize];
[image addRepresentation:imageRep];

...use image

If you've got a chunk of data, the easiest way to turn it into an NSImage is to use NSBitmapImageRep. Specifically something like:

NSData * byteData = [NSData dataWithBytes:data length:length];
NSBitmapImageRep * imageRep = [NSBitmapImageRep imageRepWithData:byteData];
NSSize imageSize = NSMakeSize(CGImageGetWidth([imageRep CGImage]), CGImageGetHeight([imageRep CGImage]));

NSImage * image = [[NSImage alloc] initWithSize:imageSize];
[image addRepresentation:imageRep];

...use image
很酷又爱笑 2024-09-20 05:09:33

希望这会有所帮助

size_t bufferLength = width * height * 4;
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, data, bufferLength, NULL);
size_t bitsPerComponent = 8;
size_t bitsPerPixel = 32;
size_t bytesPerRow = 4 * width;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

CGImageRef iref = CGImageCreate(width, 
                                height, 
                                bitsPerComponent, 
                                bitsPerPixel, 
                                bytesPerRow, 
                                colorSpaceRef, 
                                bitmapInfo, 
                                provider,   // data provider
                                NULL,       // decode
                                YES,        // should interpolate
                                renderingIntent);

_image = [[NSImage alloc] initWithCGImage:iref size:NSMakeSize(width, height)];

Hope this will be helpful

size_t bufferLength = width * height * 4;
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, data, bufferLength, NULL);
size_t bitsPerComponent = 8;
size_t bitsPerPixel = 32;
size_t bytesPerRow = 4 * width;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

CGImageRef iref = CGImageCreate(width, 
                                height, 
                                bitsPerComponent, 
                                bitsPerPixel, 
                                bytesPerRow, 
                                colorSpaceRef, 
                                bitmapInfo, 
                                provider,   // data provider
                                NULL,       // decode
                                YES,        // should interpolate
                                renderingIntent);

_image = [[NSImage alloc] initWithCGImage:iref size:NSMakeSize(width, height)];
半山落雨半山空 2024-09-20 05:09:33

通道不是 4 的版本:

size_t bufferLength = width * height * channel;
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, data, bufferLength, NULL);
size_t bitsPerComponent = 8;
size_t bitsPerPixel = bitsPerComponent * channel;
size_t bytesPerRow = channel * width;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast;
if(channel < 4) {
    bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaNone;
}
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

CGImageRef iref = CGImageCreate(width, 
                                height, 
                                bitsPerComponent, 
                                bitsPerPixel, 
                                bytesPerRow, 
                                colorSpaceRef, 
                                bitmapInfo, 
                                provider,   // data provider
                                NULL,       // decode
                                YES,        // should interpolate
                                renderingIntent);

_image = [[NSImage alloc] initWithCGImage:iref size:NSMakeSize(width, height)];

version with channel not 4:

size_t bufferLength = width * height * channel;
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, data, bufferLength, NULL);
size_t bitsPerComponent = 8;
size_t bitsPerPixel = bitsPerComponent * channel;
size_t bytesPerRow = channel * width;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast;
if(channel < 4) {
    bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaNone;
}
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

CGImageRef iref = CGImageCreate(width, 
                                height, 
                                bitsPerComponent, 
                                bitsPerPixel, 
                                bytesPerRow, 
                                colorSpaceRef, 
                                bitmapInfo, 
                                provider,   // data provider
                                NULL,       // decode
                                YES,        // should interpolate
                                renderingIntent);

_image = [[NSImage alloc] initWithCGImage:iref size:NSMakeSize(width, height)];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文