OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.shapedetection; | 5 package org.chromium.chrome.browser.shapedetection; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.graphics.Bitmap; | 8 import android.graphics.Bitmap; |
9 import android.graphics.Point; | 9 import android.graphics.Point; |
10 import android.graphics.Rect; | 10 import android.graphics.Rect; |
11 import android.util.SparseArray; | 11 import android.util.SparseArray; |
12 | 12 |
13 import com.google.android.gms.vision.Frame; | 13 import com.google.android.gms.vision.Frame; |
14 import com.google.android.gms.vision.barcode.Barcode; | 14 import com.google.android.gms.vision.barcode.Barcode; |
15 import com.google.android.gms.vision.barcode.BarcodeDetector; | 15 import com.google.android.gms.vision.barcode.BarcodeDetector; |
16 | 16 |
17 import org.chromium.base.Log; | 17 import org.chromium.base.Log; |
18 import org.chromium.chrome.browser.externalauth.ExternalAuthUtils; | 18 import org.chromium.chrome.browser.externalauth.ExternalAuthUtils; |
19 import org.chromium.chrome.browser.externalauth.UserRecoverableErrorHandler; | 19 import org.chromium.chrome.browser.externalauth.UserRecoverableErrorHandler; |
20 import org.chromium.gfx.mojom.PointF; | 20 import org.chromium.gfx.mojom.PointF; |
21 import org.chromium.gfx.mojom.RectF; | 21 import org.chromium.gfx.mojom.RectF; |
22 import org.chromium.mojo.system.MojoException; | 22 import org.chromium.mojo.system.MojoException; |
| 23 import org.chromium.mojo.system.SharedBufferHandle; |
| 24 import org.chromium.mojo.system.SharedBufferHandle.MapFlags; |
23 import org.chromium.services.service_manager.InterfaceFactory; | 25 import org.chromium.services.service_manager.InterfaceFactory; |
24 import org.chromium.shape_detection.mojom.BarcodeDetection; | 26 import org.chromium.shape_detection.mojom.BarcodeDetection; |
25 import org.chromium.shape_detection.mojom.BarcodeDetectionResult; | 27 import org.chromium.shape_detection.mojom.BarcodeDetectionResult; |
26 import org.chromium.skia.mojom.ColorType; | |
27 | 28 |
28 import java.nio.ByteBuffer; | 29 import java.nio.ByteBuffer; |
29 | 30 |
30 /** | 31 /** |
31 * Implementation of mojo BarcodeDetection, using Google Play Services vision pa
ckage. | 32 * Implementation of mojo BarcodeDetection, using Google Play Services vision pa
ckage. |
32 */ | 33 */ |
33 public class BarcodeDetectionImpl implements BarcodeDetection { | 34 public class BarcodeDetectionImpl implements BarcodeDetection { |
34 private static final String TAG = "BarcodeDetectionImpl"; | 35 private static final String TAG = "BarcodeDetectionImpl"; |
35 | 36 |
36 private final Context mContext; | 37 private final Context mContext; |
37 private BarcodeDetector mBarcodeDetector; | 38 private BarcodeDetector mBarcodeDetector; |
38 | 39 |
39 public BarcodeDetectionImpl(Context context) { | 40 public BarcodeDetectionImpl(Context context) { |
40 mContext = context; | 41 mContext = context; |
41 mBarcodeDetector = new BarcodeDetector.Builder(mContext).build(); | 42 mBarcodeDetector = new BarcodeDetector.Builder(mContext).build(); |
42 } | 43 } |
43 | 44 |
44 @Override | 45 @Override |
45 public void detect(org.chromium.skia.mojom.Bitmap bitmapData, DetectResponse
callback) { | 46 public void detect( |
| 47 SharedBufferHandle frameData, int width, int height, DetectResponse
callback) { |
46 if (!ExternalAuthUtils.getInstance().canUseGooglePlayServices( | 48 if (!ExternalAuthUtils.getInstance().canUseGooglePlayServices( |
47 mContext, new UserRecoverableErrorHandler.Silent())) { | 49 mContext, new UserRecoverableErrorHandler.Silent())) { |
48 Log.e(TAG, "Google Play Services not available"); | 50 Log.e(TAG, "Google Play Services not available"); |
49 callback.call(new BarcodeDetectionResult[0]); | 51 callback.call(new BarcodeDetectionResult[0]); |
50 return; | 52 return; |
51 } | 53 } |
52 // The vision library will be downloaded the first time the API is used | 54 // 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, | 55 // 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 | 56 // 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. | 57 // v.9.2, see https://developers.google.com/android/guides/releases. |
56 if (!mBarcodeDetector.isOperational()) { | 58 if (!mBarcodeDetector.isOperational()) { |
57 Log.e(TAG, "BarcodeDetector is not operational"); | 59 Log.e(TAG, "BarcodeDetector is not operational"); |
58 callback.call(new BarcodeDetectionResult[0]); | 60 callback.call(new BarcodeDetectionResult[0]); |
59 return; | 61 return; |
60 } | 62 } |
61 | 63 |
62 // TODO(junwei.fu): Consider supporting other bitmap pixel formats, | 64 final long numPixels = (long) width * height; |
63 // https://crbug.com/684921. | 65 // TODO(mcasas): https://crbug.com/670028 homogeneize overflow checking. |
64 if (bitmapData.colorType != ColorType.RGBA_8888 | 66 if (!frameData.isValid() || width <= 0 || height <= 0 || numPixels > (Lo
ng.MAX_VALUE / 4)) { |
65 && bitmapData.colorType != ColorType.BGRA_8888) { | |
66 Log.e(TAG, "Unsupported bitmap pixel format"); | |
67 callback.call(new BarcodeDetectionResult[0]); | 67 callback.call(new BarcodeDetectionResult[0]); |
68 return; | 68 return; |
69 } | 69 } |
70 | |
71 int width = bitmapData.width; | |
72 int height = bitmapData.height; | |
73 final long numPixels = (long) width * height; | |
74 // TODO(mcasas): https://crbug.com/670028 homogeneize overflow checking. | |
75 if (bitmapData.pixelData == null || width <= 0 || height <= 0 | |
76 || numPixels > (Long.MAX_VALUE / 4)) { | |
77 callback.call(new BarcodeDetectionResult[0]); | |
78 return; | |
79 } | |
80 | 70 |
81 // Mapping |frameData| will fail if the intended mapped size is larger | 71 // Mapping |frameData| will fail if the intended mapped size is larger |
82 // than its actual capacity, which is limited by the appropriate | 72 // than its actual capacity, which is limited by the appropriate |
83 // mojo::edk::Configuration entry. | 73 // mojo::edk::Configuration entry. |
84 ByteBuffer imageBuffer = ByteBuffer.wrap(bitmapData.pixelData); | 74 ByteBuffer imageBuffer = frameData.map(0, numPixels * 4, MapFlags.none()
); |
85 if (imageBuffer.capacity() <= 0) { | 75 if (imageBuffer.capacity() <= 0) { |
86 callback.call(new BarcodeDetectionResult[0]); | 76 callback.call(new BarcodeDetectionResult[0]); |
87 return; | 77 return; |
88 } | 78 } |
89 | 79 |
90 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_88
88); | 80 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_88
88); |
91 bitmap.copyPixelsFromBuffer(imageBuffer); | 81 bitmap.copyPixelsFromBuffer(imageBuffer); |
92 | 82 |
93 Frame frame = null; | 83 Frame frame = null; |
94 try { | 84 try { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 public Factory(Context context) { | 133 public Factory(Context context) { |
144 mContext = context; | 134 mContext = context; |
145 } | 135 } |
146 | 136 |
147 @Override | 137 @Override |
148 public BarcodeDetection createImpl() { | 138 public BarcodeDetection createImpl() { |
149 return new BarcodeDetectionImpl(mContext); | 139 return new BarcodeDetectionImpl(mContext); |
150 } | 140 } |
151 } | 141 } |
152 } | 142 } |
OLD | NEW |