【问题标题】:AVCaptureStillImageOutput Image returning upside downAVCaptureStillImageOutput 图像倒置返回
【发布时间】:2014-04-21 07:50:54
【问题描述】:

我正在使用captureStillImageAsynchronouslyFromConnection从相机捕捉图像

图像被颠倒保存。我尝试使用

保存图像
UIImage *flippedImage = [UIImage imageWithCGImage:image.CGImage scale:image.scale orientation:UIImageOrientationUp]

但是这个方向只存在于元数据中,当我从文件中复制数据来处理它时,处理后的图像是颠倒的。 (快速浏览显示原件是直立的,但处理/过滤后的图像是颠倒的)。

设置视频捕捉方向的正确方法是什么?

我在视频预览层上设置了方向,但这不会影响AVCaptureStillImageOutput对象缓冲区中的捕获数据。

我阅读了AVCaptureStillImageOutput 标头,但似乎没有说明如何强制相机预览方向指向图像缓冲区。

例如- (AVMetadataObject *)transformedMetadataObjectForMetadataObject:(AVMetadataObject *)metadataObject connection:(AVCaptureConnection *)connection NS_AVAILABLE_IOS(6_0);

谢谢

【问题讨论】:

    标签: ios avcapture


    【解决方案1】:

    正如您所提到的,这样做您只是在更改元数据。 您需要使用此方法来实际翻转图像。

    试试这个:

    - (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;
    }
    

    更多信息here.

    【讨论】:

      【解决方案2】:

      “问题”是图像方向,iOS 相机只返回一个方向的图像,除非你设置了 -videoOrientation 属性来转换已经拍摄的图像。所以可能会有不同的问题:

      1. videoOrientation 未正确设置
      2. videoOrientation 完全没有设置

      videoOrientation 属性仅适用于静止图像和视频捕获,不适用于单个缓冲区。
      这个 sn-p 来自 Apple AVCam 示例代码。

          [[[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>
      

      首先根据预览视图设置方向,然后拍摄,然后以正确的方向保存到资源库中

      【讨论】:

        猜你喜欢
        • 2013-11-21
        • 1970-01-01
        • 2015-05-06
        • 2023-03-15
        • 1970-01-01
        • 2013-07-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多