Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(159)

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/shapedetection/BarcodeDetectionImpl.java

Issue 2512123002: Shape Detection: QR detection in Chrome Android using Play Services (Closed)
Patch Set: dcheng@ request to check for |numPixels| overflow Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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;
63 if (!frameData.isValid() || width <= 0 || height <= 0 || (numPixels / 4) > Long.MAX_VALUE) {
dcheng 2016/12/01 06:15:06 This overflow check isn't quite correct: it should
mcasas 2016/12/01 16:05:51 Done.
64 callback.call(new BarcodeDetectionResult[0]);
65 return;
66 }
67
68 // Mapping |frameData| will fail if the intended mapped size is larger
69 // than its actual capacitywhich is limited by the appropriate
Bernhard Bauer 2016/11/30 23:01:35 Nit: "capacity, which"
mcasas 2016/12/01 16:05:51 Done.
70 // mojo::edk::Configuration entry.
71 ByteBuffer imageBuffer = frameData.map(0, numPixels * 4, MapFlags.none() );
72 if (imageBuffer.capacity() <= 0) {
73 callback.call(new BarcodeDetectionResult[0]);
74 return;
75 }
76
77 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_88 88);
78 bitmap.copyPixelsFromBuffer(imageBuffer);
79
80 Frame frame = null;
81 try {
82 // This constructor implies a pixel format conversion to YUV.
83 frame = new Frame.Builder().setBitmap(bitmap).build();
84 } catch (IllegalArgumentException | IllegalStateException ex) {
85 Log.e(TAG, "Frame.Builder().setBitmap() or build(): " + ex);
86 callback.call(new BarcodeDetectionResult[0]);
87 return;
88 }
89
90 final SparseArray<Barcode> barcodes = mBarcodeDetector.detect(frame);
91
92 BarcodeDetectionResult[] barcodeArray = new BarcodeDetectionResult[barco des.size()];
93 for (int i = 0; i < barcodes.size(); i++) {
94 barcodeArray[i] = new BarcodeDetectionResult();
95 final Barcode barcode = barcodes.valueAt(i);
96 barcodeArray[i].rawValue = barcode.rawValue;
97 final Rect rect = barcode.getBoundingBox();
98 barcodeArray[i].boundingBox = new RectF();
99 barcodeArray[i].boundingBox.x = rect.left;
100 barcodeArray[i].boundingBox.y = rect.top;
101 barcodeArray[i].boundingBox.width = rect.width();
102 barcodeArray[i].boundingBox.height = rect.height();
103 }
104 callback.call(barcodeArray);
105 }
106
107 @Override
108 public void close() {
109 mBarcodeDetector.release();
110 }
111
112 @Override
113 public void onConnectionError(MojoException e) {
114 close();
115 }
116
117 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698