iphone : image captured from camera changes orientation
Asked Answered
R

4

2

I made an iphone app to capture image from camera and to set that image in next view. But the problem is that image is rotated. i.e landscape image becomes potraite and protraite image becomes landscape. I have referred many codes but could not get solution.

My code is :

- (void)btnCapturePressed
{
 if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {

            picker=[[UIImagePickerController alloc] init];
            picker.delegate=self;

            [picker setAllowsEditing:YES];

            picker.sourceType=UIImagePickerControllerSourceTypeCamera;
            //[self.navigationController pushViewController:(UIViewController *)ipc animated:YES];
            [self presentModalViewController:picker animated:YES];
            [picker release];
      }
}

-(void) imagePickerController:(UIImagePickerController *) picker didFinishPickingMediaWithInfo :(NSDictionary *)info
{
           UIImage *imageToScale=[info objectForKey:UIImagePickerControllerOriginalImage];

            imgView = [[UIImageView alloc] initWithImage:imageToScale];

            [picker presentModalViewController:cropper animated:YES];
}

I have also refered the link. with same problem, but could not find the solution.

Please help me.

Thanks.

Rella answered 2/7, 2012 at 9:54 Comment(7)
does your image get rotated to some specific degree ?Bramwell
so you have to just make them rotated by -90 again before display this will solve you problem if you want how to do it just ask me for helpBramwell
if you need code for it just ask for it I will give you it in answerBramwell
Does the answer is working for youBramwell
What happend does your problem is solved?Bramwell
ya my problem is sloved. thanks a lot for helping : )Rella
YWC and love to help you any time ;)Bramwell
B
4

So for that at the time of image take store the orientation of the device and pass it to the method below as parameter Here just give any name to method and pass the parameter orientation

switch (orientation) {
            case UIDeviceOrientationPortrait:
                [featureLayer setAffineTransform:CGAffineTransformMakeRotation(DegreesToRadians(0.))];
                break;
            case UIDeviceOrientationPortraitUpsideDown:
                [featureLayer setAffineTransform:CGAffineTransformMakeRotation(DegreesToRadians(180.))];
                break;
            case UIDeviceOrientationLandscapeLeft:
                [featureLayer setAffineTransform:CGAffineTransformMakeRotation(DegreesToRadians(90.))];
                break;
            case UIDeviceOrientationLandscapeRight:
                [featureLayer setAffineTransform:CGAffineTransformMakeRotation(DegreesToRadians(-90.))];
                break;
            case UIDeviceOrientationFaceUp:
            case UIDeviceOrientationFaceDown:
            default:
                break; // leave the layer in its last known orientation
        }

and the macro I have used here DegreesToRadians() is as follow

static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};

This will definitely works.

Happy Coding :)

EDIT

If the above code doesn't work well then use this one

@interface UIImage (RotationMethods)
- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees;
@end

@implementation UIImage (RotationMethods)

- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees 
{   
    // calculate the size of the rotated view's containing box for our drawing space
    UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.size.width, self.size.height)];
    CGAffineTransform t = CGAffineTransformMakeRotation(DegreesToRadians(degrees));
    rotatedViewBox.transform = t;
    CGSize rotatedSize = rotatedViewBox.frame.size;

    // Create the bitmap context
    UIGraphicsBeginImageContext(rotatedSize);
    CGContextRef bitmap = UIGraphicsGetCurrentContext();

    // Move the origin to the middle of the image so we will rotate and scale around the center.
    CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);

    //   // Rotate the image context
    CGContextRotateCTM(bitmap, DegreesToRadians(degrees));

    // Now, draw the rotated/scaled image into the context
    CGContextScaleCTM(bitmap, 1.0, -1.0);
    CGContextDrawImage(bitmap, CGRectMake(-self.size.width / 2, -self.size.height / 2, self.size.width, self.size.height), [self CGImage]);

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;

}
@end

and then call the above function as below

switch (orientation) {
        case UIDeviceOrientationPortrait:
            image = [image imageRotatedByDegrees:0];
            break;
        case UIDeviceOrientationPortraitUpsideDown:
            image = [image imageRotatedByDegrees:180];
            break;
        case UIDeviceOrientationLandscapeLeft:
            image = [image imageRotatedByDegrees:-90];
            break;
        case UIDeviceOrientationLandscapeRight:
            image = [image imageRotatedByDegrees:90];
            break;
        case UIDeviceOrientationFaceUp:
        case UIDeviceOrientationFaceDown:
        default:
            break; // leave the layer in its last known orientation
    }   

If the image is not in required orientation then add 90 to all of the above imageRotatedByDegrees's argument (i.e. if it is 0 then it will be 0+90) or as you required.

EDIT 1

UIDeviceOrientation curDeviceOrientation = [[UIDevice currentDevice] orientation];
Bramwell answered 3/7, 2012 at 7:38 Comment(10)
at the time of taking image just pass the device orientation to the method as parameterBramwell
featureLayer is the image which hase to be rotatedBramwell
ok I dont know why but the edited answer will help you. Just put the @implement and @interface portion of edited answer, the first code part as it is to the .m file where you want to call the image rotation and then the switch with required degrees to call as mentioned in answer this will definitely work as it is tested and used in one of my projectBramwell
then just put -90 in the switch statements and its solved so the 4 values are -90, 90, -180, 0 respectively.Bramwell
Just put this code to get the device orientation at the time of taking picture from camera and before saving the image to phoneBramwell
for that put -270 and -90 in place of -180 and 0 respectively and i think this will solve the problem as you said it is rotated by +90Bramwell
the code for getting device orientation gives me signal abort for BAD ACCESS. and shows device orientation UNKNOWNRella
let us continue this discussion in chatBramwell
Hi @ClaricPWI. Can you please clarify how can we get the orientation just after the image capture and before we use that image. I need to set the height and width accordingly.Hach
For that if you only intend to find out portrait or landscape you just use the height & width ratio to find out that image is landscape or portraitBramwell
M
3

I was struggling with this issue but found a much easier solution:

  AVCaptureConnection *vc = [self.snapper connectionWithMediaType:AVMediaTypeVideo];

  // THIS IS THE KEY LINE!!!!!!!!!!!!!!!!!!!!!
  [vc setVideoOrientation:[self interfaceToVideoOrientation]];

  [self.snapper captureStillImageAsynchronouslyFromConnection:vc
                                            completionHandler:
   ^(CMSampleBufferRef buf, NSError *err) {
     NSData* data = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:buf];
     UIImage* im = [UIImage imageWithData:data];
     // Do something with the image.
  }];


- (AVCaptureVideoOrientation)interfaceToVideoOrientation
{
  switch ([UIApplication sharedApplication].statusBarOrientation)
  {
    case UIInterfaceOrientationPortrait:
      return AVCaptureVideoOrientationPortrait;
    case UIInterfaceOrientationPortraitUpsideDown:
      return AVCaptureVideoOrientationPortraitUpsideDown;
    case UIInterfaceOrientationLandscapeRight:
      return AVCaptureVideoOrientationLandscapeRight;
    case UIInterfaceOrientationLandscapeLeft:
      return AVCaptureVideoOrientationLandscapeLeft;
    default:
      return AVCaptureVideoOrientationPortrait;
  }
}

Setting the video orientation before the capture has now fixed my issue with the image coming out with the wrong orientation.

Monasticism answered 24/3, 2015 at 6:19 Comment(1)
After dealing with this orientation issue for hour after hour, you finally helped me. Amazing!! Thank you so much, my app works now.Diagnose
V
1

I refer the discussion over apple's forum (https://discussions.apple.com/thread/1537011?start=0&tstart=0) and found the following solution; It worked perfectly for me; I just copy paste it and it worked like gem;

-(UIImage *) scaleAndRotateImage(UIImage *)image
{
 int kMaxResolution = 320; // Or whatever

 CGImageRef imgRef = image.CGImage;

 CGFloat width = CGImageGetWidth(imgRef);
 CGFloat height = CGImageGetHeight(imgRef);


 CGAffineTransform transform = CGAffineTransformIdentity;
 CGRect bounds = CGRectMake(0, 0, width, height);
 if (width > kMaxResolution || height > kMaxResolution) {
 CGFloat ratio = width/height;
 if (ratio > 1) {
 bounds.size.width = kMaxResolution;
 bounds.size.height = bounds.size.width / ratio;
 }
 else {
 bounds.size.height = kMaxResolution;
 bounds.size.width = bounds.size.height * ratio;
 }
 }

 CGFloat scaleRatio = bounds.size.width / width;
 CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));
 CGFloat boundHeight;
 UIImageOrientation orient = image.imageOrientation;
 switch(orient) {

 case UIImageOrientationUp: //EXIF = 1
 transform = CGAffineTransformIdentity;
 break;

 case UIImageOrientationUpMirrored: //EXIF = 2
 transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);
 transform = CGAffineTransformScale(transform, -1.0, 1.0);
 break;

 case UIImageOrientationDown: //EXIF = 3
 transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);
 transform = CGAffineTransformRotate(transform, M_PI);
 break;

 case UIImageOrientationDownMirrored: //EXIF = 4
 transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);
 transform = CGAffineTransformScale(transform, 1.0, -1.0);
 break;

 case UIImageOrientationLeftMirrored: //EXIF = 5
 boundHeight = bounds.size.height;
 bounds.size.height = bounds.size.width;
 bounds.size.width = boundHeight;
 transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width);
 transform = CGAffineTransformScale(transform, -1.0, 1.0);
 transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
 break;

 case UIImageOrientationLeft: //EXIF = 6
 boundHeight = bounds.size.height;
 bounds.size.height = bounds.size.width;
 bounds.size.width = boundHeight;
 transform = CGAffineTransformMakeTranslation(0.0, imageSize.width);
 transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
 break;

 case UIImageOrientationRightMirrored: //EXIF = 7
 boundHeight = bounds.size.height;
 bounds.size.height = bounds.size.width;
 bounds.size.width = boundHeight;
 transform = CGAffineTransformMakeScale(-1.0, 1.0);
 transform = CGAffineTransformRotate(transform, M_PI / 2.0);
 break;

 case UIImageOrientationRight: //EXIF = 8
 boundHeight = bounds.size.height;
 bounds.size.height = bounds.size.width;
 bounds.size.width = boundHeight;
 transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0);
 transform = CGAffineTransformRotate(transform, M_PI / 2.0);
 break;

 default:
 [NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"];

 }

 UIGraphicsBeginImageContext(bounds.size);

 CGContextRef context = UIGraphicsGetCurrentContext();

 if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) {
 CGContextScaleCTM(context, -scaleRatio, scaleRatio);
 CGContextTranslateCTM(context, -height, 0);
 }
 else {
 CGContextScaleCTM(context, scaleRatio, -scaleRatio);
 CGContextTranslateCTM(context, 0, -height);
 }

 CGContextConcatCTM(context, transform);

 CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);
 UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();

 return imageCopy;
}
Vatic answered 3/1, 2013 at 10:0 Comment(1)
Thank you, this helped me a lot - especially where you show how to extract the orientation information from the image!Art
A
1

I had same problem and this helped for me.Hope it would help you too. It took me 2min to fix this issue. http://xcodetipss.blogspot.com/2012/04/image-rotated-when-taken-from-camera.html

Aphasic answered 2/6, 2014 at 9:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.