cronapp-cordova-plugin-barcodescanner 2.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +16 -0
- package/LICENSE.txt +22 -0
- package/README.md +52 -0
- package/hooks/windows/check-arch.js +52 -0
- package/package.json +37 -0
- package/plugin.xml +70 -0
- package/spec/helper/cordova.js +83 -0
- package/spec/index.spec.js +78 -0
- package/src/android/README.md +1 -0
- package/src/android/barcodescanner-release-2.1.5.aar +0 -0
- package/src/android/barcodescanner.gradle +17 -0
- package/src/android/com/phonegap/plugins/barcodescanner/BarcodeScanner.java +328 -0
- package/src/browser/BarcodeScannerProxy.js +24 -0
- package/src/ios/CDVBarcodeScanner.bundle/beep.caf +0 -0
- package/src/ios/CDVBarcodeScanner.bundle/torch.png +0 -0
- package/src/ios/CDVBarcodeScanner.bundle/torch@2x.png +0 -0
- package/src/ios/CDVBarcodeScanner.bundle/torch@3x.png +0 -0
- package/src/ios/CDVBarcodeScanner.mm +1072 -0
- package/src/ios/scannerOverlay.xib +185 -0
- package/src/windows/BarcodeScannerProxy.js +738 -0
- package/src/windows/assets/plugin-barcodeScanner.css +89 -0
- package/src/windows/lib/Properties/AssemblyInfo.cs +39 -0
- package/src/windows/lib/Reader.cs +173 -0
- package/src/windows/lib/WinRTBarcodeReader.csproj +137 -0
- package/src/windows/lib/ZXing.winmd +0 -0
- package/src/windows/lib.UW/ANY/ZXing.winmd +0 -0
- package/src/windows/lib.UW/ARM/ZXing.winmd +0 -0
- package/src/windows/lib.UW/x64/ZXing.winmd +0 -0
- package/src/windows/lib.UW/x86/ZXing.winmd +0 -0
- package/www/barcodescanner.js +156 -0
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PhoneGap is available under *either* the terms of the modified BSD license *or* the
|
|
3
|
+
* MIT License (2008). See http://opensource.org/licenses/alphabetical for full text.
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) Matt Kane 2010
|
|
6
|
+
* Copyright (c) 2011, IBM Corporation
|
|
7
|
+
* Copyright (c) 2013, Maciej Nux Jaros
|
|
8
|
+
*/
|
|
9
|
+
package com.phonegap.plugins.barcodescanner;
|
|
10
|
+
|
|
11
|
+
import org.json.JSONArray;
|
|
12
|
+
import org.json.JSONException;
|
|
13
|
+
import org.json.JSONObject;
|
|
14
|
+
|
|
15
|
+
import android.Manifest;
|
|
16
|
+
import android.app.Activity;
|
|
17
|
+
import android.content.Intent;
|
|
18
|
+
import android.os.Bundle;
|
|
19
|
+
import android.util.Log;
|
|
20
|
+
import android.content.pm.PackageManager;
|
|
21
|
+
|
|
22
|
+
import org.apache.cordova.CordovaPlugin;
|
|
23
|
+
import org.apache.cordova.CallbackContext;
|
|
24
|
+
import org.apache.cordova.PluginResult;
|
|
25
|
+
import org.apache.cordova.PermissionHelper;
|
|
26
|
+
|
|
27
|
+
import com.google.zxing.client.android.CaptureActivity;
|
|
28
|
+
import com.google.zxing.client.android.encode.EncodeActivity;
|
|
29
|
+
import com.google.zxing.client.android.Intents;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* This calls out to the ZXing barcode reader and returns the result.
|
|
33
|
+
*
|
|
34
|
+
* @sa https://github.com/apache/cordova-android/blob/master/framework/src/org/apache/cordova/CordovaPlugin.java
|
|
35
|
+
*/
|
|
36
|
+
public class BarcodeScanner extends CordovaPlugin {
|
|
37
|
+
public static final int REQUEST_CODE = 0x0ba7c;
|
|
38
|
+
|
|
39
|
+
private static final String SCAN = "scan";
|
|
40
|
+
private static final String ENCODE = "encode";
|
|
41
|
+
private static final String CANCELLED = "cancelled";
|
|
42
|
+
private static final String FORMAT = "format";
|
|
43
|
+
private static final String TEXT = "text";
|
|
44
|
+
private static final String DATA = "data";
|
|
45
|
+
private static final String TYPE = "type";
|
|
46
|
+
private static final String PREFER_FRONTCAMERA = "preferFrontCamera";
|
|
47
|
+
private static final String ORIENTATION = "orientation";
|
|
48
|
+
private static final String SHOW_FLIP_CAMERA_BUTTON = "showFlipCameraButton";
|
|
49
|
+
private static final String RESULTDISPLAY_DURATION = "resultDisplayDuration";
|
|
50
|
+
private static final String SHOW_TORCH_BUTTON = "showTorchButton";
|
|
51
|
+
private static final String TORCH_ON = "torchOn";
|
|
52
|
+
private static final String SAVE_HISTORY = "saveHistory";
|
|
53
|
+
private static final String DISABLE_BEEP = "disableSuccessBeep";
|
|
54
|
+
private static final String FORMATS = "formats";
|
|
55
|
+
private static final String PROMPT = "prompt";
|
|
56
|
+
private static final String TEXT_TYPE = "TEXT_TYPE";
|
|
57
|
+
private static final String EMAIL_TYPE = "EMAIL_TYPE";
|
|
58
|
+
private static final String PHONE_TYPE = "PHONE_TYPE";
|
|
59
|
+
private static final String SMS_TYPE = "SMS_TYPE";
|
|
60
|
+
|
|
61
|
+
private static final String LOG_TAG = "BarcodeScanner";
|
|
62
|
+
|
|
63
|
+
private String [] permissions = { Manifest.permission.CAMERA };
|
|
64
|
+
|
|
65
|
+
private JSONArray requestArgs;
|
|
66
|
+
private CallbackContext callbackContext;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Constructor.
|
|
70
|
+
*/
|
|
71
|
+
public BarcodeScanner() {
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Executes the request.
|
|
76
|
+
*
|
|
77
|
+
* This method is called from the WebView thread. To do a non-trivial amount of work, use:
|
|
78
|
+
* cordova.getThreadPool().execute(runnable);
|
|
79
|
+
*
|
|
80
|
+
* To run on the UI thread, use:
|
|
81
|
+
* cordova.getActivity().runOnUiThread(runnable);
|
|
82
|
+
*
|
|
83
|
+
* @param action The action to execute.
|
|
84
|
+
* @param args The exec() arguments.
|
|
85
|
+
* @param callbackContext The callback context used when calling back into JavaScript.
|
|
86
|
+
* @return Whether the action was valid.
|
|
87
|
+
*
|
|
88
|
+
* @sa https://github.com/apache/cordova-android/blob/master/framework/src/org/apache/cordova/CordovaPlugin.java
|
|
89
|
+
*/
|
|
90
|
+
@Override
|
|
91
|
+
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
|
|
92
|
+
this.callbackContext = callbackContext;
|
|
93
|
+
this.requestArgs = args;
|
|
94
|
+
|
|
95
|
+
if (action.equals(ENCODE)) {
|
|
96
|
+
JSONObject obj = args.optJSONObject(0);
|
|
97
|
+
if (obj != null) {
|
|
98
|
+
String type = obj.optString(TYPE);
|
|
99
|
+
String data = obj.optString(DATA);
|
|
100
|
+
|
|
101
|
+
// If the type is null then force the type to text
|
|
102
|
+
if (type == null) {
|
|
103
|
+
type = TEXT_TYPE;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (data == null) {
|
|
107
|
+
callbackContext.error("User did not specify data to encode");
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
encode(type, data);
|
|
112
|
+
} else {
|
|
113
|
+
callbackContext.error("User did not specify data to encode");
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
} else if (action.equals(SCAN)) {
|
|
117
|
+
|
|
118
|
+
//android permission auto add
|
|
119
|
+
if(!hasPermisssion()) {
|
|
120
|
+
requestPermissions(0);
|
|
121
|
+
} else {
|
|
122
|
+
scan(args);
|
|
123
|
+
}
|
|
124
|
+
} else {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
return true;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Starts an intent to scan and decode a barcode.
|
|
132
|
+
*/
|
|
133
|
+
public void scan(final JSONArray args) {
|
|
134
|
+
|
|
135
|
+
final CordovaPlugin that = this;
|
|
136
|
+
|
|
137
|
+
cordova.getThreadPool().execute(new Runnable() {
|
|
138
|
+
public void run() {
|
|
139
|
+
|
|
140
|
+
Intent intentScan = new Intent(that.cordova.getActivity().getBaseContext(), CaptureActivity.class);
|
|
141
|
+
intentScan.setAction(Intents.Scan.ACTION);
|
|
142
|
+
intentScan.addCategory(Intent.CATEGORY_DEFAULT);
|
|
143
|
+
|
|
144
|
+
// add config as intent extras
|
|
145
|
+
if (args.length() > 0) {
|
|
146
|
+
|
|
147
|
+
JSONObject obj;
|
|
148
|
+
JSONArray names;
|
|
149
|
+
String key;
|
|
150
|
+
Object value;
|
|
151
|
+
|
|
152
|
+
for (int i = 0; i < args.length(); i++) {
|
|
153
|
+
|
|
154
|
+
try {
|
|
155
|
+
obj = args.getJSONObject(i);
|
|
156
|
+
} catch (JSONException e) {
|
|
157
|
+
Log.i("CordovaLog", e.getLocalizedMessage());
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
names = obj.names();
|
|
162
|
+
for (int j = 0; j < names.length(); j++) {
|
|
163
|
+
try {
|
|
164
|
+
key = names.getString(j);
|
|
165
|
+
value = obj.get(key);
|
|
166
|
+
|
|
167
|
+
if (value instanceof Integer) {
|
|
168
|
+
intentScan.putExtra(key, (Integer) value);
|
|
169
|
+
} else if (value instanceof String) {
|
|
170
|
+
intentScan.putExtra(key, (String) value);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
} catch (JSONException e) {
|
|
174
|
+
Log.i("CordovaLog", e.getLocalizedMessage());
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
intentScan.putExtra(Intents.Scan.CAMERA_ID, obj.optBoolean(PREFER_FRONTCAMERA, false) ? 1 : 0);
|
|
179
|
+
intentScan.putExtra(Intents.Scan.SHOW_FLIP_CAMERA_BUTTON, obj.optBoolean(SHOW_FLIP_CAMERA_BUTTON, false));
|
|
180
|
+
intentScan.putExtra(Intents.Scan.SHOW_TORCH_BUTTON, obj.optBoolean(SHOW_TORCH_BUTTON, false));
|
|
181
|
+
intentScan.putExtra(Intents.Scan.TORCH_ON, obj.optBoolean(TORCH_ON, false));
|
|
182
|
+
intentScan.putExtra(Intents.Scan.SAVE_HISTORY, obj.optBoolean(SAVE_HISTORY, false));
|
|
183
|
+
boolean beep = obj.optBoolean(DISABLE_BEEP, false);
|
|
184
|
+
intentScan.putExtra(Intents.Scan.BEEP_ON_SCAN, !beep);
|
|
185
|
+
if (obj.has(RESULTDISPLAY_DURATION)) {
|
|
186
|
+
intentScan.putExtra(Intents.Scan.RESULT_DISPLAY_DURATION_MS, "" + obj.optLong(RESULTDISPLAY_DURATION));
|
|
187
|
+
}
|
|
188
|
+
if (obj.has(FORMATS)) {
|
|
189
|
+
intentScan.putExtra(Intents.Scan.FORMATS, obj.optString(FORMATS));
|
|
190
|
+
}
|
|
191
|
+
if (obj.has(PROMPT)) {
|
|
192
|
+
intentScan.putExtra(Intents.Scan.PROMPT_MESSAGE, obj.optString(PROMPT));
|
|
193
|
+
}
|
|
194
|
+
if (obj.has(ORIENTATION)) {
|
|
195
|
+
intentScan.putExtra(Intents.Scan.ORIENTATION_LOCK, obj.optString(ORIENTATION));
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// avoid calling other phonegap apps
|
|
202
|
+
intentScan.setPackage(that.cordova.getActivity().getApplicationContext().getPackageName());
|
|
203
|
+
|
|
204
|
+
that.cordova.startActivityForResult(that, intentScan, REQUEST_CODE);
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Called when the barcode scanner intent completes.
|
|
211
|
+
*
|
|
212
|
+
* @param requestCode The request code originally supplied to startActivityForResult(),
|
|
213
|
+
* allowing you to identify who this result came from.
|
|
214
|
+
* @param resultCode The integer result code returned by the child activity through its setResult().
|
|
215
|
+
* @param intent An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
|
|
216
|
+
*/
|
|
217
|
+
@Override
|
|
218
|
+
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
|
|
219
|
+
if (requestCode == REQUEST_CODE && this.callbackContext != null) {
|
|
220
|
+
if (resultCode == Activity.RESULT_OK) {
|
|
221
|
+
JSONObject obj = new JSONObject();
|
|
222
|
+
try {
|
|
223
|
+
obj.put(TEXT, intent.getStringExtra("SCAN_RESULT"));
|
|
224
|
+
obj.put(FORMAT, intent.getStringExtra("SCAN_RESULT_FORMAT"));
|
|
225
|
+
obj.put(CANCELLED, false);
|
|
226
|
+
} catch (JSONException e) {
|
|
227
|
+
Log.d(LOG_TAG, "This should never happen");
|
|
228
|
+
}
|
|
229
|
+
//this.success(new PluginResult(PluginResult.Status.OK, obj), this.callback);
|
|
230
|
+
this.callbackContext.success(obj);
|
|
231
|
+
} else if (resultCode == Activity.RESULT_CANCELED) {
|
|
232
|
+
JSONObject obj = new JSONObject();
|
|
233
|
+
try {
|
|
234
|
+
obj.put(TEXT, "");
|
|
235
|
+
obj.put(FORMAT, "");
|
|
236
|
+
obj.put(CANCELLED, true);
|
|
237
|
+
} catch (JSONException e) {
|
|
238
|
+
Log.d(LOG_TAG, "This should never happen");
|
|
239
|
+
}
|
|
240
|
+
//this.success(new PluginResult(PluginResult.Status.OK, obj), this.callback);
|
|
241
|
+
this.callbackContext.success(obj);
|
|
242
|
+
} else {
|
|
243
|
+
//this.error(new PluginResult(PluginResult.Status.ERROR), this.callback);
|
|
244
|
+
this.callbackContext.error("Unexpected error");
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Initiates a barcode encode.
|
|
251
|
+
*
|
|
252
|
+
* @param type Endoiding type.
|
|
253
|
+
* @param data The data to encode in the bar code.
|
|
254
|
+
*/
|
|
255
|
+
public void encode(String type, String data) {
|
|
256
|
+
Intent intentEncode = new Intent(this.cordova.getActivity().getBaseContext(), EncodeActivity.class);
|
|
257
|
+
intentEncode.setAction(Intents.Encode.ACTION);
|
|
258
|
+
intentEncode.putExtra(Intents.Encode.TYPE, type);
|
|
259
|
+
intentEncode.putExtra(Intents.Encode.DATA, data);
|
|
260
|
+
// avoid calling other phonegap apps
|
|
261
|
+
intentEncode.setPackage(this.cordova.getActivity().getApplicationContext().getPackageName());
|
|
262
|
+
|
|
263
|
+
this.cordova.getActivity().startActivity(intentEncode);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* check application's permissions
|
|
268
|
+
*/
|
|
269
|
+
public boolean hasPermisssion() {
|
|
270
|
+
for(String p : permissions)
|
|
271
|
+
{
|
|
272
|
+
if(!PermissionHelper.hasPermission(this, p))
|
|
273
|
+
{
|
|
274
|
+
return false;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
return true;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* We override this so that we can access the permissions variable, which no longer exists in
|
|
282
|
+
* the parent class, since we can't initialize it reliably in the constructor!
|
|
283
|
+
*
|
|
284
|
+
* @param requestCode The code to get request action
|
|
285
|
+
*/
|
|
286
|
+
public void requestPermissions(int requestCode)
|
|
287
|
+
{
|
|
288
|
+
PermissionHelper.requestPermissions(this, requestCode, permissions);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* processes the result of permission request
|
|
293
|
+
*
|
|
294
|
+
* @param requestCode The code to get request action
|
|
295
|
+
* @param permissions The collection of permissions
|
|
296
|
+
* @param grantResults The result of grant
|
|
297
|
+
*/
|
|
298
|
+
public void onRequestPermissionResult(int requestCode, String[] permissions,
|
|
299
|
+
int[] grantResults) throws JSONException
|
|
300
|
+
{
|
|
301
|
+
PluginResult result;
|
|
302
|
+
for (int r : grantResults) {
|
|
303
|
+
if (r == PackageManager.PERMISSION_DENIED) {
|
|
304
|
+
Log.d(LOG_TAG, "Permission Denied!");
|
|
305
|
+
result = new PluginResult(PluginResult.Status.ILLEGAL_ACCESS_EXCEPTION);
|
|
306
|
+
this.callbackContext.sendPluginResult(result);
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
switch(requestCode)
|
|
312
|
+
{
|
|
313
|
+
case 0:
|
|
314
|
+
scan(this.requestArgs);
|
|
315
|
+
break;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* This plugin launches an external Activity when the camera is opened, so we
|
|
321
|
+
* need to implement the save/restore API in case the Activity gets killed
|
|
322
|
+
* by the OS while it's in the background.
|
|
323
|
+
*/
|
|
324
|
+
public void onRestoreStateForActivityResult(Bundle state, CallbackContext callbackContext) {
|
|
325
|
+
this.callbackContext = callbackContext;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
function scan(success, error) {
|
|
2
|
+
var code = window.prompt("Enter barcode value (empty value will fire the error handler):");
|
|
3
|
+
if(code) {
|
|
4
|
+
var result = {
|
|
5
|
+
text:code,
|
|
6
|
+
format:"Fake",
|
|
7
|
+
cancelled:false
|
|
8
|
+
};
|
|
9
|
+
success(result);
|
|
10
|
+
} else {
|
|
11
|
+
error("No barcode");
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function encode(type, data, success, errorCallback) {
|
|
16
|
+
success();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
module.exports = {
|
|
20
|
+
scan: scan,
|
|
21
|
+
encode: encode
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
require("cordova/exec/proxy").add("BarcodeScanner",module.exports);
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|