【问题标题】:Camera image orientation相机图像方向
【发布时间】:2011-04-15 19:33:26
【问题描述】:

在我的应用中,我使用相机拍摄图像。这些作为 NSData 表示形式存储在 NSArray 中。当我将 NSData 转换回图像时,方向现在是横向而不是纵向。

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

有人解释一下吗?谢谢。

【问题讨论】:

    标签: ios uiimage nsdata


    【解决方案1】:

    您应该修复相机捕获的图像的方向,代码如下, 默认情况下,相机图像的方向不正确

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

    【讨论】:

    • 方向正确。不正确的是一些开发者在传输或显示图像时不尊重元数据的逻辑。
    【解决方案2】:

    我认为这是 SDK 的一个错误。我遇到了这个确切的问题,然后切换到 UIImageJPEGRepresentation 解决了这个问题。

    【讨论】:

    • 对此有任何确认吗?我遇到了同样的问题,无法为我的生活找到解决方法。使用 JPEG 表示对我来说是不可能的,因为我需要 alpha 通道 PNG 提供。
    • 这对我有帮助。我已确认 JPEG 转换不会出现图像方向问题
    【解决方案3】:

    -[UIImage imageOrientation] 可能会有所帮助:)

    图像方向会影响图像数据在绘制时的显示方式。默认情况下,图像以“向上”方向显示。但是,如果图像具有关联的元数据(例如 EXIF 信息),则此属性包含该元数据指示的方向。有关此属性的可能值列表,请参阅“UIImageOrientation”。

    由于该属性是只读的,并且取决于您想要做什么,一个可能的(但丑陋的)解决方案可能是:

    UIImage *sourceImage = [arrayImage objectAtIndex:0];
    NSData *data = UIImagePNGRepresentation(sourceImage);
    UIImage *tmp = [UIImage imageWithData:data];
    UIImage *fixed = [UIImage imageWithCGImage:tmp.CGImage
                                         scale:sourceImage.scale
                                   orientation:sourceImage.imageOrientation];
    

    (未经测试,可能/必须有更清洁的东西)

    编辑:第一部分是对您问题的回答,解释不仅仅是一个修复。

    Thisthis (旧的?) 博客文章可能是您感兴趣的阅读材料。奇怪的是,当我使用UIImageJPEGRepresentation 将图像发送到服务器时,我从未遇到过这个问题......你正在使用哪个 iOS 版本?那可能是旧的 SDK 错误?

    【讨论】:

    • 这确实解决了应用程序内部的问题。但是,我在应用程序中以哪种方式定位图像并不重要,当我转换回 NSData 以通过电子邮件发送图像时,图像会从原始图像回到逆时针 90 度。这肯定看起来像一个错误?
    • 编辑的答案带有有趣的链接,下次请发布更详细的问题。
    【解决方案4】:
    dispatch_async([self sessionQueue], ^{
            // Update the orientation on the still image output video connection before capturing.
            [[[self stillImageOutput] connectionWithMediaType:AVMediaTypeVideo] setVideoOrientation:[[(AVCaptureVideoPreviewLayer *)[[self previewView] layer] connection] videoOrientation]];
    
            // Flash set to Auto for Still Capture
            [AVCamViewController setFlashMode:AVCaptureFlashModeAuto forDevice:[[self videoDeviceInput] device]];
    
            // Capture a still image.
            [[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];
                }
            }];
        });
    

    https://developer.apple.com/library/content/samplecode/AVCam/Introduction/Intro.html

    【讨论】:

    • 这解决了我在 iOS 14 上的问题。在 Swift 中如下:dataOutput.connection(with: .video)!.videoOrientation = self.previewLayer.connection!.videoOrientation
    【解决方案5】:
    //iOS Swift 3
    
    func fixRotation(image: UIImage) -> UIImage
        {
            if (image.imageOrientation == .up)
            {
                return image
            }
    
            var transform: CGAffineTransform = .identity
    
            switch image.imageOrientation {
            case .down, .downMirrored:
                transform = transform.translatedBy(x: image.size.width, y: image.size.height)
                transform = transform.rotated(by: .pi)
            case .left, .leftMirrored:
                transform = transform.translatedBy(x: image.size.width, y: 0)
                transform = transform.rotated(by: .pi / 2)
            case .right, .rightMirrored:
                transform = transform.translatedBy(x: 0, y: image.size.height)
                transform = transform.rotated(by: -(.pi / 2))
            case .up, .upMirrored:
                break
            }
    
            switch image.imageOrientation {
            case .upMirrored, .downMirrored:
                transform = transform.translatedBy(x: image.size.width, y: 0)
                transform = transform.scaledBy(x: -1, y: 1)
            case .leftMirrored, .rightMirrored:
                transform = transform.translatedBy(x: image.size.height, y: 0)
                transform = transform.scaledBy(x: -1, y: 1)
            case .up, .down, .left, .right:
                break
            }
    
            // Now we draw the underlying CGImage into a new context, applying the transform
            // calculated above.
    
            let cgimage = image.cgImage
            let bitsPerComponent = cgimage?.bitsPerComponent
            let colorSpace = cgimage?.colorSpace
            let bitmapInfo = cgimage?.bitmapInfo
    
            let ctx = CGContext(data: nil, width: Int(image.size.width), height: Int(image.size.height), bitsPerComponent: bitsPerComponent! , bytesPerRow: 0, space: colorSpace!, bitmapInfo: bitmapInfo!.rawValue)
    
            ctx?.concatenate(transform)
    
            switch image.imageOrientation {
            case .left, .leftMirrored, .right, .rightMirrored:
                // Grr...
                ctx?.draw(image.cgImage!, in: CGRect(x: 0, y: 0, width: image.size.height, height: image.size.width))
            default:
                ctx?.draw(image.cgImage!, in: CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height))
            }
    
            let cgimg: CGImage = ctx!.makeImage()!
            let img = UIImage(cgImage: cgimg)
            return img
        }
    

    【讨论】:

      【解决方案6】:

      这里是一个稍作修改的 Xamarin.iOS (C#) 版本的最佳答案,这样您也不必手动重写它;-)。

      public static UIImage FixCameraImageRotation(UIImage image)
      {
          if (image.Orientation == UIImageOrientation.Up) return image;
      
          var transform = CGAffineTransform.MakeIdentity();
      
          //handle orientation
          switch (image.Orientation)
          {
              case UIImageOrientation.Down:
              case UIImageOrientation.DownMirrored:
                  transform.Rotate((float)Math.PI);
                  transform.Translate(image.Size.Width, image.Size.Height);
                  break;
              case UIImageOrientation.Left:
              case UIImageOrientation.LeftMirrored:
                  transform.Rotate((float)Math.PI / 2);
                  transform.Translate(image.Size.Width, 0);
                  break;
              case UIImageOrientation.Right:
              case UIImageOrientation.RightMirrored:
                  transform.Rotate(-(float)Math.PI / 2);
                  transform.Translate(0, image.Size.Height);
                  break;
          }
      
          //handle mirroring
          switch (image.Orientation)
          {
              case UIImageOrientation.UpMirrored:
              case UIImageOrientation.DownMirrored:
                  transform.Translate(image.Size.Width, 0);
                  transform.Scale(-1, 1);
                  break;
              case UIImageOrientation.LeftMirrored:
              case UIImageOrientation.RightMirrored:
                  transform.Translate(image.Size.Height, 0);
                  transform.Scale(-1, 1);
                  break;
          }
      
          //create context and apply transformation
          using (var context = new CGBitmapContext(
              IntPtr.Zero,
              (nint)image.Size.Width,
              (nint)image.Size.Height,
              image.CGImage.BitsPerComponent,
              image.CGImage.BytesPerRow,
              image.CGImage.ColorSpace,
              image.CGImage.BitmapInfo))
          {
              context.ConcatCTM(transform);
      
              //draw image
              switch (image.Orientation)
              {
                  case UIImageOrientation.Left:
                  case UIImageOrientation.LeftMirrored:
                  case UIImageOrientation.Right:
                  case UIImageOrientation.RightMirrored:
                      context.DrawImage(new CGRect(0, 0, image.Size.Height, image.Size.Width), image.CGImage);
                      break;
                  default:
                      context.DrawImage(new CGRect(0, 0, image.Size.Width, image.Size.Height), image.CGImage);
                      break;
              }
      
              //convert result to UIImage
              using (var cgImage = context.ToImage())
              {
                  var result = UIImage.FromImage(cgImage);
                  return result;
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2011-06-01
        • 2019-04-13
        • 1970-01-01
        • 2015-04-07
        • 2014-05-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多