Skip to content

Commit

Permalink
feat(example): add barcode screen
Browse files Browse the repository at this point in the history
  • Loading branch information
a7medev committed Sep 30, 2023
1 parent 56030f5 commit f310a5e
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 8 deletions.
2 changes: 1 addition & 1 deletion barcode-scanning/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export enum BarcodeFormat {
AZTEC = 4096,
}

interface Barcode {
export interface Barcode {
format: BarcodeFormat;
value: string;
}
Expand Down
15 changes: 15 additions & 0 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ PODS:
- GoogleUtilities/Environment (~> 7.7)
- nanopb (< 2.30910.0, >= 2.30908.0)
- PromisesObjC (< 3.0, >= 1.2)
- GoogleMLKit/BarcodeScanning (4.0.0):
- GoogleMLKit/MLKitCore
- MLKitBarcodeScanning (~> 3.0.0)
- GoogleMLKit/FaceDetection (4.0.0):
- GoogleMLKit/MLKitCore
- MLKitFaceDetection (~> 3.0.0)
Expand Down Expand Up @@ -146,6 +149,9 @@ PODS:
- hermes-engine/Pre-built (0.72.4)
- libevent (2.1.12)
- MLImage (1.0.0-beta4)
- MLKitBarcodeScanning (3.0.0):
- MLKitCommon (~> 9.0)
- MLKitVision (~> 5.0)
- MLKitCommon (9.0.0):
- GoogleDataTransport (~> 9.0)
- GoogleToolboxForMac/Logger (~> 2.1)
Expand Down Expand Up @@ -621,6 +627,9 @@ PODS:
- React-jsi (= 0.72.4)
- React-logger (= 0.72.4)
- React-perflogger (= 0.72.4)
- RNMLKitBarcodeScanning (1.3.0):
- GoogleMLKit/BarcodeScanning (= 4.0.0)
- React
- RNMLKitFaceDetection (1.2.0):
- GoogleMLKit/FaceDetection (= 4.0.0)
- React
Expand Down Expand Up @@ -717,6 +726,7 @@ DEPENDENCIES:
- React-runtimescheduler (from `../node_modules/react-native/ReactCommon/react/renderer/runtimescheduler`)
- React-utils (from `../node_modules/react-native/ReactCommon/react/utils`)
- ReactCommon/turbomodule/core (from `../node_modules/react-native/ReactCommon`)
- "RNMLKitBarcodeScanning (from `../node_modules/@react-native-ml-kit/barcode-scanning`)"
- "RNMLKitFaceDetection (from `../node_modules/@react-native-ml-kit/face-detection`)"
- "RNMLKitImageLabeling (from `../node_modules/@react-native-ml-kit/image-labeling`)"
- "RNMLKitTextRecognition (from `../node_modules/@react-native-ml-kit/text-recognition`)"
Expand Down Expand Up @@ -744,6 +754,7 @@ SPEC REPOS:
- GTMSessionFetcher
- libevent
- MLImage
- MLKitBarcodeScanning
- MLKitCommon
- MLKitFaceDetection
- MLKitImageLabeling
Expand Down Expand Up @@ -861,6 +872,8 @@ EXTERNAL SOURCES:
:path: "../node_modules/react-native/ReactCommon/react/utils"
ReactCommon:
:path: "../node_modules/react-native/ReactCommon"
RNMLKitBarcodeScanning:
:path: "../node_modules/@react-native-ml-kit/barcode-scanning"
RNMLKitFaceDetection:
:path: "../node_modules/@react-native-ml-kit/face-detection"
RNMLKitImageLabeling:
Expand Down Expand Up @@ -908,6 +921,7 @@ SPEC CHECKSUMS:
hermes-engine: 81191603c4eaa01f5e4ae5737a9efcf64756c7b2
libevent: 4049cae6c81cdb3654a443be001fb9bdceff7913
MLImage: 7bb7c4264164ade9bf64f679b40fb29c8f33ee9b
MLKitBarcodeScanning: 04e264482c5f3810cb89ebc134ef6b61e67db505
MLKitCommon: c1b791c3e667091918d91bda4bba69a91011e390
MLKitFaceDetection: 4981488f71ea8ed718cda692a29ab562637d9ae5
MLKitImageLabeling: 9fcdbbfbc454f22009c10015c403ac6319385cdf
Expand Down Expand Up @@ -957,6 +971,7 @@ SPEC CHECKSUMS:
React-runtimescheduler: 4941cc1b3cf08b792fbf666342c9fc95f1969035
React-utils: b79f2411931f9d3ea5781404dcbb2fa8a837e13a
ReactCommon: 4b2bdcb50a3543e1c2b2849ad44533686610826d
RNMLKitBarcodeScanning: e787b4653f19c32db49a1825738f2cf9cc5fff09
RNMLKitFaceDetection: 3aac39267a3d2b63049a13fb288c999faa2f188e
RNMLKitImageLabeling: 415b8bc3b4b0bb9ce1a034ace52f9ffa413b7711
RNMLKitTextRecognition: b2ba09361f084db39655346a65d4c07327a34afd
Expand Down
1 change: 1 addition & 0 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"test": "jest"
},
"dependencies": {
"@react-native-ml-kit/barcode-scanning": "link:../barcode-scanning",
"@react-native-ml-kit/face-detection": "link:../face-detection",
"@react-native-ml-kit/image-labeling": "link:../image-labeling",
"@react-native-ml-kit/text-recognition": "link:../text-recognition",
Expand Down
17 changes: 12 additions & 5 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import TextRecognitionScreen from './text-recognition/TextRecognitionScreen';
import FaceDetectionScreen from './face-detection/FaceDetectionScreen';
import ImageLabelingScreen from './image-labeling/ImageLabelingScreen';
import HomeScreen from './core/HomeScreen';
import BarcodeScanningScreen from './barcode-scanning/BarcodeScanningScreen';

export type ParamList = {
Home: undefined;
TextRecognition: undefined;
BarcodeScanning: undefined;
FaceDetection: undefined;
ImageLabeling: undefined;
};
Expand All @@ -20,16 +22,21 @@ const App: React.FC = () => {
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen
name="FaceDetection"
component={FaceDetectionScreen}
options={{title: 'Face Detection'}}
/>
<Stack.Screen
name="TextRecognition"
component={TextRecognitionScreen}
options={{title: 'Text Recognition'}}
/>
<Stack.Screen
name="BarcodeScanning"
component={BarcodeScanningScreen}
options={{title: 'Barcode Scanning'}}
/>
<Stack.Screen
name="FaceDetection"
component={FaceDetectionScreen}
options={{title: 'Face Detection'}}
/>
<Stack.Screen
name="ImageLabeling"
component={ImageLabelingScreen}
Expand Down
63 changes: 63 additions & 0 deletions example/src/barcode-scanning/BarcodeScanningScreen.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import React, {useState} from 'react';
import {Text, StyleSheet, View, FlatList} from 'react-native';
import BarcodeScanning, {Barcode} from '@react-native-ml-kit/barcode-scanning';
import ChooseImageButton, {ImageDetails} from '../core/ChooseImageButton';
import Label from '../core/Label';
import PreviewImage from '../core/PreviewImage';

const BarcodeScanningScreen = () => {
const [image, setImage] = useState<ImageDetails>();
const [barcodes, setBarcodes] = useState<Barcode[]>([]);

const handleChoose = async (currentImage: ImageDetails) => {
setImage(currentImage);

const result = await BarcodeScanning.scan(currentImage.path);

setBarcodes(result);
};

return (
<View style={styles.container}>
<ChooseImageButton onChoose={handleChoose} />

{image && <PreviewImage source={image.path} />}

{barcodes.length > 0 && (
<>
<Text style={styles.heading}>Barcodes</Text>

<FlatList
data={barcodes}
style={styles.list}
keyExtractor={barcode => `${barcode.format}-${barcode.value}`}
renderItem={({item}) => (
<Label key={item.value}>
{item.value} - {item.format}
</Label>
)}
/>
</>
)}
</View>
);
};

const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
heading: {
fontSize: 20,
marginBottom: 10,
marginTop: 20,
},
list: {
width: '100%',
padding: 10,
},
});

