@habibov77/cordova-plugin-mcc-mlkit-barcode-scanner-fixed 1.0.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/README.md +166 -0
- package/package.json +47 -0
- package/plugin.xml +130 -0
- package/src/android/build-extras.gradle +14 -0
- package/src/android/res/assets/beep.ogg +0 -0
- package/src/android/res/drawable/flashlight.png +0 -0
- package/src/android/res/drawable/torch_active.xml +12 -0
- package/src/android/res/drawable/torch_inactive.xml +12 -0
- package/src/android/res/layout/capture_activity.xml +51 -0
- package/src/android/res/values/strings-de.xml +14 -0
- package/src/android/res/values/strings-en.xml +14 -0
- package/src/android/src/CaptureActivity.java +496 -0
- package/src/android/src/MLKitBarcodeScanner.java +183 -0
- package/src/android/src/utils/BitmapUtils.java +286 -0
- package/src/android/src/utils/FrameMetadata.java +69 -0
- package/src/ios/CDViOSScanner.h +21 -0
- package/src/ios/CDViOSScanner.m +197 -0
- package/src/ios/CameraViewController.h +38 -0
- package/src/ios/CameraViewController.m +554 -0
- package/src/ios/Sounds/beep.caf +0 -0
- package/src/ios/de.lproj/Localizable.strings +8 -0
- package/src/ios/en.lproj/Localizable.strings +8 -0
- package/www/BarcodeScanner.contract.js +10 -0
- package/www/BarcodeScanner.plugin.js +133 -0
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2020 Google LLC. All rights reserved.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
package com.mcc.cordova.plugins.mlkit.barcode.scanner.utils;
|
|
18
|
+
|
|
19
|
+
import android.annotation.TargetApi;
|
|
20
|
+
import android.content.ContentResolver;
|
|
21
|
+
import android.graphics.Bitmap;
|
|
22
|
+
import android.graphics.BitmapFactory;
|
|
23
|
+
import android.graphics.ImageFormat;
|
|
24
|
+
import android.graphics.Matrix;
|
|
25
|
+
import android.graphics.Rect;
|
|
26
|
+
import android.graphics.YuvImage;
|
|
27
|
+
import android.media.Image;
|
|
28
|
+
import android.media.Image.Plane;
|
|
29
|
+
import android.net.Uri;
|
|
30
|
+
import android.os.Build.VERSION_CODES;
|
|
31
|
+
import android.provider.MediaStore;
|
|
32
|
+
import android.util.Log;
|
|
33
|
+
|
|
34
|
+
import androidx.annotation.Nullable;
|
|
35
|
+
import androidx.annotation.RequiresApi;
|
|
36
|
+
import androidx.camera.core.ExperimentalGetImage;
|
|
37
|
+
import androidx.camera.core.ImageProxy;
|
|
38
|
+
import androidx.exifinterface.media.ExifInterface;
|
|
39
|
+
|
|
40
|
+
import java.io.ByteArrayOutputStream;
|
|
41
|
+
import java.io.IOException;
|
|
42
|
+
import java.io.InputStream;
|
|
43
|
+
import java.nio.ByteBuffer;
|
|
44
|
+
|
|
45
|
+
/** Utils functions for bitmap conversions. */
|
|
46
|
+
public class BitmapUtils {
|
|
47
|
+
private static final String TAG = "BitmapUtils";
|
|
48
|
+
|
|
49
|
+
/** Converts NV21 format byte buffer to bitmap. */
|
|
50
|
+
@Nullable
|
|
51
|
+
public static Bitmap getBitmap(ByteBuffer data, FrameMetadata metadata) {
|
|
52
|
+
data.rewind();
|
|
53
|
+
byte[] imageInBuffer = new byte[data.limit()];
|
|
54
|
+
data.get(imageInBuffer, 0, imageInBuffer.length);
|
|
55
|
+
try {
|
|
56
|
+
YuvImage image = new YuvImage(imageInBuffer, ImageFormat.NV21, metadata.getWidth(), metadata.getHeight(), null);
|
|
57
|
+
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
|
58
|
+
image.compressToJpeg(new Rect(0, 0, metadata.getWidth(), metadata.getHeight()), 80, stream);
|
|
59
|
+
|
|
60
|
+
Bitmap bmp = BitmapFactory.decodeByteArray(stream.toByteArray(), 0, stream.size());
|
|
61
|
+
|
|
62
|
+
stream.close();
|
|
63
|
+
return rotateBitmap(bmp, metadata.getRotation(), false, false);
|
|
64
|
+
} catch (Exception e) {
|
|
65
|
+
Log.e("VisionProcessorBase", "Error: " + e.getMessage());
|
|
66
|
+
}
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Converts a YUV_420_888 image from CameraX API to a bitmap. */
|
|
71
|
+
@RequiresApi(VERSION_CODES.KITKAT)
|
|
72
|
+
@Nullable
|
|
73
|
+
@ExperimentalGetImage
|
|
74
|
+
public static Bitmap getBitmap(ImageProxy image) {
|
|
75
|
+
FrameMetadata frameMetadata = new FrameMetadata.Builder().setWidth(image.getWidth()).setHeight(image.getHeight())
|
|
76
|
+
.setRotation(image.getImageInfo().getRotationDegrees()).build();
|
|
77
|
+
|
|
78
|
+
ByteBuffer nv21Buffer = yuv420ThreePlanesToNV21(image.getImage().getPlanes(), image.getWidth(), image.getHeight());
|
|
79
|
+
return getBitmap(nv21Buffer, frameMetadata);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Rotates a bitmap if it is converted from a bytebuffer. */
|
|
83
|
+
private static Bitmap rotateBitmap(Bitmap bitmap, int rotationDegrees, boolean flipX, boolean flipY) {
|
|
84
|
+
Matrix matrix = new Matrix();
|
|
85
|
+
|
|
86
|
+
// Rotate the image back to straight.
|
|
87
|
+
matrix.postRotate(rotationDegrees);
|
|
88
|
+
|
|
89
|
+
// Mirror the image along the X or Y axis.
|
|
90
|
+
matrix.postScale(flipX ? -1.0f : 1.0f, flipY ? -1.0f : 1.0f);
|
|
91
|
+
Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
|
|
92
|
+
|
|
93
|
+
// Recycle the old bitmap if it has changed.
|
|
94
|
+
if (rotatedBitmap != bitmap) {
|
|
95
|
+
bitmap.recycle();
|
|
96
|
+
}
|
|
97
|
+
return rotatedBitmap;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
@Nullable
|
|
101
|
+
public static Bitmap getBitmapFromContentUri(ContentResolver contentResolver, Uri imageUri) throws IOException {
|
|
102
|
+
Bitmap decodedBitmap = MediaStore.Images.Media.getBitmap(contentResolver, imageUri);
|
|
103
|
+
if (decodedBitmap == null) {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
int orientation = getExifOrientationTag(contentResolver, imageUri);
|
|
107
|
+
|
|
108
|
+
int rotationDegrees = 0;
|
|
109
|
+
boolean flipX = false;
|
|
110
|
+
boolean flipY = false;
|
|
111
|
+
// See e.g. https://magnushoff.com/articles/jpeg-orientation/ for a detailed
|
|
112
|
+
// explanation on each
|
|
113
|
+
// orientation.
|
|
114
|
+
switch (orientation) {
|
|
115
|
+
case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
|
|
116
|
+
flipX = true;
|
|
117
|
+
break;
|
|
118
|
+
case ExifInterface.ORIENTATION_ROTATE_90:
|
|
119
|
+
rotationDegrees = 90;
|
|
120
|
+
break;
|
|
121
|
+
case ExifInterface.ORIENTATION_TRANSPOSE:
|
|
122
|
+
rotationDegrees = 90;
|
|
123
|
+
flipX = true;
|
|
124
|
+
break;
|
|
125
|
+
case ExifInterface.ORIENTATION_ROTATE_180:
|
|
126
|
+
rotationDegrees = 180;
|
|
127
|
+
break;
|
|
128
|
+
case ExifInterface.ORIENTATION_FLIP_VERTICAL:
|
|
129
|
+
flipY = true;
|
|
130
|
+
break;
|
|
131
|
+
case ExifInterface.ORIENTATION_ROTATE_270:
|
|
132
|
+
rotationDegrees = -90;
|
|
133
|
+
break;
|
|
134
|
+
case ExifInterface.ORIENTATION_TRANSVERSE:
|
|
135
|
+
rotationDegrees = -90;
|
|
136
|
+
flipX = true;
|
|
137
|
+
break;
|
|
138
|
+
case ExifInterface.ORIENTATION_UNDEFINED:
|
|
139
|
+
case ExifInterface.ORIENTATION_NORMAL:
|
|
140
|
+
default:
|
|
141
|
+
// No transformations necessary in this case.
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return rotateBitmap(decodedBitmap, rotationDegrees, flipX, flipY);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
private static int getExifOrientationTag(ContentResolver resolver, Uri imageUri) {
|
|
148
|
+
// We only support parsing EXIF orientation tag from local file on the device.
|
|
149
|
+
// See also:
|
|
150
|
+
// https://android-developers.googleblog.com/2016/12/introducing-the-exifinterface-support-library.html
|
|
151
|
+
if (!ContentResolver.SCHEME_CONTENT.equals(imageUri.getScheme())
|
|
152
|
+
&& !ContentResolver.SCHEME_FILE.equals(imageUri.getScheme())) {
|
|
153
|
+
return 0;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
ExifInterface exif;
|
|
157
|
+
try (InputStream inputStream = resolver.openInputStream(imageUri)) {
|
|
158
|
+
if (inputStream == null) {
|
|
159
|
+
return 0;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
exif = new ExifInterface(inputStream);
|
|
163
|
+
} catch (IOException e) {
|
|
164
|
+
Log.e(TAG, "failed to open file to read rotation meta data: " + imageUri, e);
|
|
165
|
+
return 0;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Converts YUV_420_888 to NV21 bytebuffer.
|
|
173
|
+
*
|
|
174
|
+
* <p>
|
|
175
|
+
* The NV21 format consists of a single byte array containing the Y, U and V
|
|
176
|
+
* values. For an image of size S, the first S positions of the array contain
|
|
177
|
+
* all the Y values. The remaining positions contain interleaved V and U values.
|
|
178
|
+
* U and V are subsampled by a factor of 2 in both dimensions, so there are S/4
|
|
179
|
+
* U values and S/4 V values. In summary, the NV21 array will contain S Y values
|
|
180
|
+
* followed by S/4 VU values: YYYYYYYYYYYYYY(...)YVUVUVUVU(...)VU
|
|
181
|
+
*
|
|
182
|
+
* <p>
|
|
183
|
+
* YUV_420_888 is a generic format that can describe any YUV image where U and V
|
|
184
|
+
* are subsampled by a factor of 2 in both dimensions. {@link Image#getPlanes}
|
|
185
|
+
* returns an array with the Y, U and V planes. The Y plane is guaranteed not to
|
|
186
|
+
* be interleaved, so we can just copy its values into the first part of the
|
|
187
|
+
* NV21 array. The U and V planes may already have the representation in the
|
|
188
|
+
* NV21 format. This happens if the planes share the same buffer, the V buffer
|
|
189
|
+
* is one position before the U buffer and the planes have a pixelStride of 2.
|
|
190
|
+
* If this is case, we can just copy them to the NV21 array.
|
|
191
|
+
*/
|
|
192
|
+
@RequiresApi(VERSION_CODES.KITKAT)
|
|
193
|
+
private static ByteBuffer yuv420ThreePlanesToNV21(Plane[] yuv420888planes, int width, int height) {
|
|
194
|
+
int imageSize = width * height;
|
|
195
|
+
byte[] out = new byte[imageSize + 2 * (imageSize / 4)];
|
|
196
|
+
|
|
197
|
+
if (areUVPlanesNV21(yuv420888planes, width, height)) {
|
|
198
|
+
// Copy the Y values.
|
|
199
|
+
yuv420888planes[0].getBuffer().get(out, 0, imageSize);
|
|
200
|
+
|
|
201
|
+
ByteBuffer uBuffer = yuv420888planes[1].getBuffer();
|
|
202
|
+
ByteBuffer vBuffer = yuv420888planes[2].getBuffer();
|
|
203
|
+
// Get the first V value from the V buffer, since the U buffer does not contain
|
|
204
|
+
// it.
|
|
205
|
+
vBuffer.get(out, imageSize, 1);
|
|
206
|
+
// Copy the first U value and the remaining VU values from the U buffer.
|
|
207
|
+
uBuffer.get(out, imageSize + 1, 2 * imageSize / 4 - 1);
|
|
208
|
+
} else {
|
|
209
|
+
// Fallback to copying the UV values one by one, which is slower but also works.
|
|
210
|
+
// Unpack Y.
|
|
211
|
+
unpackPlane(yuv420888planes[0], width, height, out, 0, 1);
|
|
212
|
+
// Unpack U.
|
|
213
|
+
unpackPlane(yuv420888planes[1], width, height, out, imageSize + 1, 2);
|
|
214
|
+
// Unpack V.
|
|
215
|
+
unpackPlane(yuv420888planes[2], width, height, out, imageSize, 2);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return ByteBuffer.wrap(out);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Checks if the UV plane buffers of a YUV_420_888 image are in the NV21 format.
|
|
223
|
+
*/
|
|
224
|
+
@RequiresApi(VERSION_CODES.KITKAT)
|
|
225
|
+
private static boolean areUVPlanesNV21(Plane[] planes, int width, int height) {
|
|
226
|
+
int imageSize = width * height;
|
|
227
|
+
|
|
228
|
+
ByteBuffer uBuffer = planes[1].getBuffer();
|
|
229
|
+
ByteBuffer vBuffer = planes[2].getBuffer();
|
|
230
|
+
|
|
231
|
+
// Backup buffer properties.
|
|
232
|
+
int vBufferPosition = vBuffer.position();
|
|
233
|
+
int uBufferLimit = uBuffer.limit();
|
|
234
|
+
|
|
235
|
+
// Advance the V buffer by 1 byte, since the U buffer will not contain the first
|
|
236
|
+
// V value.
|
|
237
|
+
vBuffer.position(vBufferPosition + 1);
|
|
238
|
+
// Chop off the last byte of the U buffer, since the V buffer will not contain
|
|
239
|
+
// the last U value.
|
|
240
|
+
uBuffer.limit(uBufferLimit - 1);
|
|
241
|
+
|
|
242
|
+
// Check that the buffers are equal and have the expected number of elements.
|
|
243
|
+
boolean areNV21 = (vBuffer.remaining() == (2 * imageSize / 4 - 2)) && (vBuffer.compareTo(uBuffer) == 0);
|
|
244
|
+
|
|
245
|
+
// Restore buffers to their initial state.
|
|
246
|
+
vBuffer.position(vBufferPosition);
|
|
247
|
+
uBuffer.limit(uBufferLimit);
|
|
248
|
+
|
|
249
|
+
return areNV21;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Unpack an image plane into a byte array.
|
|
254
|
+
*
|
|
255
|
+
* The input plane data will be copied in 'out', starting at 'offset' and every
|
|
256
|
+
* pixel will be spaced by 'pixelStride'. Note that there is no row padding on
|
|
257
|
+
* the output.
|
|
258
|
+
*/
|
|
259
|
+
@TargetApi(VERSION_CODES.KITKAT)
|
|
260
|
+
private static void unpackPlane(Plane plane, int width, int height, byte[] out, int offset, int pixelStride) {
|
|
261
|
+
ByteBuffer buffer = plane.getBuffer();
|
|
262
|
+
buffer.rewind();
|
|
263
|
+
|
|
264
|
+
// Compute the size of the current plane.
|
|
265
|
+
// We assume that it has the aspect ratio as the original image.
|
|
266
|
+
int numRow = (buffer.limit() + plane.getRowStride() - 1) / plane.getRowStride();
|
|
267
|
+
if (numRow == 0) {
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
int scaleFactor = height / numRow;
|
|
271
|
+
int numCol = width / scaleFactor;
|
|
272
|
+
|
|
273
|
+
// Extract the data in the output buffer.
|
|
274
|
+
int outputPos = offset;
|
|
275
|
+
int rowStart = 0;
|
|
276
|
+
for (int row = 0; row < numRow; row++) {
|
|
277
|
+
int inputPos = rowStart;
|
|
278
|
+
for (int col = 0; col < numCol; col++) {
|
|
279
|
+
out[outputPos] = buffer.get(inputPos);
|
|
280
|
+
outputPos += pixelStride;
|
|
281
|
+
inputPos += plane.getPixelStride();
|
|
282
|
+
}
|
|
283
|
+
rowStart += plane.getRowStride();
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2020 Google LLC. All rights reserved.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
package com.mcc.cordova.plugins.mlkit.barcode.scanner.utils;
|
|
18
|
+
|
|
19
|
+
/** Describing a frame info. */
|
|
20
|
+
public class FrameMetadata {
|
|
21
|
+
|
|
22
|
+
private final int width;
|
|
23
|
+
private final int height;
|
|
24
|
+
private final int rotation;
|
|
25
|
+
|
|
26
|
+
public int getWidth() {
|
|
27
|
+
return width;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
public int getHeight() {
|
|
31
|
+
return height;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public int getRotation() {
|
|
35
|
+
return rotation;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
private FrameMetadata(int width, int height, int rotation) {
|
|
39
|
+
this.width = width;
|
|
40
|
+
this.height = height;
|
|
41
|
+
this.rotation = rotation;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
public static class Builder {
|
|
45
|
+
|
|
46
|
+
private int width;
|
|
47
|
+
private int height;
|
|
48
|
+
private int rotation;
|
|
49
|
+
|
|
50
|
+
public Builder setWidth(int width) {
|
|
51
|
+
this.width = width;
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
public Builder setHeight(int height) {
|
|
56
|
+
this.height = height;
|
|
57
|
+
return this;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
public Builder setRotation(int rotation) {
|
|
61
|
+
this.rotation = rotation;
|
|
62
|
+
return this;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
public FrameMetadata build() {
|
|
66
|
+
return new FrameMetadata(width, height, rotation);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
#import <AVFoundation/AVFoundation.h>
|
|
2
|
+
#import <Foundation/Foundation.h>
|
|
3
|
+
#import <Cordova/CDV.h>
|
|
4
|
+
#import <UIKit/UIKit.h>
|
|
5
|
+
#import "CameraViewController.h"
|
|
6
|
+
|
|
7
|
+
@class UIViewController;
|
|
8
|
+
|
|
9
|
+
@interface CDViOSScanner : CDVPlugin {
|
|
10
|
+
NSString *_callback;
|
|
11
|
+
Boolean _scannerOpen;
|
|
12
|
+
AVAudioPlayer* _player;
|
|
13
|
+
Boolean _beepOnSuccess;
|
|
14
|
+
Boolean _vibrateOnSuccess;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
@property (nonatomic, retain) CameraViewController* cameraViewController;
|
|
18
|
+
|
|
19
|
+
- (void) startScan:(CDVInvokedUrlCommand *)command;
|
|
20
|
+
|
|
21
|
+
@end
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
@import MLKitBarcodeScanning;
|
|
2
|
+
|
|
3
|
+
#import "CDViOSScanner.h"
|
|
4
|
+
|
|
5
|
+
@class UIViewController;
|
|
6
|
+
|
|
7
|
+
@interface CDViOSScanner ()
|
|
8
|
+
{
|
|
9
|
+
NSInteger _previousStatusBarStyle;
|
|
10
|
+
UIInterfaceOrientation _previousOrientation;
|
|
11
|
+
}
|
|
12
|
+
@end
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@implementation CDViOSScanner
|
|
16
|
+
|
|
17
|
+
- (void)pluginInitialize
|
|
18
|
+
{
|
|
19
|
+
_previousStatusBarStyle = -1;
|
|
20
|
+
_previousOrientation = UIInterfaceOrientationUnknown;
|
|
21
|
+
NSString *beepSoundPath = [[NSBundle mainBundle] pathForResource:@"beep" ofType:@"caf"];
|
|
22
|
+
NSURL *beepSoundUrl = [NSURL fileURLWithPath:beepSoundPath];
|
|
23
|
+
self->_player = [[AVAudioPlayer alloc] initWithContentsOfURL:beepSoundUrl
|
|
24
|
+
error:nil];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
- (void)startScan:(CDVInvokedUrlCommand *)command
|
|
28
|
+
{
|
|
29
|
+
_previousOrientation = [[UIApplication sharedApplication] statusBarOrientation];
|
|
30
|
+
|
|
31
|
+
BOOL hasCamera = [UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera];
|
|
32
|
+
|
|
33
|
+
if (hasCamera)
|
|
34
|
+
{
|
|
35
|
+
//Force portrait orientation.
|
|
36
|
+
[[UIDevice currentDevice] setValue: [NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:@"orientation"];
|
|
37
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
38
|
+
NSLog(@"Arguments %@", command.arguments);
|
|
39
|
+
if (self->_scannerOpen == YES)
|
|
40
|
+
{
|
|
41
|
+
//Scanner is currently open, throw error.
|
|
42
|
+
NSArray *response = @[@"SCANNER_OPEN", @"", @""];
|
|
43
|
+
CDVPluginResult *pluginResult=[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsArray:response];
|
|
44
|
+
|
|
45
|
+
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
|
46
|
+
}
|
|
47
|
+
else
|
|
48
|
+
{
|
|
49
|
+
//Open scanner.
|
|
50
|
+
self->_scannerOpen = YES;
|
|
51
|
+
self.cameraViewController = [[CameraViewController alloc] init];
|
|
52
|
+
self.cameraViewController.delegate = self;
|
|
53
|
+
|
|
54
|
+
//Provide settings to the camera view.
|
|
55
|
+
NSNumberFormatter* f = [[NSNumberFormatter alloc] init];
|
|
56
|
+
f.numberStyle = NSNumberFormatterDecimalStyle;
|
|
57
|
+
NSDictionary* config = [command.arguments objectAtIndex:0];
|
|
58
|
+
self->_beepOnSuccess = [[config valueForKey:@"beepOnSuccess"] boolValue] ?: NO;
|
|
59
|
+
self->_vibrateOnSuccess = [[config valueForKey:@"vibrateOnSuccess"] boolValue] ?: NO;
|
|
60
|
+
NSNumber* barcodeFormats = [config valueForKey:@"barcodeFormats"] ?: @1234;
|
|
61
|
+
self.cameraViewController.barcodeFormats = barcodeFormats;
|
|
62
|
+
self.cameraViewController.detectorSize = (CGFloat)[[config valueForKey:@"detectorSize"] ?: @0.5 floatValue];
|
|
63
|
+
NSNumber* timeoutMs = [config valueForKey:@"timeoutMs"];
|
|
64
|
+
self.cameraViewController.timeoutMs = timeoutMs != nil ? [timeoutMs integerValue] : 0;
|
|
65
|
+
self.cameraViewController.modalPresentationStyle = UIModalPresentationFullScreen;
|
|
66
|
+
|
|
67
|
+
NSLog(@"scanAreaSize: %f, barcodeFormats: %@", self.cameraViewController.detectorSize, self.cameraViewController.barcodeFormats);
|
|
68
|
+
|
|
69
|
+
[self.viewController presentViewController:self.cameraViewController animated: NO completion:nil];
|
|
70
|
+
self->_callback = command.callbackId;
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
else
|
|
75
|
+
{
|
|
76
|
+
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:NSLocalizedString(@"The device has no camera.", @"Message to the user if the device has no camera.") preferredStyle:UIAlertControllerStyleAlert];
|
|
77
|
+
UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
|
|
78
|
+
[alert addAction:defaultAction];
|
|
79
|
+
|
|
80
|
+
[self.viewController presentViewController:alert animated:YES completion:nil];
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
- (void)sendResult:(MLKBarcode *)barcode
|
|
85
|
+
{
|
|
86
|
+
[self.cameraViewController dismissViewControllerAnimated:NO completion:nil];
|
|
87
|
+
_scannerOpen = NO;
|
|
88
|
+
|
|
89
|
+
NSString* value = barcode.rawValue;
|
|
90
|
+
|
|
91
|
+
// rawValue returns null if string is not UTF-8 encoded.
|
|
92
|
+
// If that's the case, we will decode it as ASCII,
|
|
93
|
+
// because it's the most common encoding for barcodes.
|
|
94
|
+
// e.g. https://www.barcodefaq.com/1d/code-128/
|
|
95
|
+
if(barcode.rawValue == nil)
|
|
96
|
+
{
|
|
97
|
+
value = [[NSString alloc] initWithData:barcode.rawData encoding:NSASCIIStringEncoding];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
NSArray* response = @[value, @(barcode.format), @(barcode.valueType)];
|
|
101
|
+
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:response];
|
|
102
|
+
|
|
103
|
+
[self playBeep];
|
|
104
|
+
|
|
105
|
+
[self resetOrientation];
|
|
106
|
+
[self.commandDelegate sendPluginResult:pluginResult callbackId:_callback];
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
- (void)playBeep
|
|
110
|
+
{
|
|
111
|
+
if (self->_beepOnSuccess)
|
|
112
|
+
{
|
|
113
|
+
[self->_player prepareToPlay];
|
|
114
|
+
[self->_player play];
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (self->_vibrateOnSuccess)
|
|
118
|
+
{
|
|
119
|
+
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
- (void)closeScanner
|
|
124
|
+
{
|
|
125
|
+
[self.cameraViewController dismissViewControllerAnimated:NO completion:nil];
|
|
126
|
+
_scannerOpen = NO;
|
|
127
|
+
|
|
128
|
+
NSArray *response = @[@"USER_CANCELLED", @"", @""];
|
|
129
|
+
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsArray:response];
|
|
130
|
+
|
|
131
|
+
[self resetOrientation];
|
|
132
|
+
[self.commandDelegate sendPluginResult:pluginResult callbackId:_callback];
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
- (void)sendScanTimeout
|
|
136
|
+
{
|
|
137
|
+
[self.cameraViewController dismissViewControllerAnimated:NO completion:nil];
|
|
138
|
+
_scannerOpen = NO;
|
|
139
|
+
|
|
140
|
+
NSArray *response = @[@"SCAN_TIMEOUT", @"", @""];
|
|
141
|
+
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsArray:response];
|
|
142
|
+
|
|
143
|
+
[self resetOrientation];
|
|
144
|
+
[self.commandDelegate sendPluginResult:pluginResult callbackId:_callback];
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
- (void)resetOrientation
|
|
148
|
+
{
|
|
149
|
+
if (_previousOrientation != UIInterfaceOrientationUnknown && _previousOrientation != UIInterfaceOrientationPortrait)
|
|
150
|
+
{
|
|
151
|
+
[[UIDevice currentDevice] setValue: [NSNumber numberWithInteger: _previousOrientation] forKey:@"orientation"];
|
|
152
|
+
NSLog(@"Changing device orientation to previous orientation");
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
- (void)show:(CDVInvokedUrlCommand*)command
|
|
158
|
+
{
|
|
159
|
+
if (self.cameraViewController == nil)
|
|
160
|
+
{
|
|
161
|
+
NSLog(@"Tried to show scanner after it was closed.");
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (_previousStatusBarStyle != -1)
|
|
166
|
+
{
|
|
167
|
+
NSLog(@"Tried to show scanner while already shown");
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
_previousStatusBarStyle = [UIApplication sharedApplication].statusBarStyle;
|
|
172
|
+
_previousOrientation = [[UIApplication sharedApplication] statusBarOrientation];
|
|
173
|
+
|
|
174
|
+
__block UINavigationController* nav = [[UINavigationController alloc]
|
|
175
|
+
initWithRootViewController:self.cameraViewController];
|
|
176
|
+
|
|
177
|
+
nav.navigationBarHidden = YES;
|
|
178
|
+
nav.modalPresentationStyle = UIModalPresentationFullScreen;
|
|
179
|
+
|
|
180
|
+
__weak CDViOSScanner* weakSelf = self;
|
|
181
|
+
|
|
182
|
+
// Run later to avoid the "took a long time" log message.
|
|
183
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
184
|
+
if (weakSelf.cameraViewController != nil)
|
|
185
|
+
{
|
|
186
|
+
CGRect frame = [[UIScreen mainScreen] bounds];
|
|
187
|
+
UIWindow* tmpWindow = [[UIWindow alloc] initWithFrame:frame];
|
|
188
|
+
UIViewController* tmpController = [[UIViewController alloc] init];
|
|
189
|
+
[tmpWindow setRootViewController:tmpController];
|
|
190
|
+
[tmpWindow setWindowLevel:UIWindowLevelNormal];
|
|
191
|
+
[tmpWindow makeKeyAndVisible];
|
|
192
|
+
[tmpController presentViewController:nav animated:NO completion:nil];
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
@end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2016-present Google Inc. All Rights Reserved.
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
@import MLKitBarcodeScanning;
|
|
18
|
+
@import UIKit;
|
|
19
|
+
|
|
20
|
+
// View controller demonstraing how to use the barcode detector with the AVFoundation
|
|
21
|
+
// video pipeline.
|
|
22
|
+
@protocol senddataProtocol <NSObject>
|
|
23
|
+
|
|
24
|
+
- (void)closeScanner;
|
|
25
|
+
- (void)sendResult:(MLKBarcode *)result;
|
|
26
|
+
- (void)sendScanTimeout;
|
|
27
|
+
|
|
28
|
+
@end
|
|
29
|
+
|
|
30
|
+
@interface CameraViewController : UIViewController
|
|
31
|
+
|
|
32
|
+
@property(nonatomic,assign)id delegate;
|
|
33
|
+
@property(nonatomic,assign) NSNumber *barcodeFormats;
|
|
34
|
+
@property(nonatomic,assign) CGFloat detectorSize;
|
|
35
|
+
@property(nonatomic,assign) NSInteger timeoutMs;
|
|
36
|
+
|
|
37
|
+
@end
|
|
38
|
+
|