Skip to content
Closed
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
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@
"evictable",
"exchangerate",
"exchrate",
"EXIF",
"exfy",
"exitstatus",
"Expatriot",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const getDeviceOrientationAwareImageSize: GetDeviceOrientationAwareImageSize = (
imageHeight: isRotated ? width : height,
aspectRatioWidth: isRotated ? aspectRatioWidth : aspectRatioHeight,
aspectRatioHeight: isRotated ? aspectRatioHeight : aspectRatioWidth,
rotation,
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type DeviceOrientationAwareImageSize = {
imageHeight: number;
aspectRatioWidth?: number;
aspectRatioHeight?: number;
rotation?: number;
};

type GetDeviceOrientationAwareImageSizeParams = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function cropImageToAspectRatio(
imageHeight,
aspectRatioWidth: ratioWidth,
aspectRatioHeight: ratioHeight,
rotation,
} = getDeviceOrientationAwareImageSize({imageSize, orientation, aspectRatioWidth, aspectRatioHeight});

if (!imageWidth || !imageHeight || !ratioWidth || !ratioHeight) {
Expand All @@ -68,6 +69,41 @@ function cropImageToAspectRatio(
const crop = calculateCropRect(imageWidth, imageHeight, ratioWidth, ratioHeight, shouldAlignTop);
const croppedFilename = `receipt_cropped_${Date.now()}.jpeg`;

// On Android, when EXIF rotation of 90° or 270° is detected, the image loader (Glide)
// may or may not auto-apply the rotation depending on the device. We normalize the
// image first to strip EXIF metadata, then check whether rotation was already applied
// by comparing pixel dimensions, and act accordingly. We only handle 90/270 because
// 180° rotation doesn't swap dimensions, making auto-rotation undetectable.
if (rotation === 90 || rotation === 270) {
const tempFilename = `receipt_normalized_${Date.now()}.jpeg`;
return cropOrRotateImage(image.source, [], {compress: 1, name: tempFilename, type: IMAGE_TYPE}).then((normalizedImage) => {
const normalizedUri = 'uri' in normalizedImage ? normalizedImage.uri : undefined;
if (!normalizedUri) {
return image;
}
return ImageSize.getSize(normalizedUri).then((normalizedSize) => {
// If the normalized image dimensions differ from the raw dimensions,
// the image loader already applied EXIF rotation during normalization.
const wasAutoRotated = normalizedSize.width !== imageSize.width || normalizedSize.height !== imageSize.height;

const cropPromise = wasAutoRotated
? cropOrRotateImage(normalizedUri, [{crop: calculateCropRect(normalizedSize.width, normalizedSize.height, ratioWidth, ratioHeight, shouldAlignTop)}], {
compress: 1,
name: croppedFilename,
type: IMAGE_TYPE,
})
: cropOrRotateImage(normalizedUri, [{rotate: rotation}, {crop}], {compress: 1, name: croppedFilename, type: IMAGE_TYPE});

return cropPromise.then((croppedImage) => {
if (!croppedImage?.uri || !croppedImage?.name) {
return image;
}
return {file: croppedImage, filename: croppedImage.name, source: croppedImage.uri};
});
});
});
}

return cropOrRotateImage(image.source, [{crop}], {compress: 1, name: croppedFilename, type: IMAGE_TYPE}).then((croppedImage) => {
if (!croppedImage?.uri || !croppedImage?.name) {
return image;
Expand Down
86 changes: 66 additions & 20 deletions src/pages/iou/request/step/IOURequestStepScan/index.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React, {useCallback, useEffect, useRef, useState} from 'react';
import {Alert, AppState, StyleSheet, View} from 'react-native';
import ReactNativeBlobUtil from 'react-native-blob-util';
import {Gesture, GestureDetector} from 'react-native-gesture-handler';
import ImageSize from 'react-native-image-size';
import {RESULTS} from 'react-native-permissions';
import Animated, {useAnimatedStyle, useSharedValue, withDelay, withSequence, withSpring, withTiming} from 'react-native-reanimated';
import type {Camera, PhotoFile, Point} from 'react-native-vision-camera';
Expand All @@ -26,6 +27,7 @@ import usePolicy from '@hooks/usePolicy';
import useStyleUtils from '@hooks/useStyleUtils';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import cropOrRotateImage from '@libs/cropOrRotateImage';
import {showCameraPermissionsAlert} from '@libs/fileDownload/FileUtils';
import getPhotoSource from '@libs/fileDownload/getPhotoSource';
import getPlatform from '@libs/getPlatform';
Expand Down Expand Up @@ -54,6 +56,48 @@ import useMobileReceiptScan from './hooks/useMobileReceiptScan';
import useReceiptScan from './hooks/useReceiptScan';
import type IOURequestStepScanProps from './types';

/**
* On Android, some devices save photos with landscape pixel data and EXIF metadata
* indicating the required rotation. Whether the image displays correctly depends on
* whether the viewer auto-applies EXIF rotation, which is inconsistent across devices
* and backends. This normalizes the pixel orientation using a two-pass approach:
* 1. Strip EXIF via empty manipulateAsync to detect if Glide auto-rotated
* 2. If not auto-rotated, explicitly rotate the pixels
*/
function normalizePhotoOrientation(photoUri: string): Promise<string> {
return ImageSize.getSize(photoUri)
.then((imageSize) => {
const {rotation} = imageSize;
if (rotation !== 90 && rotation !== 270) {
return photoUri;
}

// First pass: strip EXIF metadata via empty actions
const tempFilename = `receipt_normalized_${Date.now()}.jpeg`;
return cropOrRotateImage(photoUri, [], {compress: 1, name: tempFilename, type: 'image/jpeg'}).then((normalizedImage) => {
const normalizedUri = normalizedImage?.uri;
if (!normalizedUri) {
return photoUri;
}

return ImageSize.getSize(normalizedUri).then((normalizedSize) => {
const wasAutoRotated = normalizedSize.width !== imageSize.width || normalizedSize.height !== imageSize.height;

if (wasAutoRotated) {
return normalizedUri;
}

// Glide didn't auto-rotate, apply rotation explicitly
const rotatedFilename = `receipt_rotated_${Date.now()}.jpeg`;
return cropOrRotateImage(normalizedUri, [{rotate: rotation}], {compress: 1, name: rotatedFilename, type: 'image/jpeg'}).then(
(rotatedImage) => rotatedImage?.uri ?? photoUri,
);
});
});
})
.catch(() => photoUri);
}

function IOURequestStepScan({
report,
route: {
Expand Down Expand Up @@ -386,34 +430,36 @@ function IOURequestStepScan({
})
: initialTransaction;
const transactionID = transaction?.transactionID ?? initialTransactionID;
const source = getPhotoSource(photo.path);
const originalSource = getPhotoSource(photo.path);
const filename = photo.path;
endSpan(CONST.TELEMETRY.SPAN_RECEIPT_CAPTURE);

const cameraFile = {
uri: source,
name: filename,
type: 'image/jpeg',
source,
};
return normalizePhotoOrientation(originalSource).then((source) => {
const cameraFile = {
uri: source,
name: filename,
type: 'image/jpeg',
source,
};

setMoneyRequestReceipt(transactionID, source, filename, !isEditing, 'image/jpeg');
setMoneyRequestReceipt(transactionID, source, filename, !isEditing, 'image/jpeg');

if (isEditing) {
updateScanAndNavigate(cameraFile as FileObject, source);
return;
}
if (isEditing) {
updateScanAndNavigate(cameraFile as FileObject, source);
return;
}

const newReceiptFiles = [...receiptFiles, {file: cameraFile as FileObject, source, transactionID}];
setReceiptFiles(newReceiptFiles);
const newReceiptFiles = [...receiptFiles, {file: cameraFile as FileObject, source, transactionID}];
setReceiptFiles(newReceiptFiles);

if (isMultiScanEnabled) {
setDidCapturePhoto(false);
isCapturingPhoto.current = false;
return;
}
if (isMultiScanEnabled) {
setDidCapturePhoto(false);
isCapturingPhoto.current = false;
return;
}

submitReceipts(newReceiptFiles);
submitReceipts(newReceiptFiles);
});
})
.catch((error: string) => {
isCapturingPhoto.current = false;
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/getDeviceOrientationAwareImageSizeTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ describe('getDeviceOrientationAwareImageSize', () => {
imageHeight: 1080,
aspectRatioWidth: 9,
aspectRatioHeight: 16,
rotation: 0,
});
});

Expand All @@ -139,6 +140,7 @@ describe('getDeviceOrientationAwareImageSize', () => {
imageHeight: 1080,
aspectRatioWidth: 3,
aspectRatioHeight: 4,
rotation: 180,
});
});
});
Expand All @@ -156,6 +158,7 @@ describe('getDeviceOrientationAwareImageSize', () => {
imageHeight: 1920,
aspectRatioWidth: 16,
aspectRatioHeight: 9,
rotation: 90,
});
});

Expand All @@ -171,6 +174,7 @@ describe('getDeviceOrientationAwareImageSize', () => {
imageHeight: 1920,
aspectRatioWidth: 21,
aspectRatioHeight: 9,
rotation: 270,
});
});
});
Expand Down
Loading