4 Commits

2 changed files with 20 additions and 23 deletions

View File

@@ -1,6 +1,7 @@
package com.mrousavy.camera.core package com.mrousavy.camera.core
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.res.Configuration
import android.content.Context import android.content.Context
import android.graphics.Point import android.graphics.Point
import android.util.Log import android.util.Log
@@ -97,22 +98,14 @@ class PreviewView(context: Context, callback: SurfaceHolder.Callback) :
} }
} }
override fun requestLayout() {
super.requestLayout()
// Manually trigger measure & layout, as RN on Android skips those.
// See this issue: https://github.com/facebook/react-native/issues/17968#issuecomment-721958427
post {
measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY))
layout(left, top, right, bottom)
}
}
private fun getSize(contentSize: Size, containerSize: Size, resizeMode: ResizeMode): Size { private fun getSize(contentSize: Size, containerSize: Size, resizeMode: ResizeMode): Size {
var contentSize = contentSize var contentSize = contentSize
// Swap dimensions if orientation is landscape var androidOrientation = context.getResources().getConfiguration().orientation;
if (orientation.isLandscape()) {
if (androidOrientation == Configuration.ORIENTATION_LANDSCAPE) {
contentSize = Size(contentSize.height, contentSize.width) contentSize = Size(contentSize.height, contentSize.width)
} }
val contentAspectRatio = contentSize.width.toDouble() / contentSize.height val contentAspectRatio = contentSize.width.toDouble() / contentSize.height
val containerAspectRatio = containerSize.width.toDouble() / containerSize.height val containerAspectRatio = containerSize.width.toDouble() / containerSize.height
if (!(contentAspectRatio > 0 && containerAspectRatio > 0)) { if (!(contentAspectRatio > 0 && containerAspectRatio > 0)) {

View File

@@ -34,7 +34,7 @@ extension CameraSession {
} }
let enableAudio = self.configuration?.audio != .disabled let enableAudio = self.configuration?.audio != .disabled
// Callback for when new chunks are ready // Callback for when new chunks are ready
let onChunkReady: (ChunkedRecorder.Chunk) -> Void = { chunk in let onChunkReady: (ChunkedRecorder.Chunk) -> Void = { chunk in
guard let delegate = self.delegate else { guard let delegate = self.delegate else {
@@ -191,7 +191,7 @@ extension CameraSession {
} }
} }
} }
func lockCurrentExposure(promise: Promise) { func lockCurrentExposure(promise: Promise) {
CameraQueues.cameraQueue.async { CameraQueues.cameraQueue.async {
withPromise(promise) { withPromise(promise) {
@@ -199,15 +199,19 @@ extension CameraSession {
print("No capture device available") print("No capture device available")
return return
} }
guard captureDevice.isExposureModeSupported(.custom) else {
ReactLogger.log(level: .info, message: "Custom exposure mode not supported")
return
}
do { do {
// Lock the device for configuration // Lock the device for configuration
try captureDevice.lockForConfiguration() try captureDevice.lockForConfiguration()
// Get the current exposure duration and ISO // Get the current exposure duration and ISO
let currentExposureDuration = captureDevice.exposureDuration let currentExposureDuration = captureDevice.exposureDuration
let currentISO = captureDevice.iso let currentISO = captureDevice.iso
// Check if the device supports custom exposure settings // Check if the device supports custom exposure settings
if captureDevice.isExposureModeSupported(.custom) { if captureDevice.isExposureModeSupported(.custom) {
// Lock the current exposure and ISO by setting custom exposure mode // Lock the current exposure and ISO by setting custom exposure mode
@@ -216,19 +220,19 @@ extension CameraSession {
} else { } else {
ReactLogger.log(level: .info, message:"Custom exposure mode not supported") ReactLogger.log(level: .info, message:"Custom exposure mode not supported")
} }
// Unlock the device after configuration // Unlock the device after configuration
captureDevice.unlockForConfiguration() captureDevice.unlockForConfiguration()
} catch { } catch {
ReactLogger.log(level: .warning, message:"Error locking exposure: \(error)") ReactLogger.log(level: .warning, message:"Error locking exposure: \(error)")
} }
return nil return nil
} }
} }
} }
func unlockCurrentExposure(promise: Promise) { func unlockCurrentExposure(promise: Promise) {
CameraQueues.cameraQueue.async { CameraQueues.cameraQueue.async {
withPromise(promise) { withPromise(promise) {
@@ -236,7 +240,7 @@ extension CameraSession {
print("No capture device available") print("No capture device available")
return return
} }
do { do {
if captureDevice.isExposureModeSupported(.autoExpose) { if captureDevice.isExposureModeSupported(.autoExpose) {
try captureDevice.lockForConfiguration() try captureDevice.lockForConfiguration()
@@ -246,7 +250,7 @@ extension CameraSession {
} catch { } catch {
ReactLogger.log(level: .warning, message:"Error unlocking exposure: \(error)") ReactLogger.log(level: .warning, message:"Error unlocking exposure: \(error)")
} }
return nil return nil
} }
} }