3

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?

boboboboboboo
  • 41
  • 1
  • 6
  • 1
    From past experience I know that the camera always captures images and videos in landscape orientation, no matter how you hold the camera. Somewhere in the meta data it stores how the camera was held so you know how you need to rotate the image. – Codo Nov 11 '18 at 11:16
  • 1
    Also note that the image is squeezed into the new dimensions (due to the wrong rotation). If the rotation was correct, the image wouldn't be distorted either. – Codo Nov 11 '18 at 11:22
  • @Codo It works for me. Thank you so much : ) – boboboboboboo Nov 11 '18 at 13:23
  • did u convert image back to cmsamplebuffer? Or what did u do next after get image? – famfamfam Nov 27 '21 at 10:51

0 Answers0