1 | import {
|
2 | PermissionResponse,
|
3 | PermissionStatus,
|
4 | PermissionHookOptions,
|
5 | createPermissionHook,
|
6 | UnavailabilityError,
|
7 | } from 'expo-modules-core';
|
8 | import * as React from 'react';
|
9 | import { Platform, ViewProps } from 'react-native';
|
10 |
|
11 | import ExpoBarCodeScannerModule from './ExpoBarCodeScannerModule';
|
12 | import ExpoBarCodeScannerView from './ExpoBarCodeScannerView';
|
13 |
|
14 | const { BarCodeType, Type } = ExpoBarCodeScannerModule;
|
15 |
|
16 | const EVENT_THROTTLE_MS = 500;
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | export type BarCodePoint = {
|
24 | |
25 |
|
26 |
|
27 | x: number;
|
28 | |
29 |
|
30 |
|
31 | y: number;
|
32 | };
|
33 |
|
34 |
|
35 | export type BarCodeSize = {
|
36 | |
37 |
|
38 |
|
39 | height: number;
|
40 | |
41 |
|
42 |
|
43 | width: number;
|
44 | };
|
45 |
|
46 |
|
47 | export type BarCodeBounds = {
|
48 | |
49 |
|
50 |
|
51 | origin: BarCodePoint;
|
52 | |
53 |
|
54 |
|
55 | size: BarCodeSize;
|
56 | };
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | export type BarCodeScannerResult = {
|
65 | |
66 |
|
67 |
|
68 | type: string;
|
69 | |
70 |
|
71 |
|
72 | data: string;
|
73 | |
74 |
|
75 |
|
76 | bounds?: BarCodeBounds;
|
77 | |
78 |
|
79 |
|
80 | cornerPoints?: BarCodePoint[];
|
81 | };
|
82 |
|
83 |
|
84 | export type BarCodeEvent = BarCodeScannerResult & {
|
85 | target?: number;
|
86 | };
|
87 |
|
88 |
|
89 | export type BarCodeEventCallbackArguments = {
|
90 | nativeEvent: BarCodeEvent;
|
91 | };
|
92 |
|
93 |
|
94 | export type BarCodeScannedCallback = (params: BarCodeEvent) => void;
|
95 |
|
96 |
|
97 | export type BarCodeScannerProps = ViewProps & {
|
98 | |
99 |
|
100 |
|
101 |
|
102 |
|
103 | type?: 'front' | 'back' | number;
|
104 | |
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 | barCodeTypes?: string[];
|
113 | |
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 | onBarCodeScanned?: BarCodeScannedCallback;
|
121 | };
|
122 |
|
123 | export class BarCodeScanner extends React.Component<BarCodeScannerProps> {
|
124 | lastEvents: { [key: string]: any } = {};
|
125 | lastEventsTimes: { [key: string]: any } = {};
|
126 |
|
127 | static Constants = {
|
128 | BarCodeType,
|
129 | Type,
|
130 | };
|
131 |
|
132 | static ConversionTables = {
|
133 | type: Type,
|
134 | };
|
135 |
|
136 | static defaultProps = {
|
137 | type: Type.back,
|
138 | barCodeTypes: Object.values(BarCodeType),
|
139 | };
|
140 |
|
141 |
|
142 | |
143 |
|
144 |
|
145 |
|
146 | static async getPermissionsAsync(): Promise<PermissionResponse> {
|
147 | return ExpoBarCodeScannerModule.getPermissionsAsync();
|
148 | }
|
149 |
|
150 |
|
151 | |
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 | static async requestPermissionsAsync(): Promise<PermissionResponse> {
|
158 | return ExpoBarCodeScannerModule.requestPermissionsAsync();
|
159 | }
|
160 |
|
161 |
|
162 | |
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 | static usePermissions = createPermissionHook({
|
172 | getMethod: BarCodeScanner.getPermissionsAsync,
|
173 | requestMethod: BarCodeScanner.requestPermissionsAsync,
|
174 | });
|
175 |
|
176 |
|
177 | |
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 | static async scanFromURLAsync(
|
188 | url: string,
|
189 | barCodeTypes: string[] = Object.values(BarCodeType)
|
190 | ): Promise<BarCodeScannerResult[]> {
|
191 | if (!ExpoBarCodeScannerModule.scanFromURLAsync) {
|
192 | throw new UnavailabilityError('expo-barcode-scanner', 'scanFromURLAsync');
|
193 | }
|
194 | if (Array.isArray(barCodeTypes) && !barCodeTypes.length) {
|
195 | throw new Error('No barCodeTypes specified; provide at least one barCodeType for scanner');
|
196 | }
|
197 |
|
198 | if (Platform.OS === 'ios') {
|
199 | if (Array.isArray(barCodeTypes) && !barCodeTypes.includes(BarCodeType.qr)) {
|
200 |
|
201 | throw new Error('Only QR type is supported by scanFromURLAsync() on iOS');
|
202 | }
|
203 |
|
204 | return await ExpoBarCodeScannerModule.scanFromURLAsync(url, [BarCodeType.qr]);
|
205 | }
|
206 |
|
207 |
|
208 | return await ExpoBarCodeScannerModule.scanFromURLAsync(url, barCodeTypes);
|
209 | }
|
210 |
|
211 | render() {
|
212 | const nativeProps = this.convertNativeProps(this.props);
|
213 | const { onBarCodeScanned } = this.props;
|
214 | return (
|
215 | <ExpoBarCodeScannerView
|
216 | {...nativeProps}
|
217 | onBarCodeScanned={this.onObjectDetected(onBarCodeScanned)}
|
218 | />
|
219 | );
|
220 | }
|
221 |
|
222 | onObjectDetected =
|
223 | (callback?: BarCodeScannedCallback) =>
|
224 | ({ nativeEvent }: BarCodeEventCallbackArguments) => {
|
225 | const { type } = nativeEvent;
|
226 | if (
|
227 | this.lastEvents[type] &&
|
228 | this.lastEventsTimes[type] &&
|
229 | JSON.stringify(nativeEvent) === this.lastEvents[type] &&
|
230 | Date.now() - this.lastEventsTimes[type] < EVENT_THROTTLE_MS
|
231 | ) {
|
232 | return;
|
233 | }
|
234 |
|
235 | if (callback) {
|
236 | callback(nativeEvent);
|
237 | this.lastEventsTimes[type] = new Date();
|
238 | this.lastEvents[type] = JSON.stringify(nativeEvent);
|
239 | }
|
240 | };
|
241 |
|
242 | convertNativeProps(props: BarCodeScannerProps) {
|
243 | const nativeProps: BarCodeScannerProps = {};
|
244 |
|
245 | for (const [key, value] of Object.entries(props)) {
|
246 | if (typeof value === 'string' && BarCodeScanner.ConversionTables[key]) {
|
247 | nativeProps[key] = BarCodeScanner.ConversionTables[key][value];
|
248 | } else {
|
249 | nativeProps[key] = value;
|
250 | }
|
251 | }
|
252 |
|
253 | return nativeProps;
|
254 | }
|
255 | }
|
256 |
|
257 | export { PermissionResponse, PermissionStatus, PermissionHookOptions };
|
258 | export const {
|
259 | Constants,
|
260 | getPermissionsAsync,
|
261 | requestPermissionsAsync,
|
262 | usePermissions,
|
263 | scanFromURLAsync,
|
264 | } = BarCodeScanner;
|