This repository has been archived by the owner on Feb 24, 2024. It is now read-only.
/
Decoder.js
70 lines (65 loc) · 1.72 KB
/
Decoder.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
import React, { Component } from "react";
import { Image, View, Button, Alert, StyleSheet } from "react-native";
import * as RNLBarCode from "@yyyyu/react-native-barcode";
import IMAGES from "./images";
export default class App extends Component {
render() {
return (
<View style={styles.container}>
<Image source={IMAGES.EAN_13} />
<Button title="Decode Image" onPress={this.decode} />
<Button title="Scan Screen" onPress={this.screenshot} />
</View>
);
}
decode = async () => {
try {
const result = await RNLBarCode.decode({
data: IMAGES.QR_CODE,
formats: RNLBarCode.Type.Common.QR_CODE,
decoder: RNLBarCode.Decoder.Auto
});
this.handleResult(result);
} catch (e) {
Alert.alert(RNLBarCode.Errors[error.code], error.message, [
{ text: "Ok" }
]);
}
};
screenshot = async () => {
try {
const result = await RNLBarCode.decode({
formats: RNLBarCode.Type.Common.EAN_13,
decoder: RNLBarCode.Decoder.Auto,
screenshot: true
});
this.handleResult(result);
} catch (e) {
Alert.alert(RNLBarCode.Errors[error.code], error.message, [
{ text: "Ok" }
]);
}
};
handleResult = result => {
if (result != null) {
Alert.alert(
"Success!",
`format: ${RNLBarCode.Type.Common[result.format]}, content: ${
result.content
}`,
[{ text: "Ok" }]
);
} else {
console.log("not found");
Alert.alert("Not Found", null, [{ text: "Ok" }]);
}
};
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
alignItems: "center",
backgroundColor: "#fff"
}
});