GPUImage2 , 前后摄像头切换

GPUImage2 , Switching between Front and back Camera

如何使用 swift(使用按钮切换)在 GPUImage2(切换版本)中旋转或切换前后摄像头。任何帮助都将是高度appreciated.Thank你提前。

我找到了答案。

我再次重新加载我的视图控制器,然后通过布尔值检查它并相应地初始化相机,即再次渲染视图。

让我们考虑 GPUImage2 的主要示例:

do {
    camera = try Camera(sessionPreset:AVCaptureSessionPreset640x480)
    filter = SaturationAdjustment()
    camera --> filter --> renderView
    camera.startCapture()
} catch {
    fatalError("Could not initialize rendering pipeline: \(error)")
}

一旦你的相机开始拍摄,你可以通过这种方式从后到前切换:

do {

    // Stop the capture of the current camera and remove its targets
    sharedImageProcessingContext.runOperationSynchronously{
        camera.stopCapture()
        camera.removeAllTargets()
    }

    // Build a new Camera object, using the back or front physical camera
    if camera.location == .frontFacing {
        camera = try Camera(sessionPreset:.vga640x480, location:.backFacing)
    } else {
        camear = try Camera(sessionPreset:.vga640x480, location:.frontFacing)
    }

    //Rebuild the pipeline
    camera --> filter --> renderView
    camera.startCapture()

} catch {
    print("Error switching camera : \(error)")
}

旁注 #1: 使用的默认相机是后置相机,如 Camera.init() 的默认值所暗示:

public init(sessionPreset:AVCaptureSession.Preset, 
             cameraDevice:AVCaptureDevice? = nil, 
                 location:PhysicalCameraLocation = .backFacing, 
...

旁注 #2: Camera 对象呈现 location 属性。不幸的是,它的 didSet 访问器仍在 "TODO" :

public var location:PhysicalCameraLocation {
    didSet {
        // TODO: Swap the camera locations, framebuffers as needed
    }
}