export default BarcodeScanningScreen;
4 changes: 4 additions & 0 deletions example/src/core/HomeScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ const HomeScreen = ({navigation}: NativeStackScreenProps<ParamList>) => {
title="Text Recognition"
onPress={() => navigation.navigate('TextRecognition')}
/>
<ListTile
title="Barcode Scanning"
onPress={() => navigation.navigate('BarcodeScanning')}
/>
<ListTile
title="Face Detection"
onPress={() => navigation.navigate('FaceDetection')}
Expand Down
32 changes: 32 additions & 0 deletions example/src/core/Label.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React from 'react';
import {View, Text, StyleSheet} from 'react-native';

interface LabelProps {
children: React.ReactNode;
}

const Label = ({children}: LabelProps) => {
return (
<View style={styles.label}>
<Text>{children}</Text>
</View>
);
};

const styles = StyleSheet.create({
label: {
backgroundColor: 'white',
shadowColor: '#000',
shadowOffset: {width: 0, height: 1},
shadowOpacity: 0.2,
shadowRadius: 1,
elevation: 1,
padding: 10,
borderRadius: 5,
marginBottom: 8,
marginHorizontal: 10,
alignItems: 'center',
},
});

export default Label;
4 changes: 2 additions & 2 deletions example/src/image-labeling/ImageLabelingScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {Text, StyleSheet, View} from 'react-native';
import ImageLabeling, {Label} from '@react-native-ml-kit/image-labeling';
import ChooseImageButton, {ImageDetails} from '../core/ChooseImageButton';

const App = () => {
const ImageLabelingScreen = () => {
const [labels, setLabels] = useState<Label[]>([]);

const handleChoose = async (image: ImageDetails) => {
Expand Down Expand Up @@ -50,4 +50,4 @@ const styles = StyleSheet.create({
},
});

export default App;
export default ImageLabelingScreen;
4 changes: 4 additions & 0 deletions example/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2068,6 +2068,10 @@
prompts "^2.4.0"
semver "^7.5.2"

"@react-native-ml-kit/barcode-scanning@link:../barcode-scanning":
version "0.0.0"
uid ""

"@react-native-ml-kit/face-detection@link:../face-detection":
version "0.0.0"
uid ""
Expand Down

0 comments on commit f310a5e

Please sign in to comment.