Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Camera image orientation

In my app I'm capturing images using the Camera. These are being stored in an NSArray as an NSData representation. When I convert the NSData back to the image, the orientation is now landscape instead of portrait as I took it.

    NSData *data = UIImagePNGRepresentation([arrayImage objectAtIndex:0]);
    UIImage *tmp = [UIImage imageWithData:data];

Anyone have an explanation? Thanks.

like image 446
DNewell Avatar asked Apr 15 '11 19:04

DNewell


People also ask

What is camera image orientation?

What is Camera Orientation? There are two kinds of camera orientations, Vertical or Portrait, and Horizontal or Landscape. By simply rotating your camera and changing the direction, you can achieve different looks on the images.

How do I fix the orientation of a picture?

Right-click the image and select Details to reveal a screen with metadata, including EXIF data, that you can adjust if the image supports it. Force a preferred orientation. Rotate the image, then save it. That process reconstructs the image along the requested dimensions.

How do I change the camera orientation on my phone?

Things You Should Know. Swipe down from the top of your screen to enable "Auto-Rotate," then turn your phone to the side to switch to landscape mode. Turn your phone upright to switch back to portrait mode.


2 Answers

you should fix orientation of image captured by camera the code follows, by default the orientation of camera image is not correct

- (UIImage *)fixrotation:(UIImage *)image
{

    if (image.imageOrientation == UIImageOrientationUp) return image;
    CGAffineTransform transform = CGAffineTransformIdentity;

    switch (image.imageOrientation) {
        case UIImageOrientationDown:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, image.size.width, image.size.height);
            transform = CGAffineTransformRotate(transform, M_PI);
            break;

        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
            transform = CGAffineTransformTranslate(transform, image.size.width, 0);
            transform = CGAffineTransformRotate(transform, M_PI_2);
            break;

        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, 0, image.size.height);
            transform = CGAffineTransformRotate(transform, -M_PI_2);
            break;
        case UIImageOrientationUp:
        case UIImageOrientationUpMirrored:
            break;
    }

    switch (image.imageOrientation) {
        case UIImageOrientationUpMirrored:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, image.size.width, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;

        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, image.size.height, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;
        case UIImageOrientationUp:
        case UIImageOrientationDown:
        case UIImageOrientationLeft:
        case UIImageOrientationRight:
            break;
    }

    // Now we draw the underlying CGImage into a new context, applying the transform
    // calculated above.
    CGContextRef ctx = CGBitmapContextCreate(NULL, image.size.width, image.size.height,
                                             CGImageGetBitsPerComponent(image.CGImage), 0,
                                             CGImageGetColorSpace(image.CGImage),
                                             CGImageGetBitmapInfo(image.CGImage));
    CGContextConcatCTM(ctx, transform);
    switch (image.imageOrientation) {
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            // Grr...
            CGContextDrawImage(ctx, CGRectMake(0,0,image.size.height,image.size.width), image.CGImage);
            break;

        default:
            CGContextDrawImage(ctx, CGRectMake(0,0,image.size.width,image.size.height), image.CGImage);
            break;
    }

    // And now we just create a new UIImage from the drawing context
    CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
    UIImage *img = [UIImage imageWithCGImage:cgimg];
    CGContextRelease(ctx);
    CGImageRelease(cgimg);
    return img;
}
like image 63
suvish valsan Avatar answered Oct 10 '22 01:10

suvish valsan


I think this is a bug with the SDK. I ran into this exact problem and then switched over to UIImageJPEGRepresentation which fixed the problem.

like image 20
FreaknBigPanda Avatar answered Oct 10 '22 02:10

FreaknBigPanda