如何 AVCaptureSession.sessionPreset 到 720x1280px(或任何东西的 1/1.77 比例)

How to AVCaptureSession.sessionPreset to 720x1280px(or 1/1.77 scale of anything)

我正在使用 swift3,当我使用 AVCaptureSessionPresetMedium 等时无法将分辨率更改为自定义值。它不适合屏幕比例 (1/1.77)。

    let output = AVCaptureVideoDataOutput()

    output.setSampleBufferDelegate(self, queue: sampleQueue)

    let metaOutput = AVCaptureMetadataOutput()
    metaOutput.setMetadataObjectsDelegate(self, queue: faceQueue)

    session.beginConfiguration()

// 所需分辨率:720x1280 像素 // session.sessionPreset = AVCaptureSessionPresetMedium;

    if session.canAddInput(input) {
        session.addInput(input)
    }
    if session.canAddOutput(output) {
        output .alwaysDiscardsLateVideoFrames = true;
        session.addOutput(output)
        connection1 = output.connection(withMediaType: AVMediaTypeVideo)
        connection1?.preferredVideoStabilizationMode = AVCaptureVideoStabilizationMode.auto;
        connection1?.videoOrientation = .portrait
        connection1?.isVideoMirrored = true;
    }
    if session.canAddOutput(metaOutput) {
        output .alwaysDiscardsLateVideoFrames = true;
        session.addOutput(metaOutput)
        connection2 = metaOutput.connection(withMediaType: AVMediaTypeMetadata)
        connection2?.preferredVideoStabilizationMode = AVCaptureVideoStabilizationMode.auto;
        connection2?.videoOrientation = .portrait
        connection2?.isVideoMirrored = true
    }

您应该使用 AVCaptureSessionPreset1280x720 预设。预设以横向表示,但 1280x720 的捕获设置与 720x1280 相同,唯一的区别是方向。例如,对于支持旋转的应用程序:

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!)
{
    guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return }
    cameraImage = CIImage(cvPixelBuffer: pixelBuffer)
    print(cameraImage?.extent ?? "")
}

横向打印 (0.0, 0.0, 1280.0, 720.0),纵向打印 (0.0, 0.0, 720.0, 1280.0)