簡體   English   中英

使用CGImageSourceRef加載圖像時旋轉錯誤

[英]Wrong rotation when loading image using CGImageSourceRef

我使用下面的代碼在C ++庫中加載圖像。 加載某些圖像時,圖像的旋轉是錯誤的。 它似乎會影響來自iPhone相機的JPEG。 我該如何解決?

據推測,有一個標志位於相機捕獲的JPEG設置。 以這種方式加載圖像時我不確定如何訪問它。

CGImageSourceRef source = CGImageSourceCreateWithURL(url, NULL);
CFRelease(url);

if(source==NULL)
    return IM_ErrFileError;

CGImageRef image = CGImageSourceCreateImageAtIndex(source, 0, NULL);
CFRelease(source);

if(image==NULL)
    return IM_ErrFileError;

int w = (int)CGImageGetWidth(image);
int h = (int)CGImageGetHeight(image);

im::Err err = img.Create(im::IntSize(w, h), bands, sizeof(uint8_t), imgtype);
if(IM_FAILED(err))
{
    CGImageRelease(image);
    return err;
}

img.Clear();

CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(colorspacename);
if(colorSpace==NULL)
{
    CGImageRelease(image);
    return IM_ErrAlloc;
}

// Create RGBA context
CGContextRef context = CGBitmapContextCreate(img.BytePtr(), w, h, 8, img.StrideBytes(), colorSpace, alphainfo);

if(context==NULL)
{
    CGColorSpaceRelease(colorSpace);
    CGImageRelease(image);
    return IM_ErrAlloc;
}

CGColorSpaceRelease(colorSpace);

CGRect rect;

rect.origin.x = 0;
rect.origin.y = 0;
rect.size.width = w;
rect.size.height = h;

CGContextDrawImage(context, rect, image);

CGContextRelease(context);
CGImageRelease(image);

基於答案,這是我修改代碼的方式。 首先得到方向:

int orientation = 1;

CFDictionaryRef dict = CGImageSourceCopyPropertiesAtIndex(source, 0, NULL);
if(dict)
{
    CFNumberRef imageOrientation;

    if(CFDictionaryGetValueIfPresent(dict, kCGImagePropertyOrientation, (const void **)&imageOrientation))
    {
        if(imageOrientation)
            CFNumberGetValue(imageOrientation, kCFNumberIntType, &orientation);
    }

    CFRelease(dict);
}

確保尺寸正確:

int width = (int)CGImageGetWidth(image);
int height = (int)CGImageGetHeight(image);

int canvasw, canvash;

if(orientation<=4)
{
    canvasw = width;
    canvash = height;
}
else
{
    canvasw = height;
    canvash = width;
}

使用畫布大小創建位圖上下文圖像。 渲染時補償方向:

switch(orientation)
{
    case 2:
        // 2 = 0th row is at the top, and 0th column is on the right - Flip Horizontal
        CGContextConcatCTM(context, CGAffineTransformMake(-1.0, 0.0, 0.0, 1.0, width, 0.0));
        break;

    case 3:
        // 3 = 0th row is at the bottom, and 0th column is on the right - Rotate 180 degrees
        CGContextConcatCTM(context, CGAffineTransformMake(-1.0, 0.0, 0.0, -1.0, width, height));
        break;

    case 4:
        // 4 = 0th row is at the bottom, and 0th column is on the left - Flip Vertical
        CGContextConcatCTM(context, CGAffineTransformMake(1.0, 0.0, 0, -1.0, 0.0, height));
        break;

    case 5:
        // 5 = 0th row is on the left, and 0th column is the top - Rotate -90 degrees and Flip Vertical
        CGContextConcatCTM(context, CGAffineTransformMake(0.0, -1.0, -1.0, 0.0, height, width));
        break;

    case 6:
        // 6 = 0th row is on the right, and 0th column is the top - Rotate 90 degrees
        CGContextConcatCTM(context, CGAffineTransformMake(0.0, -1.0, 1.0, 0.0, 0.0, width));
        break;

    case 7:
        // 7 = 0th row is on the right, and 0th column is the bottom - Rotate 90 degrees and Flip Vertical
        CGContextConcatCTM(context, CGAffineTransformMake(0.0, 1.0, 1.0, 0.0, 0.0, 0.0));
        break;

    case 8:
        // 8 = 0th row is on the left, and 0th column is the bottom - Rotate -90 degrees
        CGContextConcatCTM(context, CGAffineTransformMake(0.0, 1.0, -1.0, 0.0, height, 0.0));
        break;

    default:
        break;
}

我實際上沒有嘗試使用iPhone圖像,但我建議使用CGImageSourceCopyPropertiesAtIndex來獲取kCGImagePropertyOrientation 一旦知道了正確的方向,就可以在繪制時應用一些變換。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM