AVCaptureStillImageOutput Image returning upside down

不打扰是莪最后的温柔 提交于 2020-01-01 05:33:40

问题


I am capturing images from the camera using captureStillImageAsynchronouslyFromConnection

The images are being saved upside down. I tried saving the image using

UIImage *flippedImage = [UIImage imageWithCGImage:image.CGImage scale:image.scale orientation:UIImageOrientationUp]

but this orientation is only in the metadata and when I copy the data from the file to process it the processed images are upside down. (Quick look shows the originals upright, but the processed/filtered images are upside down).

What is the correct way to set the orientation for the video capturing?

I set the orientation on the video preview layer but that doesn't effect the captured data in the AVCaptureStillImageOutput object's buffer.

I read the AVCaptureStillImageOutput header but there doesn't seem to be an explanation of how to force the camera previews orientation to the image buffer.

e.g. - (AVMetadataObject *)transformedMetadataObjectForMetadataObject:(AVMetadataObject *)metadataObject connection:(AVCaptureConnection *)connection NS_AVAILABLE_IOS(6_0);

Thanks


回答1:


The "problem" is the image orientation, iOS camera returns images only in one orientation, except if you set the -videoOrientation property that transform already the image taken. So there can be different problems:

  1. videoOrientation non set correctly
  2. videoOrientation not set at ALL

The videoOrientation property works only for still images and video capturing, not for single buffers.
This snippet is tanken from Apple AVCam sample code.

    [[[self stillImageOutput] connectionWithMediaType:AVMediaTypeVideo] setVideoOrientation:[[(AVCaptureVideoPreviewLayer *)[[self previewView] layer] connection] videoOrientation]];

    [AVCamViewController setFlashMode:AVCaptureFlashModeAuto forDevice:[[self videoDeviceInput] device]];

    [[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:[[self stillImageOutput] connectionWithMediaType:AVMediaTypeVideo] completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {

        if (imageDataSampleBuffer)
        {
            NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
            UIImage *image = [[UIImage alloc] initWithData:imageData];
            [[[ALAssetsLibrary alloc] init] writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)[image imageOrientation] completionBlock:nil];
        }
    }]; <br>

First it sets the orientation according to the preview view, later it takes the shot, and later it saves it on the asset library with correct orientation




回答2:


As you mentioned, by doing that you are only changing the metadata. You need to use this method to actually flip the image.

Try this:

- (UIImage *)flipImage:(UIImage *)image
{
    UIGraphicsBeginImageContext(image.size);
    CGContextDrawImage(UIGraphicsGetCurrentContext(),CGRectMake(0, 0, image.size.width, image.size.height),image.CGImage);
    UIImage *i = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return i;
}

More info here.



来源:https://stackoverflow.com/questions/22431813/avcapturestillimageoutput-image-returning-upside-down

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!