-
Notifications
You must be signed in to change notification settings - Fork 2
/
App.js
146 lines (135 loc) · 3.76 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import "expo-dev-client";
import { StatusBar } from "expo-status-bar";
import React, { useEffect, useState } from "react";
import { Asset } from "expo-asset";
import { Button, Image, StyleSheet, Text, View } from "react-native";
import ProgressCircle from "react-native-progress/Circle";
import TesseractOcr, {
LANG_ENGLISH,
useEventListener,
} from "react-native-tesseract-ocr";
import * as FileSystem from "expo-file-system";
const DEFAULT_HEIGHT = 500;
const DEFAULT_WIDTH = 600;
const defaultPickerOptions = {
cropping: true,
height: DEFAULT_HEIGHT,
width: DEFAULT_WIDTH,
};
export default function App() {
const [fetchingTrainingData, setFetchingTrainingData] = useState(true);
const [isLoading, setIsLoading] = useState(false);
const [progress, setProgress] = useState(0);
const imgSrc = require("./assets/sample.png");
const [imgUri, setImgUri] = useState(null);
const [text, setText] = useState("");
async function getTrainedData() {
const trainedDataUrl =
"https://github.com/tesseract-ocr/tessdata/raw/3.04.00/eng.traineddata";
const trainedDataDir = FileSystem.cacheDirectory + "tessdata/";
const trainedDataPath = trainedDataDir + "eng.traineddata";
const pathInfo = await FileSystem.getInfoAsync(trainedDataPath);
if (pathInfo.exists) {
setFetchingTrainingData(false);
} else {
alert("Training data does not exist. Downloading...");
await FileSystem.makeDirectoryAsync(trainedDataDir, { intermediates: true });
FileSystem.downloadAsync(trainedDataUrl, trainedDataPath)
.then(({ uri }) => {
alert(`Finished downloading to ${uri}`);
setFetchingTrainingData(false);
})
.catch((error) => {
alert(error);
});
}
}
Asset.fromModule(require("./assets/sample.png"))
.downloadAsync()
.then(({ localUri }) => setImgUri(localUri));
useEventListener("onProgressChange", (p) => {
setProgress(p.percent / 100);
});
useEffect(() => {
getTrainedData();
}, []);
const recognizeTextFromImage = async (path) => {
setIsLoading(true);
try {
const tesseractOptions = {};
const recognizedText = await TesseractOcr.recognize(
path,
LANG_ENGLISH,
tesseractOptions
);
setText(recognizedText);
} catch (err) {
console.error(err);
setText("");
}
setIsLoading(false);
setProgress(0);
};
return (
<View style={styles.container}>
<Text style={styles.title}>Expo OCR Example</Text>
<StatusBar style="auto" />
<View style={styles.options}>
<View style={styles.button}>
<Button
disabled={fetchingTrainingData || isLoading}
title="Recognize"
onPress={() => {
recognizeTextFromImage(imgUri);
}}
/>
</View>
</View>
{imgUri && (
<View style={styles.imageContainer}>
<Image style={styles.image} source={imgSrc} />
{isLoading ? (
<ProgressCircle showsText progress={progress} />
) : (
<Text>{text}</Text>
)}
</View>
)}
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
alignItems: "center",
backgroundColor: "#F5FCFF",
},
options: {
flexDirection: "row",
justifyContent: "space-between",
padding: 10,
},
button: {
marginHorizontal: 10,
},
imageContainer: {
justifyContent: "center",
alignItems: "center",
},
image: {
marginVertical: 15,
height: DEFAULT_HEIGHT / 2.5,
width: DEFAULT_WIDTH / 2.5,
},
title: {
fontSize: 20,
textAlign: "center",
margin: 10,
},
instructions: {
textAlign: "center",
color: "#333333",
marginBottom: 5,
},
});