fix: Fix savePhotoToFile() sometimes writing empty files (#1746)

This commit is contained in:
Vojtech Novak 2023-09-01 19:41:42 +02:00 committed by GitHub
parent cebb7b3e89
commit fc0e2bf34b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 10 deletions

View File

@ -1,5 +1,6 @@
[*.{kt,kts}]
indent_size=2
indent_style=space
insert_final_newline=true
max_line_length=off
disabled_rules=no-wildcard-imports

View File

@ -64,15 +64,22 @@ suspend fun CameraView.takePhoto(optionsMap: ReadableMap): WritableMap {
}
}
private fun writeImageToStream(imageBytes: ByteArray, stream: OutputStream, isMirrored: Boolean) {
if (isMirrored) {
private fun writePhotoToFile(photo: CameraSession.CapturedPhoto, file: File) {
val byteBuffer = photo.image.planes[0].buffer
if (photo.isMirrored) {
val imageBytes = ByteArray(byteBuffer.remaining()).apply { byteBuffer.get(this) }
val bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size)
val matrix = Matrix()
matrix.preScale(-1f, 1f)
val processedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, false)
processedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream)
val processedBitmap =
Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, false)
FileOutputStream(file).use { stream ->
processedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream)
}
} else {
stream.write(imageBytes)
val channel = FileOutputStream(file).channel
channel.write(byteBuffer)
channel.close()
}
}
@ -83,12 +90,8 @@ private suspend fun savePhotoToFile(context: Context,
when (photo.format) {
// When the format is JPEG or DEPTH JPEG we can simply save the bytes as-is
ImageFormat.JPEG, ImageFormat.DEPTH_JPEG -> {
val buffer = photo.image.planes[0].buffer
val bytes = ByteArray(buffer.remaining()).apply { buffer.get(this) }
val file = createFile(context, ".jpg")
FileOutputStream(file).use { stream ->
writeImageToStream(bytes, stream, photo.isMirrored)
}
writePhotoToFile(photo, file)
return@withContext file.absolutePath
}