Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

working prototype of orientation normalisation #3222

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,70 @@ import com.mrousavy.camera.core.types.Flash
import com.mrousavy.camera.core.types.Orientation
import com.mrousavy.camera.core.types.TakePhotoOptions
import com.mrousavy.camera.core.utils.FileUtils
import android.graphics.BitmapFactory
import android.graphics.Matrix
import android.media.ExifInterface
import android.graphics.Bitmap
import java.io.File
import java.io.FileOutputStream
import java.io.IOException

fun rotateImageAndSave(imagePath: String): Boolean {
// Decode the image into a Bitmap
val bitmap = BitmapFactory.decodeFile(imagePath) ?: return false

try {
// Read the Exif data for orientation
val exif = ExifInterface(imagePath)
val orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL)

// Create a Matrix object to rotate the Bitmap
val matrix = Matrix()
when (orientation) {
ExifInterface.ORIENTATION_ROTATE_90 -> matrix.postRotate(90f)
ExifInterface.ORIENTATION_ROTATE_180 -> matrix.postRotate(180f)
ExifInterface.ORIENTATION_ROTATE_270 -> matrix.postRotate(270f)
ExifInterface.ORIENTATION_FLIP_HORIZONTAL -> matrix.postScale(-1f, 1f)
ExifInterface.ORIENTATION_FLIP_VERTICAL -> matrix.postScale(1f, -1f)
ExifInterface.ORIENTATION_TRANSPOSE -> {
matrix.postRotate(90f)
matrix.postScale(-1f, 1f)
}
ExifInterface.ORIENTATION_TRANSVERSE -> {
matrix.postRotate(-90f)
matrix.postScale(-1f, 1f)
}
}

// If there's no rotation or flipping needed, return the original bitmap
val correctedBitmap = if (!matrix.isIdentity) {
Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)
} else {
bitmap
}

// Save the rotated bitmap back to the file path
val file = File(imagePath)
val outputStream = FileOutputStream(file)
correctedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream)
outputStream.flush()
outputStream.close()

// Update EXIF metadata to reset the orientation tag to "normal"
val newExif = ExifInterface(imagePath)
newExif.setAttribute(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL.toString())
newExif.saveAttributes()

// Recycle the bitmap to free up memory
correctedBitmap.recycle()

return true
} catch (e: IOException) {
e.printStackTrace()
}

return false
}

