【问题标题】:Fix orientation of images taken with front camera ios修复使用前置摄像头 ios 拍摄的图像的方向
【发布时间】:2021-03-14 12:52:01
【问题描述】:

我正在使用 Swift 在我的应用程序中使用自定义相机实现。 当图像被捕获时,称为func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?)。我使用photo.fileDataRepresentation() 获取图像数据,然后使用UIImage 上的以下扩展名来修复照片的方向。

func fixedOrientation() -> UIImage? {
    guard imageOrientation != UIImage.Orientation.up else {
        // This is default orientation, don't need to do anything
        return self.copy() as? UIImage
    }
    
    guard let cgImage = self.cgImage else {
        // CGImage is not available
        return nil
    }
    
    guard let colorSpace = cgImage.colorSpace, let ctx = CGContext(data: nil, width: Int(size.width), height: Int(size.height), bitsPerComponent: cgImage.bitsPerComponent, bytesPerRow: 0, space: colorSpace, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) else {
        return nil // Not able to create CGContext
    }
    
    var transform = CGAffineTransform.identity
    
    switch imageOrientation {
    case .down, .downMirrored:
        transform = transform.translatedBy(x: size.width, y: size.height)
        transform = transform.rotated(by: CGFloat.pi)
    case .left, .leftMirrored:
        transform = transform.translatedBy(x: size.width, y: 0)
        transform = transform.rotated(by: CGFloat.pi / 2.0)
    case .right, .rightMirrored:
        transform = transform.translatedBy(x: 0, y: size.height)
        transform = transform.rotated(by: CGFloat.pi / -2.0)
    case .up, .upMirrored:
        break
    @unknown default:
        break
    }
    
    // Flip image one more time if needed to, this is to prevent flipped image
    switch imageOrientation {
    case .upMirrored, .downMirrored:
        transform = transform.translatedBy(x: size.width, y: 0)
        transform = transform.scaledBy(x: -1, y: 1)
    case .leftMirrored, .rightMirrored:
        transform = transform.translatedBy(x: size.height, y: 0)
        transform = transform.scaledBy(x: -1, y: 1)
    case .up, .down, .left, .right:
        break
    @unknown default:
        break
    }
    
    ctx.concatenate(transform)
    
    switch imageOrientation {
    case .left, .leftMirrored, .right, .rightMirrored:
        ctx.draw(cgImage, in: CGRect(x: 0, y: 0, width: size.height, height: size.width))
    default:
        ctx.draw(cgImage, in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
    }
    
    guard let newCGImage = ctx.makeImage() else { return nil }
    return UIImage(cgImage: newCGImage, scale: 1, orientation: .up)
}

显然,这对于使用后置摄像头拍摄的图像效果很好,但使用正面摄像头拍摄时我遇到了问题。

  1. 如果自拍照片是纵向拍摄的,该方法返回镜像的照片。(这没什么大不了的)
  2. 如果自拍照片是在右/左横向拍摄的,则输出代码也是镜像但错误旋转的照片。这是我需要您帮助的地方,以便正确旋转照片。

注意:旋转设备时,我还将 videoOrientationAVCaptureConnection 更改。

【问题讨论】:

    标签: ios swift camera front-camera


    【解决方案1】:

    原来我正在更改videoOrientation,而旋转确实发生了变化,这是错误的。在拍摄照片之前,我必须改变这个逻辑。现在它工作正常。我还修复了镜像问题,如果使用前置摄像头,将 isVideoMirrored 设置为 true。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-06
      • 2016-07-19
      相关资源
      最近更新 更多