feat: High quality mode (enableHighQualityPhotos) (#194)

* feat: High Quality photo capture

* prepare photo output for re-used settings

* use high quality captures

* Remove `enableVirtualDeviceFusion` as that is enabled by default

* Clean up configuration, remove default

* format

* Update CameraViewManager.kt

* rename

* Update CameraProps.ts

* Fix overriding `photoSettings`

* Update CameraView+TakePhoto.swift

* Update CameraView+TakePhoto.swift
This commit is contained in:
Marc Rousavy
2021-06-10 13:49:34 +02:00
committed by GitHub
parent 88a30e5723
commit 0e606affce
9 changed files with 68 additions and 48 deletions

View File

@@ -94,12 +94,21 @@ extension CameraView {
if photo?.boolValue == true {
ReactLogger.log(level: .info, message: "Adding Photo output...")
photoOutput = AVCapturePhotoOutput()
photoOutput!.isDepthDataDeliveryEnabled = photoOutput!.isDepthDataDeliverySupported && enableDepthData
if let enableHighResolutionCapture = self.enableHighResolutionCapture?.boolValue {
photoOutput!.isHighResolutionCaptureEnabled = enableHighResolutionCapture
if enableHighQualityPhotos?.boolValue == true {
photoOutput!.isHighResolutionCaptureEnabled = true
if #available(iOS 13.0, *) {
photoOutput!.isVirtualDeviceConstituentPhotoDeliveryEnabled = photoOutput!.isVirtualDeviceConstituentPhotoDeliverySupported
photoOutput!.maxPhotoQualityPrioritization = .quality
} else {
photoOutput!.isDualCameraDualPhotoDeliveryEnabled = photoOutput!.isDualCameraDualPhotoDeliverySupported
}
}
if #available(iOS 12.0, *) {
photoOutput!.isPortraitEffectsMatteDeliveryEnabled = photoOutput!.isPortraitEffectsMatteDeliverySupported && self.enablePortraitEffectsMatteDelivery
if enableDepthData {
photoOutput!.isDepthDataDeliveryEnabled = photoOutput!.isDepthDataDeliverySupported
}
if #available(iOS 12.0, *), enablePortraitEffectsMatteDelivery {
photoOutput!.isPortraitEffectsMatteDeliveryEnabled = photoOutput!.isPortraitEffectsMatteDeliverySupported
}
guard captureSession.canAddOutput(photoOutput!) else {
invokeOnError(.parameter(.unsupportedOutput(outputDescriptor: "photo-output")))

View File

@@ -36,19 +36,35 @@ extension CameraView {
}
}
var photoSettings = AVCapturePhotoSettings()
ReactLogger.log(level: .info, message: "Capturing photo...")
var format: [String: Any]?
// photo codec
if let photoCodecString = options["photoCodec"] as? String {
guard let photoCodec = AVVideoCodecType(withString: photoCodecString) else {
promise.reject(error: .capture(.invalidPhotoCodec))
return
}
if photoOutput.availablePhotoCodecTypes.contains(photoCodec) {
photoSettings = AVCapturePhotoSettings(format: [AVVideoCodecKey: photoCodec])
} else {
promise.reject(error: .parameter(.invalid(unionName: "PhotoCodec", receivedValue: photoCodecString)))
return
}
if photoOutput.availablePhotoCodecTypes.contains(photoCodec) {
format = [AVVideoCodecKey: photoCodec]
} else {
promise.reject(error: .capture(.invalidPhotoCodec))
return
}
}
// Create photo settings
let photoSettings = AVCapturePhotoSettings(format: format)
// default, overridable settings if high quality capture was enabled
if self.enableHighQualityPhotos?.boolValue == true {
photoSettings.isHighResolutionPhotoEnabled = true
if #available(iOS 13.0, *) {
photoSettings.photoQualityPrioritization = .quality
}
}
// flash
if videoDeviceInput.device.isFlashAvailable, let flash = options["flash"] as? String {
guard let flashMode = AVCaptureDevice.FlashMode(withString: flash) else {
promise.reject(error: .parameter(.invalid(unionName: "FlashMode", receivedValue: flash)))
@@ -56,16 +72,14 @@ extension CameraView {
}
photoSettings.flashMode = flashMode
}
photoSettings.isHighResolutionPhotoEnabled = photoOutput.isHighResolutionCaptureEnabled
if !photoSettings.__availablePreviewPhotoPixelFormatTypes.isEmpty {
photoSettings.previewPhotoFormat = [kCVPixelBufferPixelFormatTypeKey as String: photoSettings.__availablePreviewPhotoPixelFormatTypes.first!]
}
// depth data
photoSettings.isDepthDataDeliveryEnabled = photoOutput.isDepthDataDeliveryEnabled
photoSettings.embedsDepthDataInPhoto = photoSettings.isDepthDataDeliveryEnabled
if #available(iOS 12.0, *) {
photoSettings.isPortraitEffectsMatteDeliveryEnabled = photoOutput.isPortraitEffectsMatteDeliveryEnabled
photoSettings.embedsPortraitEffectsMatteInPhoto = photoSettings.isPortraitEffectsMatteDeliveryEnabled
}
// quality prioritization
if #available(iOS 13.0, *), let qualityPrioritization = options["qualityPrioritization"] as? String {
guard let photoQualityPrioritization = AVCapturePhotoOutput.QualityPrioritization(withString: qualityPrioritization) else {
promise.reject(error: .parameter(.invalid(unionName: "QualityPrioritization", receivedValue: qualityPrioritization)))
@@ -73,24 +87,26 @@ extension CameraView {
}
photoSettings.photoQualityPrioritization = photoQualityPrioritization
}
// red-eye reduction
if #available(iOS 12.0, *), let autoRedEyeReduction = options["enableAutoRedEyeReduction"] as? Bool {
photoSettings.isAutoRedEyeReductionEnabled = autoRedEyeReduction
}
if let enableVirtualDeviceFusion = options["enableVirtualDeviceFusion"] as? Bool {
if #available(iOS 13.0, *) {
photoSettings.isAutoVirtualDeviceFusionEnabled = enableVirtualDeviceFusion
} else {
photoSettings.isAutoDualCameraFusionEnabled = enableVirtualDeviceFusion
}
}
// stabilization
if let enableAutoStabilization = options["enableAutoStabilization"] as? Bool {
photoSettings.isAutoStillImageStabilizationEnabled = enableAutoStabilization
}
// distortion correction
if #available(iOS 14.1, *), let enableAutoDistortionCorrection = options["enableAutoDistortionCorrection"] as? Bool {
photoSettings.isAutoContentAwareDistortionCorrectionEnabled = enableAutoDistortionCorrection
}
photoOutput.capturePhoto(with: photoSettings, delegate: PhotoCaptureDelegate(promise: promise))
// Assume that `takePhoto` is always called with the same parameters, so prepare the next call too.
photoOutput.setPreparedPhotoSettingsArray([photoSettings], completionHandler: nil)
}
}
}

