How to rotate or switch between front and back camera in GPUImage2(Switch version) using swift (toggle with a button). Any help would be highly appreciated.Thank you in advance.
Let's consider the main example of GPUImage2 :
do {
camera = try Camera(sessionPreset:AVCaptureSessionPreset640x480)
filter = SaturationAdjustment()
camera --> filter --> renderView
camera.startCapture()
} catch {
fatalError("Could not initialize rendering pipeline: \(error)")
}
Once your camera started capturing, you can switch from back to front this way :
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)")
}
Side note #1 : The default camera used is the back one, as implied by the default values of Camera.init() :
public init(sessionPreset:AVCaptureSession.Preset,
cameraDevice:AVCaptureDevice? = nil,
location:PhysicalCameraLocation = .backFacing,
...
Side note #2 :
the Camera object presents the location
property. Unfortunately, its didSet
accessor is still in "TODO" :
public var location:PhysicalCameraLocation {
didSet {
// TODO: Swap the camera locations, framebuffers as needed
}
}