OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.chrome.browser.shapedetection; | |
6 | |
7 import android.content.Context; | |
8 import android.graphics.Bitmap; | |
9 import android.graphics.Rect; | |
10 import android.util.SparseArray; | |
11 | |
12 import com.google.android.gms.vision.Frame; | |
13 import com.google.android.gms.vision.barcode.Barcode; | |
14 import com.google.android.gms.vision.barcode.BarcodeDetector; | |
15 | |
16 import org.chromium.base.Log; | |
17 import org.chromium.blink.mojom.BarcodeDetection; | |
18 import org.chromium.blink.mojom.BarcodeDetectionResult; | |
19 import org.chromium.chrome.browser.externalauth.ExternalAuthUtils; | |
20 import org.chromium.chrome.browser.externalauth.UserRecoverableErrorHandler; | |
21 import org.chromium.gfx.mojom.RectF; | |
22 import org.chromium.mojo.system.MojoException; | |
23 import org.chromium.mojo.system.SharedBufferHandle; | |
24 import org.chromium.mojo.system.SharedBufferHandle.MapFlags; | |
25 | |
26 import java.nio.ByteBuffer; | |
27 | |
28 /** | |
29 * Implementation of mojo BarcodeDetection, using Google Play Services vision pa ckage. | |
30 */ | |
31 public class BarcodeDetectionImpl implements BarcodeDetection { | |
32 private static final String TAG = "BarcodeDetectionImpl"; | |
33 | |
34 private final Context mContext; | |
35 private BarcodeDetector mBarcodeDetector; | |
36 | |
37 public BarcodeDetectionImpl(Context context) { | |
38 Log.d(TAG, "BarcodeDetectionImpl ctor()"); | |
39 mContext = context; | |
40 mBarcodeDetector = new BarcodeDetector.Builder(mContext).build(); | |
41 } | |
42 | |
43 @Override | |
44 public void detect( | |
45 SharedBufferHandle frameData, int width, int height, DetectResponse callback) { | |
46 if (!ExternalAuthUtils.getInstance().canUseGooglePlayServices( | |
47 mContext, new UserRecoverableErrorHandler.Silent())) { | |
48 Log.e(TAG, "Google Play Services not available"); | |
49 callback.call(new BarcodeDetectionResult[0]); | |
50 return; | |
51 } | |
52 // The vision library will be downloaded the first time the API is used | |
53 // on the device; this happens "fast", but it might have not completed, | |
54 // bail in this case. Also, the API was disabled between and v.9.0 and | |
55 // v.9.2, see https://developers.google.com/android/guides/releases. | |
56 if (!mBarcodeDetector.isOperational()) { | |
57 Log.e(TAG, "BarcodeDetector is not operational"); | |
58 callback.call(new BarcodeDetectionResult[0]); | |
59 return; | |
60 } | |
61 | |
62 final long numPixels = (long) width * height; | |
Bernhard Bauer
2016/11/30 17:09:45
Are we doing any additional sanity checks of the v
mcasas
2016/11/30 19:57:09
The input |frameData| is a shared memory, so
it ne
Bernhard Bauer
2016/11/30 23:01:34
I'm actually less concerned about the mapping fail
mcasas
2016/12/01 16:05:51
This is interesting and sad information.
For the
dcheng
2016/12/01 22:10:46
As a counter point... a number of security team me
| |
63 if (!frameData.isValid() || width <= 0 || height <= 0) { | |
64 callback.call(new BarcodeDetectionResult[0]); | |
65 return; | |
66 } | |
67 | |
68 ByteBuffer imageBuffer = frameData.map(0, numPixels * 4, MapFlags.none() ); | |
69 if (imageBuffer.capacity() <= 0) { | |
70 callback.call(new BarcodeDetectionResult[0]); | |
71 return; | |
72 } | |
73 | |
74 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_88 88); | |
75 bitmap.copyPixelsFromBuffer(imageBuffer); | |
76 | |
77 Frame frame = null; | |
78 try { | |
79 // This constructor implies a pixel format conversion to YUV. | |
80 frame = new Frame.Builder().setBitmap(bitmap).build(); | |
81 } catch (IllegalArgumentException | IllegalStateException ex) { | |
82 Log.e(TAG, "Frame.Builder().setBitmap() or build(): " + ex); | |
83 callback.call(new BarcodeDetectionResult[0]); | |
84 return; | |
85 } | |
86 | |
87 final SparseArray<Barcode> barcodes = mBarcodeDetector.detect(frame); | |
88 | |
89 BarcodeDetectionResult[] barcodeArray = new BarcodeDetectionResult[barco des.size()]; | |
90 for (int i = 0; i < barcodes.size(); i++) { | |
91 barcodeArray[i] = new BarcodeDetectionResult(); | |
92 final Barcode barcode = barcodes.valueAt(i); | |
93 barcodeArray[i].rawValue = barcode.rawValue; | |
94 final Rect rect = barcode.getBoundingBox(); | |
95 barcodeArray[i].boundingBox = new RectF(); | |
96 barcodeArray[i].boundingBox.x = rect.left; | |
97 barcodeArray[i].boundingBox.y = rect.top; | |
98 barcodeArray[i].boundingBox.width = rect.width(); | |
99 barcodeArray[i].boundingBox.height = rect.height(); | |
100 } | |
101 callback.call(barcodeArray); | |
102 } | |
103 | |
104 @Override | |
105 public void close() { | |
106 mBarcodeDetector.release(); | |
107 } | |
108 | |
109 @Override | |
110 public void onConnectionError(MojoException e) { | |
111 close(); | |
112 } | |
113 | |
114 } | |
OLD | NEW |