Version 1
This commit is contained in:
@ -17,7 +17,7 @@ protocol CameraControllerDelegate {
|
||||
|
||||
class CameraController: UIViewController {
|
||||
|
||||
private let imageSize = 299 // New for XCode models, 227 for turicreate
|
||||
private let imageSize = 299 // New for XCode models, 227/229 for turicreate
|
||||
|
||||
// MARK: Outlets
|
||||
|
||||
@ -34,17 +34,30 @@ class CameraController: UIViewController {
|
||||
}
|
||||
|
||||
var delegate: CameraControllerDelegate?
|
||||
|
||||
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
|
||||
return .portrait
|
||||
}
|
||||
|
||||
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
|
||||
return .portrait
|
||||
}
|
||||
|
||||
override var shouldAutorotate: Bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// MARK: Actions
|
||||
|
||||
@IBAction func backButtonPressed() {
|
||||
self.giveFeedback(.medium)
|
||||
delegate?.didCancel()
|
||||
self.dismiss(animated: true)
|
||||
}
|
||||
|
||||
@IBAction func imageButtonPressed() {
|
||||
self.giveFeedback(.medium)
|
||||
imageButton.isEnabled = false
|
||||
event("Taking image")
|
||||
cameraView.capture()
|
||||
}
|
||||
|
||||
@ -83,6 +96,11 @@ class CameraController: UIViewController {
|
||||
cancelButton.borderColor = tint
|
||||
cancelButton.set(template: "cancel", with: tint)
|
||||
}
|
||||
|
||||
private func giveFeedback(_ style: UIImpactFeedbackGenerator.FeedbackStyle) {
|
||||
let generator = UIImpactFeedbackGenerator(style: style)
|
||||
generator.impactOccurred()
|
||||
}
|
||||
|
||||
// MARK: Alerts
|
||||
|
||||
@ -105,6 +123,10 @@ class CameraController: UIViewController {
|
||||
alert.addAction(settingsAction)
|
||||
self.present(alert, animated: true, completion: nil)
|
||||
}
|
||||
|
||||
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
|
||||
cameraView.didReceiveTouch(touches.first!)
|
||||
}
|
||||
}
|
||||
|
||||
extension CameraController: PhotoCaptureHandlerDelegate {
|
||||
|
@ -51,6 +51,8 @@ class CameraView: UIView {
|
||||
var videoDeviceInput: AVCaptureDeviceInput!
|
||||
|
||||
private let photoOutput = AVCapturePhotoOutput()
|
||||
|
||||
private var cameraDevice: AVCaptureDevice?
|
||||
|
||||
private let photoCaptureProcessor = PhotoCaptureHandler()
|
||||
|
||||
@ -146,52 +148,80 @@ class CameraView: UIView {
|
||||
session.sessionPreset = .photo
|
||||
|
||||
// Add video input.
|
||||
|
||||
guard let backCameraDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back) else {
|
||||
error("No camera on device")
|
||||
setupResult = .configurationFailed
|
||||
session.commitConfiguration()
|
||||
return
|
||||
}
|
||||
|
||||
let videoDeviceInput: AVCaptureDeviceInput
|
||||
do {
|
||||
guard let backCameraDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back) else {
|
||||
print("No camera on device")
|
||||
setupResult = .configurationFailed
|
||||
session.commitConfiguration()
|
||||
return
|
||||
}
|
||||
|
||||
let videoDeviceInput = try AVCaptureDeviceInput(device: backCameraDevice)
|
||||
|
||||
if session.canAddInput(videoDeviceInput) {
|
||||
session.addInput(videoDeviceInput)
|
||||
self.videoDeviceInput = videoDeviceInput
|
||||
|
||||
DispatchQueue.main.async {
|
||||
self.videoPreviewLayer.connection?.videoOrientation = .portrait
|
||||
}
|
||||
} else {
|
||||
print("Could not add video device input to the session")
|
||||
setupResult = .configurationFailed
|
||||
session.commitConfiguration()
|
||||
return
|
||||
}
|
||||
videoDeviceInput = try AVCaptureDeviceInput(device: backCameraDevice)
|
||||
} catch {
|
||||
print("Could not create video device input: \(error)")
|
||||
self.error("Could not create video device input: \(error)")
|
||||
setupResult = .configurationFailed
|
||||
session.commitConfiguration()
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
guard session.canAddInput(videoDeviceInput) else {
|
||||
error("Could not add video device input to the session")
|
||||
setupResult = .configurationFailed
|
||||
session.commitConfiguration()
|
||||
return
|
||||
}
|
||||
|
||||
session.addInput(videoDeviceInput)
|
||||
self.videoDeviceInput = videoDeviceInput
|
||||
self.cameraDevice = backCameraDevice
|
||||
DispatchQueue.main.async {
|
||||
self.videoPreviewLayer.connection?.videoOrientation = .portrait
|
||||
}
|
||||
|
||||
// Add photo output.
|
||||
if session.canAddOutput(photoOutput) {
|
||||
session.addOutput(photoOutput)
|
||||
|
||||
photoOutput.isHighResolutionCaptureEnabled = true
|
||||
photoOutput.isDepthDataDeliveryEnabled = false
|
||||
photoOutput.isDualCameraDualPhotoDeliveryEnabled = false
|
||||
photoOutput.isLivePhotoCaptureEnabled = false
|
||||
} else {
|
||||
print("Could not add photo output to the session")
|
||||
guard session.canAddOutput(photoOutput) else {
|
||||
error("Could not add photo output to the session")
|
||||
setupResult = .configurationFailed
|
||||
session.commitConfiguration()
|
||||
return
|
||||
}
|
||||
|
||||
session.addOutput(photoOutput)
|
||||
|
||||
photoOutput.isHighResolutionCaptureEnabled = true
|
||||
photoOutput.isDepthDataDeliveryEnabled = false
|
||||
photoOutput.isDualCameraDualPhotoDeliveryEnabled = false
|
||||
photoOutput.isLivePhotoCaptureEnabled = false
|
||||
session.commitConfiguration()
|
||||
}
|
||||
|
||||
func didReceiveTouch(_ touch: UITouch) {
|
||||
let screenSize = bounds.size
|
||||
let location = touch.location(in: self)
|
||||
let focusPoint = CGPoint(x: location.y / screenSize.height, y: 1.0 - location.x / screenSize.width)
|
||||
event("Focusing on point (\(focusPoint.x),\(focusPoint.y))")
|
||||
if let device = cameraDevice {
|
||||
do {
|
||||
try device.lockForConfiguration()
|
||||
|
||||
if device.isFocusPointOfInterestSupported {
|
||||
device.focusPointOfInterest = focusPoint
|
||||
device.focusMode = .autoFocus
|
||||
}
|
||||
// if device.isExposurePointOfInterestSupported {
|
||||
// device.exposurePointOfInterest = focusPoint
|
||||
// device.exposureMode = .autoExpose
|
||||
// }
|
||||
device.unlockForConfiguration()
|
||||
} catch {
|
||||
self.error("Could not lock device for configuration: \(error)")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension CameraView: Logger {
|
||||
|
||||
static let logToken = "CameraView"
|
||||
}
|
||||
|
Reference in New Issue
Block a user