suspend fun CameraSession.takePhoto(options: TakePhotoOptions): Photo {
val camera = camera ?: throw CameraNotReadyError()
Expand Down Expand Up @@ -33,6 +97,10 @@ suspend fun CameraSession.takePhoto(options: TakePhotoOptions): Photo {
CameraQueues.cameraExecutor
)

if (options.normalizeOrientation) {
rotateImageAndSave(photoFile.uri.path)
}

// Parse resulting photo (EXIF data)
val size = FileUtils.getImageSize(photoFile.uri.path)
val rotation = photoOutput.targetRotation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ import com.facebook.react.bridge.ReadableMap
import com.mrousavy.camera.core.utils.FileUtils
import com.mrousavy.camera.core.utils.OutputFile

data class TakePhotoOptions(val file: OutputFile, val flash: Flash, val enableShutterSound: Boolean) {

data class TakePhotoOptions(val file: OutputFile, val flash: Flash, val enableShutterSound: Boolean, val normalizeOrientation: Boolean) {
companion object {
fun fromJS(context: Context, map: ReadableMap): TakePhotoOptions {
val flash = if (map.hasKey("flash")) Flash.fromUnionValue(map.getString("flash")) else Flash.OFF
val enableShutterSound = if (map.hasKey("enableShutterSound")) map.getBoolean("enableShutterSound") else false
val directory = if (map.hasKey("path")) FileUtils.getDirectory(map.getString("path")) else context.cacheDir

val normalize = if (map.hasKey("normalizeOrientation")) map.getBoolean("normalizeOrientation") else false
val outputFile = OutputFile(context, directory, ".jpg")
return TakePhotoOptions(outputFile, flash, enableShutterSound)
return TakePhotoOptions(outputFile, flash, enableShutterSound, normalize)
}
}
}
1 change: 1 addition & 0 deletions package/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@shopify/react-native-skia": "^1.3.4",
"react": "^18.3.1",
"react-native": "^0.74.2",
"react-native-compressor": "^1.9.0",
"react-native-gesture-handler": "^2.16.2",
"react-native-mmkv": "^2.12.2",
"react-native-pressable-opacity": "^1.0.10",
Expand Down
8 changes: 3 additions & 5 deletions package/example/src/CameraPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,9 @@ export function CameraPage({ navigation }: Props): React.ReactElement {

const screenAspectRatio = SCREEN_HEIGHT / SCREEN_WIDTH
const format = useCameraFormat(device, [
{ fps: targetFps },
{ videoAspectRatio: screenAspectRatio },
{ videoResolution: 'max' },
{ photoAspectRatio: screenAspectRatio },
{ photoResolution: 'max' },

{ photoResolution: { width: 1440, height: 1440 } },

])

const fps = Math.min(format?.maxFps ?? 1, targetFps)
Expand Down
2 changes: 1 addition & 1 deletion package/example/src/MediaPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export function MediaPage({ navigation, route }: Props): React.ReactElement {
return (
<View style={[styles.container, screenStyle]}>
{type === 'photo' && (
<Image source={source} style={StyleSheet.absoluteFill} resizeMode="cover" onLoadEnd={onMediaLoadEnd} onLoad={onMediaLoad} />
<Image source={source} style={StyleSheet.absoluteFill} resizeMode="contain" onLoadEnd={onMediaLoadEnd} onLoad={onMediaLoad} />
)}
{type === 'video' && (
<Video
Expand Down
10 changes: 9 additions & 1 deletion package/example/src/views/CaptureButton.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useCallback, useRef } from 'react'
import type { ViewProps } from 'react-native'
import { StyleSheet, View } from 'react-native'
import { Alert, StyleSheet, View } from 'react-native'
import type { PanGestureHandlerGestureEvent, TapGestureHandlerStateChangeEvent } from 'react-native-gesture-handler'
import { PanGestureHandler, State, TapGestureHandler } from 'react-native-gesture-handler'
import Reanimated, {
Expand All @@ -17,6 +17,7 @@ import Reanimated, {
} from 'react-native-reanimated'
import type { Camera, PhotoFile, VideoFile } from 'react-native-vision-camera'
import { CAPTURE_BUTTON_SIZE, SCREEN_HEIGHT, SCREEN_WIDTH } from './../Constants'
import { getImageMetaData } from 'react-native-compressor';

const START_RECORDING_DELAY = 200
const BORDER_WIDTH = CAPTURE_BUTTON_SIZE * 0.1
Expand Down Expand Up @@ -62,7 +63,14 @@ const _CaptureButton: React.FC<Props> = ({
const photo = await camera.current.takePhoto({
flash: flash,
enableShutterSound: false,
normalizeOrientation: true,
})
Alert.alert('Photo taken!', `width: ${photo.width}, height: ${photo.height}`)
console.log('Photo taken!', photo.width, photo.height)
getImageMetaData(photo.path).then((data) => {
console.log('Image Meta Data:', data);
Alert.alert('Image Meta Data:', JSON.stringify(data));
});
onMediaCaptured(photo, 'photo')
} catch (e) {
console.error('Failed to take photo!', e)
Expand Down
5 changes: 5 additions & 0 deletions package/example/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5622,6 +5622,11 @@ react-is@^17.0.1:
resolved "https://registry.yarnpkg.com/react-is/-/react-is-17.0.2.tgz#e691d4a8e9c789365655539ab372762b0efb54f0"
integrity sha512-w2GsyukL62IJnlaff/nRegPQR94C/XXamvMWmSHRJ4y7Ts/4ocGRmTHvOs8PSE6pB3dWOrD/nueuU5sduBsQ4w==

react-native-compressor@^1.9.0:
version "1.9.0"
resolved "https://registry.yarnpkg.com/react-native-compressor/-/react-native-compressor-1.9.0.tgz#3b5aabac97f996036990f41294383a34510aaf22"
integrity sha512-kJgRBTrvI8/yOJs8GhXfc1bZui/Wl3FNKOkc3K5+EIgihzU/vS+UTLkojGcYwoYXxj4xP7GFwp0SJLsNOOJZNw==

react-native-gesture-handler@^2.16.2:
version "2.16.2"
resolved "https://registry.yarnpkg.com/react-native-gesture-handler/-/react-native-gesture-handler-2.16.2.tgz#032bd2a07334292d7f6cff1dc9d1ec928f72e26d"
Expand Down
7 changes: 7 additions & 0 deletions package/src/types/PhotoFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ export interface TakePhotoOptions {
* @default true
*/
enableShutterSound?: boolean

/**
* Whether to normalize orientation or not.
*
* @default false
*/
normalizeOrientation?: boolean
}

/**
Expand Down