View File

@@ -21,7 +21,7 @@ import UIKit
private let propsThatRequireReconfiguration = ["cameraId",
"enableDepthData",
"enableHighResolutionCapture",
"enableHighQualityPhotos",
"enablePortraitEffectsMatteDelivery",
"preset",
"photo",
@@ -40,7 +40,7 @@ public final class CameraView: UIView {
// props that require reconfiguring
@objc var cameraId: NSString?
@objc var enableDepthData = false
@objc var enableHighResolutionCapture: NSNumber? // nullable bool
@objc var enableHighQualityPhotos: NSNumber? // nullable bool
@objc var enablePortraitEffectsMatteDelivery = false
@objc var preset: String?
// use cases

View File

@@ -25,7 +25,7 @@ RCT_EXTERN_METHOD(getAvailableCameraDevices:(RCTPromiseResolveBlock)resolve reje
RCT_EXPORT_VIEW_PROPERTY(isActive, BOOL);
RCT_EXPORT_VIEW_PROPERTY(cameraId, NSString);
RCT_EXPORT_VIEW_PROPERTY(enableDepthData, BOOL);
RCT_EXPORT_VIEW_PROPERTY(enableHighResolutionCapture, NSNumber); // nullable bool
RCT_EXPORT_VIEW_PROPERTY(enableHighQualityPhotos, NSNumber); // nullable bool
RCT_EXPORT_VIEW_PROPERTY(enablePortraitEffectsMatteDelivery, BOOL);
// use cases
RCT_EXPORT_VIEW_PROPERTY(photo, NSNumber); // nullable bool