Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/shapedetection/BarcodeDetectionImpl.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/shapedetection/BarcodeDetectionImpl.java b/chrome/android/java/src/org/chromium/chrome/browser/shapedetection/BarcodeDetectionImpl.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b706393b99f600b2fc3637e75e7b2de766b8494f |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/shapedetection/BarcodeDetectionImpl.java |
| @@ -0,0 +1,118 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser.shapedetection; |
| + |
| +import android.content.Context; |
| +import android.graphics.Bitmap; |
| +import android.graphics.Rect; |
| +import android.util.SparseArray; |
| + |
| +import com.google.android.gms.common.ConnectionResult; |
| +import com.google.android.gms.common.GoogleApiAvailability; |
| +import com.google.android.gms.vision.Frame; |
| +import com.google.android.gms.vision.barcode.Barcode; |
| +import com.google.android.gms.vision.barcode.BarcodeDetector; |
| + |
| +import org.chromium.base.Log; |
| +import org.chromium.blink.mojom.BarcodeDetection; |
| +import org.chromium.blink.mojom.BarcodeDetectionResult; |
| +import org.chromium.gfx.mojom.RectF; |
| +import org.chromium.mojo.system.MojoException; |
| +import org.chromium.mojo.system.SharedBufferHandle; |
| +import org.chromium.mojo.system.SharedBufferHandle.MapFlags; |
| + |
| +import java.nio.ByteBuffer; |
| + |
| +/** |
| + * Implementation of mojo BarcodeDetection, using Google Play Services vision package. |
| + */ |
| +public class BarcodeDetectionImpl implements BarcodeDetection { |
| + private static final String TAG = "BarcodeDetectionImpl"; |
| + |
| + // https://cs.chromium.org/chromium/src/mojo/edk/system/broker_host.cc?l=24 |
| + private static final int MOJO_SHAREDBUFFER_MAX_BYTES = 16 * 1024 * 1024; |
| + |
| + private final Context mContext; |
| + private BarcodeDetector mBarcodeDetector; |
| + |
| + public BarcodeDetectionImpl(Context context) { |
| + Log.d(TAG, "BarcodeDetectionImpl ctor()"); |
| + mContext = context; |
| + mBarcodeDetector = new BarcodeDetector.Builder(mContext).build(); |
| + } |
| + |
| + @Override |
| + public void detect( |
| + SharedBufferHandle frameData, int width, int height, DetectResponse callback) { |
| + if (GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(mContext) |
|
dgn
2016/11/24 10:32:32
Consider using ExternalAuthUtils#canUseGooglePlayS
mcasas
2016/11/28 22:03:37
Done.
|
| + != ConnectionResult.SUCCESS) { |
| + Log.e(TAG, "Google Play Services not available"); |
| + callback.call(new BarcodeDetectionResult[0]); |
| + return; |
| + } |
| + // The vision library will be downloaded the first time the API is used |
| + // on the device; this happens "fast", but it might have not completed, |
| + // bail in this case. Also, the API was disabled between and v.9.0 and |
| + // v.9.2, see https://developers.google.com/android/guides/releases. |
| + if (!mBarcodeDetector.isOperational()) { |
| + Log.e(TAG, "BarcodeDetector is not operational"); |
| + callback.call(new BarcodeDetectionResult[0]); |
| + return; |
| + } |
| + |
| + final long numPixels = (long) width * height; |
| + if (!frameData.isValid() || width <= 0 || height <= 0 |
| + || 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.
|
| + callback.call(new BarcodeDetectionResult[0]); |
| + return; |
| + } |
| + |
| + ByteBuffer imageBuffer = frameData.map(0, numPixels * 4, MapFlags.none()); |
| + if (imageBuffer.capacity() <= 0) { |
| + callback.call(new BarcodeDetectionResult[0]); |
| + return; |
| + } |
| + |
| + Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); |
| + bitmap.copyPixelsFromBuffer(imageBuffer); |
| + |
| + Frame frame = null; |
| + try { |
| + // This constructor implies a pixel format conversion to YUV. |
| + frame = new Frame.Builder().setBitmap(bitmap).build(); |
| + } catch (IllegalArgumentException | IllegalStateException ex) { |
| + Log.e(TAG, "Frame.Builder().setBitmap() or build(): " + ex); |
| + callback.call(new BarcodeDetectionResult[0]); |
| + return; |
| + } |
| + |
| + final SparseArray<Barcode> barcodes = mBarcodeDetector.detect(frame); |
| + |
| + BarcodeDetectionResult[] barcodeArray = new BarcodeDetectionResult[barcodes.size()]; |
| + for (int i = 0; i < barcodes.size(); i++) { |
| + barcodeArray[i] = new BarcodeDetectionResult(); |
| + final Barcode barcode = barcodes.valueAt(i); |
| + barcodeArray[i].rawValue = barcode.rawValue; |
| + final Rect rect = barcode.getBoundingBox(); |
| + barcodeArray[i].boundingBox = new RectF(); |
| + barcodeArray[i].boundingBox.x = rect.left; |
| + barcodeArray[i].boundingBox.y = rect.top; |
| + barcodeArray[i].boundingBox.width = rect.width(); |
| + barcodeArray[i].boundingBox.height = rect.height(); |
| + } |
| + callback.call(barcodeArray); |
| + } |
| + |
| + @Override |
| + public void close() { |
| + mBarcodeDetector.release(); |
| + } |
| + |
| + @Override |
| + public void onConnectionError(MojoException e) { |
| + close(); |
| + } |
| + |
| +} |