Skip to content

Commit c1a493e

Browse files
committed
Fix aspect ratio of preview in landscape mode and use takePhoto instead of takeSnapshot
1 parent ec1f746 commit c1a493e

5 files changed

Lines changed: 27 additions & 7 deletions

File tree

src/pages/iou/request/step/IOURequestStepScan/captureReceipt/index.android.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import type {CaptureReceipt} from './types';
22

3-
const captureReceipt: CaptureReceipt = (camera, {flash, hasFlash, isPlatformMuted, path}) => {
3+
const captureReceipt: CaptureReceipt = (camera, {flash, hasFlash, isPlatformMuted, path, isInLandscapeMode}) => {
44
const useFlash = flash && hasFlash;
5-
if (useFlash) {
5+
if (useFlash || isInLandscapeMode) {
66
return camera.takePhoto({
7-
flash: 'on',
7+
flash: useFlash ? 'on' : 'off',
88
enableShutterSound: !isPlatformMuted,
99
path,
1010
});

src/pages/iou/request/step/IOURequestStepScan/captureReceipt/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ type CaptureReceiptOptions = {
55
hasFlash: boolean;
66
isPlatformMuted: boolean | undefined;
77
path: string;
8+
isInLandscapeMode: boolean;
89
};
910

1011
type CaptureReceipt = (camera: Camera, options: CaptureReceiptOptions) => Promise<PhotoFile>;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type {CameraDeviceFormat} from 'react-native-vision-camera';
2+
3+
function getCameraAspectRatio(format: CameraDeviceFormat | undefined, isInLandscapeMode: boolean): number | undefined {
4+
if (!format) {
5+
return undefined;
6+
}
7+
if (isInLandscapeMode) {
8+
return format.photoWidth / format.photoHeight;
9+
}
10+
11+
return format.photoHeight / format.photoWidth;
12+
}
13+
14+
export default getCameraAspectRatio;

src/pages/iou/request/step/IOURequestStepScan/index.native.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import type {FileObject} from '@src/types/utils/Attachment';
5050
import captureReceipt from './captureReceipt';
5151
import NavigationAwareCamera from './components/NavigationAwareCamera/Camera';
5252
import ReceiptPreviews from './components/ReceiptPreviews';
53+
import getCameraAspectRatio from './getCameraAspectRatio';
5354
import useMobileReceiptScan from './hooks/useMobileReceiptScan';
5455
import useReceiptScan from './hooks/useReceiptScan';
5556
import type IOURequestStepScanProps from './types';
@@ -112,8 +113,9 @@ function IOURequestStepScan({
112113
{photoResolution: {width: CONST.RECEIPT_CAMERA.PHOTO_WIDTH, height: CONST.RECEIPT_CAMERA.PHOTO_HEIGHT}},
113114
{videoResolution: {width: windowHeight, height: windowWidth}},
114115
]);
116+
115117
// Format dimensions are in landscape orientation, so height/width gives portrait aspect ratio
116-
const cameraAspectRatio = format ? format.photoHeight / format.photoWidth : undefined;
118+
const cameraAspectRatio = getCameraAspectRatio(format, isInLandscapeMode);
117119
const fps = useMemo(() => (format ? Math.min(Math.max(30, format.minFps), format.maxFps) : 30), [format]);
118120

119121
const navigateBack = () => {
@@ -316,7 +318,7 @@ function IOURequestStepScan({
316318

317319
const path = getReceiptsUploadFolderPath();
318320

319-
captureReceipt(camera.current, {flash, hasFlash, isPlatformMuted, path})
321+
captureReceipt(camera.current, {flash, hasFlash, isPlatformMuted, path, isInLandscapeMode})
320322
.then((photo: PhotoFile) => {
321323
setDidCapturePhoto(true);
322324

@@ -435,7 +437,7 @@ function IOURequestStepScan({
435437
{cameraPermissionStatus === RESULTS.GRANTED && device != null && (
436438
<View style={[styles.cameraView, styles.alignItemsCenter]}>
437439
<GestureDetector gesture={tapGesture}>
438-
<View style={StyleUtils.getCameraViewfinderStyle(cameraAspectRatio)}>
440+
<View style={StyleUtils.getCameraViewfinderStyle(cameraAspectRatio, isInLandscapeMode)}>
439441
<NavigationAwareCamera
440442
ref={camera}
441443
device={device}

src/styles/utils/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,10 @@ function getBackgroundColorStyle(backgroundColor: ColorValue): ViewStyle {
491491
};
492492
}
493493

494-
function getCameraViewfinderStyle(aspectRatio: number | undefined): ViewStyle {
494+
function getCameraViewfinderStyle(aspectRatio: number | undefined, isInLandscapeMode: boolean): ViewStyle {
495+
if (isInLandscapeMode && aspectRatio) {
496+
return {aspectRatio, height: '100%', maxWidth: '100%'};
497+
}
495498
if (aspectRatio) {
496499
return {aspectRatio, minWidth: '100%', minHeight: '100%'};
497500
}

0 commit comments

Comments
 (0)