2021-02-19 12:41:49 -07:00
|
|
|
package com.mrousavy.camera
|
2021-02-19 08:28:14 -07:00
|
|
|
|
|
|
|
import android.annotation.SuppressLint
|
|
|
|
import android.hardware.camera2.*
|
|
|
|
import android.util.Log
|
|
|
|
import androidx.camera.camera2.interop.Camera2CameraInfo
|
|
|
|
import androidx.camera.core.ImageCapture
|
|
|
|
import androidx.camera.core.ImageProxy
|
|
|
|
import androidx.exifinterface.media.ExifInterface
|
|
|
|
import com.facebook.react.bridge.Arguments
|
|
|
|
import com.facebook.react.bridge.ReadableMap
|
|
|
|
import com.facebook.react.bridge.WritableMap
|
2021-02-26 02:56:20 -07:00
|
|
|
import com.mrousavy.camera.utils.*
|
2021-02-19 08:28:14 -07:00
|
|
|
import kotlinx.coroutines.*
|
|
|
|
import java.io.File
|
2021-05-03 11:14:19 -06:00
|
|
|
import kotlin.system.measureTimeMillis
|
2021-02-19 08:28:14 -07:00
|
|
|
|
2021-04-26 04:56:36 -06:00
|
|
|
@SuppressLint("UnsafeOptInUsageError")
|
2021-02-19 08:28:14 -07:00
|
|
|
suspend fun CameraView.takePhoto(options: ReadableMap): WritableMap = coroutineScope {
|
2021-06-27 04:37:54 -06:00
|
|
|
if (fallbackToSnapshot) {
|
|
|
|
Log.i(CameraView.TAG, "takePhoto() called, but falling back to Snapshot because 1 use-case is already occupied.")
|
|
|
|
return@coroutineScope takeSnapshot(options)
|
|
|
|
}
|
|
|
|
|
2021-02-26 02:56:20 -07:00
|
|
|
val startFunc = System.nanoTime()
|
2021-06-27 04:37:54 -06:00
|
|
|
Log.i(CameraView.TAG, "takePhoto() called")
|
2021-06-07 05:08:40 -06:00
|
|
|
if (imageCapture == null) {
|
|
|
|
if (photo == true) {
|
|
|
|
throw CameraNotReadyError()
|
|
|
|
} else {
|
|
|
|
throw PhotoNotEnabledError()
|
|
|
|
}
|
|
|
|
}
|
2021-02-19 08:28:14 -07:00
|
|
|
|
2021-02-26 02:56:20 -07:00
|
|
|
if (options.hasKey("flash")) {
|
|
|
|
val flashMode = options.getString("flash")
|
2021-06-07 05:08:40 -06:00
|
|
|
imageCapture!!.flashMode = when (flashMode) {
|
2021-02-26 02:56:20 -07:00
|
|
|
"on" -> ImageCapture.FLASH_MODE_ON
|
|
|
|
"off" -> ImageCapture.FLASH_MODE_OFF
|
|
|
|
"auto" -> ImageCapture.FLASH_MODE_AUTO
|
|
|
|
else -> throw InvalidTypeScriptUnionError("flash", flashMode ?: "(null)")
|
2021-02-19 08:28:14 -07:00
|
|
|
}
|
2021-02-26 02:56:20 -07:00
|
|
|
}
|
2021-06-21 14:42:46 -06:00
|
|
|
// All those options are not yet implemented - see https://github.com/mrousavy/react-native-vision-camera/issues/75
|
2021-03-17 12:29:03 -06:00
|
|
|
if (options.hasKey("photoCodec")) {
|
|
|
|
// TODO photoCodec
|
|
|
|
}
|
|
|
|
if (options.hasKey("qualityPrioritization")) {
|
|
|
|
// TODO qualityPrioritization
|
|
|
|
}
|
2021-02-26 02:56:20 -07:00
|
|
|
if (options.hasKey("enableAutoRedEyeReduction")) {
|
|
|
|
// TODO enableAutoRedEyeReduction
|
|
|
|
}
|
|
|
|
if (options.hasKey("enableDualCameraFusion")) {
|
|
|
|
// TODO enableDualCameraFusion
|
|
|
|
}
|
|
|
|
if (options.hasKey("enableAutoStabilization")) {
|
|
|
|
// TODO enableAutoStabilization
|
|
|
|
}
|
|
|
|
if (options.hasKey("enableAutoDistortionCorrection")) {
|
|
|
|
// TODO enableAutoDistortionCorrection
|
|
|
|
}
|
|
|
|
val skipMetadata = if (options.hasKey("skipMetadata")) options.getBoolean("skipMetadata") else false
|
2021-02-19 08:28:14 -07:00
|
|
|
|
2021-02-26 02:56:20 -07:00
|
|
|
val camera2Info = Camera2CameraInfo.from(camera!!.cameraInfo)
|
|
|
|
val lensFacing = camera2Info.getCameraCharacteristic(CameraCharacteristics.LENS_FACING)
|
2021-02-19 08:28:14 -07:00
|
|
|
|
2021-02-26 02:56:20 -07:00
|
|
|
val results = awaitAll(
|
|
|
|
async(coroutineContext) {
|
2021-05-03 11:14:19 -06:00
|
|
|
Log.d(CameraView.TAG, "Taking picture...")
|
2021-02-26 02:56:20 -07:00
|
|
|
val startCapture = System.nanoTime()
|
2021-06-07 05:08:40 -06:00
|
|
|
val pic = imageCapture!!.takePicture(takePhotoExecutor)
|
2021-02-26 02:56:20 -07:00
|
|
|
val endCapture = System.nanoTime()
|
2021-05-03 11:14:19 -06:00
|
|
|
Log.i(CameraView.TAG_PERF, "Finished image capture in ${(endCapture - startCapture) / 1_000_000}ms")
|
2021-02-26 02:56:20 -07:00
|
|
|
pic
|
|
|
|
},
|
|
|
|
async(Dispatchers.IO) {
|
2021-05-03 11:14:19 -06:00
|
|
|
Log.d(CameraView.TAG, "Creating temp file...")
|
2021-02-26 02:56:20 -07:00
|
|
|
File.createTempFile("mrousavy", ".jpg", context.cacheDir).apply { deleteOnExit() }
|
2021-02-19 08:28:14 -07:00
|
|
|
}
|
2021-02-26 02:56:20 -07:00
|
|
|
)
|
|
|
|
val photo = results.first { it is ImageProxy } as ImageProxy
|
|
|
|
val file = results.first { it is File } as File
|
|
|
|
|
|
|
|
val exif: ExifInterface?
|
|
|
|
@Suppress("BlockingMethodInNonBlockingContext")
|
|
|
|
withContext(Dispatchers.IO) {
|
2021-05-03 11:14:19 -06:00
|
|
|
Log.d(CameraView.TAG, "Saving picture to ${file.absolutePath}...")
|
|
|
|
val milliseconds = measureTimeMillis {
|
|
|
|
val flipHorizontally = lensFacing == CameraCharacteristics.LENS_FACING_FRONT
|
|
|
|
photo.save(file, flipHorizontally)
|
|
|
|
}
|
|
|
|
Log.i(CameraView.TAG_PERF, "Finished image saving in ${milliseconds}ms")
|
2021-02-26 02:56:20 -07:00
|
|
|
// TODO: Read Exif from existing in-memory photo buffer instead of file?
|
|
|
|
exif = if (skipMetadata) null else ExifInterface(file)
|
|
|
|
}
|
2021-02-19 08:28:14 -07:00
|
|
|
|
2021-02-26 02:56:20 -07:00
|
|
|
val map = Arguments.createMap()
|
|
|
|
map.putString("path", file.absolutePath)
|
|
|
|
map.putInt("width", photo.width)
|
|
|
|
map.putInt("height", photo.height)
|
|
|
|
map.putBoolean("isRawPhoto", photo.isRaw)
|
2021-02-19 08:28:14 -07:00
|
|
|
|
2021-02-26 02:56:20 -07:00
|
|
|
val metadata = exif?.buildMetadataMap()
|
|
|
|
map.putMap("metadata", metadata)
|
2021-02-19 08:28:14 -07:00
|
|
|
|
2021-02-26 02:56:20 -07:00
|
|
|
photo.close()
|
2021-02-19 08:28:14 -07:00
|
|
|
|
2021-05-03 11:14:19 -06:00
|
|
|
Log.d(CameraView.TAG, "Finished taking photo!")
|
2021-02-19 08:28:14 -07:00
|
|
|
|
2021-02-26 02:56:20 -07:00
|
|
|
val endFunc = System.nanoTime()
|
2021-05-03 11:14:19 -06:00
|
|
|
Log.i(CameraView.TAG_PERF, "Finished function execution in ${(endFunc - startFunc) / 1_000_000}ms")
|
2021-02-26 02:56:20 -07:00
|
|
|
return@coroutineScope map
|
2021-02-19 08:28:14 -07:00
|
|
|
}
|