使用CIFilter将CIImage绕中心旋转 [英] rotate CIImage around center with CIFilter

查看:219
本文介绍了使用CIFilter将CIImage绕中心旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试旋转CMSampleBuffer的像素数据(来自相机)并将其显示在UIImageView中。我不想旋转视图本身。它必须是实际的像素数据。

I'm trying to rotate the pixel data of a CMSampleBuffer (coming from the camera) and display it in a UIImageView. I Don't want to rotate the view itself. It has to be the actual pixel data.

我抓取供稿,将其转换为CIImage并使用CIFilter对其进行旋转。
我在设定旋转角度时遇到麻烦。
现在,它绕左下角旋转。我想绕着它的中心旋转它。

I grab the feed, convert it to a CIImage and with a CIFilter I rotate it. I'm having trouble with setting the point around which it has to rotate. Right now it rotates around the lower left point. I want to rotate it around its center.

 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!)

}


推荐答案

找到了它。我必须将转换应用于转换变量本身。

found it. I have to apply transformations to the transformation variable itself.

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)

这篇关于使用CIFilter将CIImage绕中心旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