iOS: Convert CMSampleBuffer to UIImage return image with wrong direction and proportion

Viewed 883

I am trying to process the realtime video form the iPhone camera by using the function in AVCaptureVideoDataOutputSampleBufferDelegate.

The video had been edited but the direction of the video is changed, and the proportion of the video is strange.

I use the following code to edit the video.

 override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    guard let captureDevice = AVCaptureDevice.default(for: .video) else { return }
    guard let input = try? AVCaptureDeviceInput(device: captureDevice) else { return }
    captureSession.addInput(input)
    let dataOutput = AVCaptureVideoDataOutput()
    dataOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "videoQueue"))
    captureSession.addOutput(dataOutput)
    let preview = AVCaptureVideoPreviewLayer(session: captureSession)
    preview.frame = cview.frame
    cview.layer.addSublayer(preview) 
    captureSession.startRunning()

}

func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {

            let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
            let cameraImage = CIImage(cvPixelBuffer: imageBuffer!)
            let comicEffect = CIFilter(name: "CIComicEffect")

            comicEffect!.setValue(cameraImage, forKey: kCIInputImageKey)

            let filteredImage = UIImage(ciImage: comicEffect!.value(forKey: kCIOutputImageKey) as! CIImage!)

            DispatchQueue.main.async {
                self.image.image = filteredImage
            }

}

And it returns the following output:

image output

To make the picture easier to compare, I removed the comicEffect: image without comic effect

The correct proportion should be like:

image with correct proportion

May I know how should I solve this problem?

0 Answers
Related