Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVCaptureVideoPreviewLayer video frame size

How can I get image size inside AVCaptureVideoPreviewLayer:

self.cameraPreviewLayer.frame = self.cameraView.frame; // (0.0, 0.0, 320.0, 568.0)

Image inside AVCaptureVideoPreviewLayer smaller than frame.

like image 532
Artem Krachulov Avatar asked Apr 30 '15 07:04

Artem Krachulov


2 Answers

You can't get the actual rendered size inside the AVCaptureVideoPreviewLayer frame. You'll have to calculate it.

Here's how you get the actual video dimensions:

AVCaptureDeviceInput *videoDeviceInput = // initialised already in your app

// Here you can get the video dimensions:
CMVideoDimensions dimensions = CMVideoFormatDescriptionGetDimensions(videoDeviceInput.device.activeFormat.formatDescription);

From here you can calc the aspect fitting rectangle inside the AVCaptureVideoPreviewLayer frame.

like image 68
Leslie Godwin Avatar answered Oct 20 '22 13:10

Leslie Godwin


Swift Code

var captureSession : AVCaptureSession?

var captureInput : AVCaptureDeviceInput?{
    get{
        return self.captureSession?.inputs.first as? AVCaptureDeviceInput
    }
}

func doSomething(){
    guard let captureInput = captureInput else{ return }

    let dims : CMVideoDimensions = CMVideoFormatDescriptionGetDimensions(captureInput.device.activeFormat.formatDescription)
        //do whatever
}
like image 35
Benny Davidovitz Avatar answered Oct 20 '22 12:10

Benny Davidovitz