简体   繁体   English

如何在横向模式而不是纵向模式下捕获avfoundation

[英]how to capture avfoundation in landscape mode instead of portrait mode

need help here, been fixing for hours but to no avail. 在这里需要帮助,已经固定好几个小时但无济于事。

i will like to capture a image and show a preview of it, in the AVCaptureVideoPreviewLayer, it show in landscape mode. 我想捕捉一个图像并显示它的预览,在AVCaptureVideoPreviewLayer中,它以横向模式显示。 However when i grab the image it was shown as portrait mode. 然而,当我抓住图像时,它显示为纵向模式。

i'm not sure why, i hope someone could help me :) 我不知道为什么,我希望有人可以帮助我:)

thanks for reading. 谢谢阅读。

when live view 实时查看 在此输入图像描述

when captured and shown in uiimageview 当捕获并在uiimageview中显示时 在此输入图像描述

- (void)addStillImageOutput
{
    [self setStillImageOutput:[[AVCaptureStillImageOutput alloc] init]];
    NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey,nil];
    [[self stillImageOutput] setOutputSettings:outputSettings];

    AVCaptureConnection *videoConnection = nil;
    for (AVCaptureConnection *connection in [[self stillImageOutput] connections]) {
        for (AVCaptureInputPort *port in [connection inputPorts]) {
            if ([[port mediaType] isEqual:AVMediaTypeVideo] ) {
                videoConnection = connection;
                break;
            }
        }
        if (videoConnection) {
            break;
        }
    }

    [[self captureSession] addOutput:[self stillImageOutput]];
}

   - (void)captureStillImage
{
    AVCaptureConnection *videoConnection = nil;
    for (AVCaptureConnection *connection in [[self stillImageOutput] connections]) {
        for (AVCaptureInputPort *port in [connection inputPorts]) {
            if ([[port mediaType] isEqual:AVMediaTypeVideo]) {
                videoConnection = connection;
                break;
            }
        }

        if([videoConnection isVideoOrientationSupported]) 
        {
            UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

             if (orientation == UIInterfaceOrientationLandscapeLeft ) {
                 //NSLog(@"2222UIInterfaceOrientationLandscapeLeft");

            [videoConnection setVideoOrientation:AVCaptureVideoOrientationLandscapeLeft];
             }
             if (orientation == UIInterfaceOrientationLandscapeRight ) 
             {//NSLog(@"222UIInterfaceOrientationLandscapeRight");

            [videoConnection setVideoOrientation:AVCaptureVideoOrientationLandscapeRight];
             }

        }
        if (videoConnection) {
            break;
        }
    }

    //NSLog(@"about to request a capture from: %@", [self stillImageOutput]);
    [[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:videoConnection

                                                         completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error) {
                CFDictionaryRef exifAttachments = CMGetAttachment(imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);

            if (exifAttachments) {
               ////NSLog(@"attachements: %@", exifAttachments);
               }
            else
            {
                         //NSLog(@"no attachments");

            }

        NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
        UIImage *image = [[UIImage alloc] initWithData:imageData];

        //NSData *jpgImageData = UIImageJPEGRepresentation(image, 0.9);
        //UIImage *jpgImage = [[UIImage alloc]initWithData:jpgImageData];
        [self setStillImage:image];
        self.imageExifDict = (__bridge NSDictionary *)exifAttachments;
                ////NSLog(@"NSdictionary from CFDict %@",self.imageExifDict);
//
        [[NSNotificationCenter defaultCenter] postNotificationName:kImageCapturedSuccessfully object:nil];

            }];
}

i pass the image from NSData to UIImage 我将图像从NSData传递给UIImage

self.capturedImage.image = [[self captureManager] stillImage];

i did with this C function: 我做了这个C函数:

UIImage *scaleAndRotateImage(UIImage *image)
{
int kMaxResolution = 2048; // 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;
}

This: 这个:

int kMaxResolution = 2048; // Or whatever

because i used in new Ipad with that resolution 因为我在新的Ipad中使用了那个分辨率

More Info : http://blog.logichigh.com/2008/06/05/uiimage-fix/ 更多信息: http//blog.logichigh.com/2008/06/05/uiimage-fix/

You missed one thing.. set the AVCaptureVideoPreviewLayer orientation according to the app orientation. 你错过了一件事......根据应用方向设置AVCaptureVideoPreviewLayer方向。

Eg: 例如:

For ios 6 and above use 适用于ios 6及以上版本

previewLayer.connection.videoOrientation=AVCaptureVideoOrientationLandscapeLeft; previewLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeLeft;

for ios 5 对于ios 5

previewLayer.orientation=AVCaptureVideoOrientationLandscapeLeft; previewLayer.orientation = AVCaptureVideoOrientationLandscapeLeft;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何以横向模式以肖像方式显示? - How to display in landscape mode in portrait? 方向肖像和风景模式 - Orientation portrait and landscape mode 如何在appdelegate上查看横向和纵向模式? - How to check landscape and portrait mode on appdelegate? 如何从纵向自动旋转为横向模式? - How to autorotate from portrait to landscape mode? 如何将视图从纵向模式旋转到横向模式 - How to rotate view from portrait to landscape mode 如何知道横向或纵向模式下的照片? - How to know if photo in Landscape or Portrait mode? pushViewController:-当设备处于横向模式时,如何在仅纵向模式下显示? - pushViewController: - how to display in portrait only mode, when device is in landscape mode? 如果UIViewController的父级处于纵向模式,如何以横向模式加载UIViewController? - How to load a UIViewController in landscape mode if its parent is in Portrait mode? 如何“旋转图像,仅当它处于纵向模式时(但不是在landScape模式下)” - How it is possible to 'rotate image, only if it is in portrait mode (But not in landScape mode)' 为什么我的视图没有以横向显示而不是纵向显示? - Why doesn't my view display in Landscape instead of Portrait mode?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM