我正在尝试旋转CMSampleBuffer(来自相机)的像素数据并将其显示在UIImageView中。我不想旋转视图本身。它必须是实际的像素数据。
我抓住了Feed,将其转换为CIImage并使用CIFilter旋转它。 我在设置它必须旋转的点时遇到了麻烦。 现在它围绕左下角旋转。我想围绕它的中心旋转它。
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) {
//create pixelbuffer
let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)!
let context = CIContext.init(options: nil)
// calculating the radians to rotate the ciimage by
let radians : Float = atan2f(Float(boxView!.transform.b), Float(boxView!.transform.a));
//create ciimage from the pixelbuffer
let options = [kCVPixelBufferCGImageCompatibilityKey as String: true,
kCVPixelBufferCGBitmapContextCompatibilityKey as String: true,]
let ci = CIImage.init(cvPixelBuffer: pixelBuffer, options: options)
//transform filter
let filter = CIFilter.init(name: "CIAffineTransform")
//set translation point to center
var transform = CGAffineTransform(translationX: self.view.frame.midX, y: self.view.frame.midY)
//rotate
transform = CGAffineTransform(rotationAngle:CGFloat(radians))
// set translation point back again to normal
var transformBack = CGAffineTransform(translationX: -self.view.frame.midX, y: -self.view.frame.midY)
//applying the transform
transform.concatenating(transformBack)
filter!.setValue(transform, forKey: kCIInputTransformKey)
filter!.setValue(ci, forKey: kCIInputImageKey)
let output = filter?.outputImage
let img = context.createCGImage(output!, from: imageView!.frame)
// send image to be displayed
self.imageView!.image = UIImage(cgImage: img!)
}
答案 0 :(得分:6)
let transform = CGAffineTransform(translationX: self.view.frame.midX, y: self.view.frame.midY).rotate(angle: CGFloat(radians)).translatedBy(x: -self.view.frame.midX, y: -self.view.frame.midY)
答案 1 :(得分:0)
快捷键5:
let image = CIImage(cvPixelBuffer: yourBuffer)
let rotatedImage = image.oriented(forExifOrientation: Int32(CGImagePropertyOrientation.right.rawValue))
对此有一个明确的API:Docs