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

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: 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.common.ConnectionResult;
13 import com.google.android.gms.common.GoogleApiAvailability;
14 import com.google.android.gms.vision.Frame;
15 import com.google.android.gms.vision.barcode.Barcode;
16 import com.google.android.gms.vision.barcode.BarcodeDetector;
17
18 import org.chromium.base.Log;
19 import org.chromium.blink.mojom.BarcodeDetection;
20 import org.chromium.blink.mojom.BarcodeDetectionResult;
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 // https://cs.chromium.org/chromium/src/mojo/edk/system/broker_host.cc?l=24
35 private static final int MOJO_SHAREDBUFFER_MAX_BYTES = 16 * 1024 * 1024;
36
37 private final Context mContext;
38 private BarcodeDetector mBarcodeDetector;
39
40 public BarcodeDetectionImpl(Context context) {
41 Log.d(TAG, "BarcodeDetectionImpl ctor()");
42 mContext = context;
43 mBarcodeDetector = new BarcodeDetector.Builder(mContext).build();
44 }
45
46 @Override
47 public void detect(
48 SharedBufferHandle frameData, int width, int height, DetectResponse callback) {
49 if (GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(mC ontext)
dgn 2016/11/24 10:32:32 Consider using ExternalAuthUtils#canUseGooglePlayS
mcasas 2016/11/28 22:03:37 Done.
50 != ConnectionResult.SUCCESS) {
51 Log.e(TAG, "Google Play Services not available");
52 callback.call(new BarcodeDetectionResult[0]);
53 return;
54 }
55 // The vision library will be downloaded the first time the API is used
56 // on the device; this happens "fast", but it might have not completed,
57 // bail in this case. Also, the API was disabled between and v.9.0 and
58 // v.9.2, see https://developers.google.com/android/guides/releases.
59 if (!mBarcodeDetector.isOperational()) {
60 Log.e(TAG, "BarcodeDetector is not operational");
61 callback.call(new BarcodeDetectionResult[0]);
62 return;
63 }
64
65 final long numPixels = (long) width * height;
66 if (!frameData.isValid() || width <= 0 || height <= 0
67 || numPixels > MOJO_SHAREDBUFFER_MAX_BYTES / 4) {
dcheng 2016/11/24 04:31:39 Drive-by: this number can (and has) changed over t
mcasas 2016/11/28 22:03:37 Done.
68 callback.call(new BarcodeDetectionResult[0]);
69 return;
70 }
71
72 ByteBuffer imageBuffer = frameData.map(0, numPixels * 4, MapFlags.none() );
73 if (imageBuffer.capacity() <= 0) {
74 callback.call(new BarcodeDetectionResult[0]);
75 return;
76 }
77
78 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_88 88);
79 bitmap.copyPixelsFromBuffer(imageBuffer);
80
81 Frame frame = null;
82 try {
83 // This constructor implies a pixel format conversion to YUV.
84 frame = new Frame.Builder().setBitmap(bitmap).build();
85 } catch (IllegalArgumentException | IllegalStateException ex) {
86 Log.e(TAG, "Frame.Builder().setBitmap() or build(): " + ex);
87 callback.call(new BarcodeDetectionResult[0]);
88 return;
89 }
90
91 final SparseArray<Barcode> barcodes = mBarcodeDetector.detect(frame);
92
93 BarcodeDetectionResult[] barcodeArray = new BarcodeDetectionResult[barco des.size()];
94 for (int i = 0; i < barcodes.size(); i++) {
95 barcodeArray[i] = new BarcodeDetectionResult();
96 final Barcode barcode = barcodes.valueAt(i);
97 barcodeArray[i].rawValue = barcode.rawValue;
98 final Rect rect = barcode.getBoundingBox();
99 barcodeArray[i].boundingBox = new RectF();
100 barcodeArray[i].boundingBox.x = rect.left;
101 barcodeArray[i].boundingBox.y = rect.top;
102 barcodeArray[i].boundingBox.width = rect.width();
103 barcodeArray[i].boundingBox.height = rect.height();
104 }
105 callback.call(barcodeArray);
106 }
107
108 @Override
109 public void close() {
110 mBarcodeDetector.release();
111 }
112
113 @Override
114 public void onConnectionError(MojoException e) {
115 close();
116 }
117
118 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698