import { usePermissions } from '@use-expo/permissions'; import { BarCodeScanner, BarCodePoint, BarCodeEvent, BarCodeBounds } from 'expo-barcode-scanner'; import * as Permissions from 'expo-permissions'; import * as ScreenOrientation from 'expo-screen-orientation'; import React from 'react'; import { Button, Platform, StyleSheet, Text, View } from 'react-native'; import * as Svg from 'react-native-svg'; const BUTTON_COLOR = Platform.OS === 'ios' ? '#fff' : '#666'; type State = { type: any; cornerPoints?: BarCodePoint[]; alerting: boolean; haveDimensions: boolean; canvasHeight?: number; canvasWidth?: number; boundingBox?: BarCodeBounds; cornerPointsString?: string; showBoundingBox: boolean; showText: boolean; data: string; }; const initialState: State = { type: BarCodeScanner.Constants.Type.back, alerting: false, haveDimensions: false, showBoundingBox: false, data: '', showText: false, }; function reducer(state: State, action: Partial): State { return { ...state, ...action, }; } export default function BarcodeScannerScreen() { const [isPermissionsGranted] = usePermissions(Permissions.CAMERA, { ask: true }); if (!isPermissionsGranted) { return ( You have not granted permission to use the camera on this device! ); } return ; } function BarcodeScannerExample() { const [state, dispatch] = React.useReducer(reducer, initialState); let canChangeOrientation = false; const toggleAlertingAboutResult = () => { dispatch({ alerting: !state.alerting }); }; const toggleScreenOrientationState = () => { if (canChangeOrientation) { ScreenOrientation.lockAsync(ScreenOrientation.OrientationLock.PORTRAIT_UP); } else { ScreenOrientation.lockAsync(ScreenOrientation.OrientationLock.ALL); } canChangeOrientation = !canChangeOrientation; }; const setCanvasDimensions = ({ nativeEvent: { layout } }: any) => { dispatch({ canvasWidth: layout.width, canvasHeight: layout.height, haveDimensions: true }); }; const toggleType = () => { dispatch({ type: state.type === BarCodeScanner.Constants.Type.back ? BarCodeScanner.Constants.Type.front : BarCodeScanner.Constants.Type.back, }); }; const handleBarCodeScanned = (barCodeEvent: any) => { if (state.alerting) { requestAnimationFrame(() => { alert(JSON.stringify(barCodeEvent)); }); } dispatch({ data: barCodeEvent.data, cornerPoints: barCodeEvent.cornerPoints, boundingBox: barCodeEvent.bounds, cornerPointsString: getPointsString(barCodeEvent.cornerPoints), }); }; const toggleText = () => dispatch({ showText: !state.showText }); const toggleBoundingBox = () => dispatch({ showBoundingBox: !state.showBoundingBox }); const getPointsString = (barCodePoints?: BarCodePoint[]): string | undefined => { if (!barCodePoints) { return; } return barCodePoints.map(({ x, y }) => `${Math.round(x)},${Math.round(y)}`).join(' '); }; const circles = (state.cornerPoints || []).map((point, index) => ( )); return ( {state.haveDimensions && ( {state.showBoundingBox && state.cornerPointsString && ( )} {state.showText && state.boundingBox && ( {state.data} )} {circles} )}