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; | |
25 import org.chromium.services.service_manager.InterfaceFactory; | 23 import org.chromium.services.service_manager.InterfaceFactory; |
26 import org.chromium.shape_detection.mojom.BarcodeDetection; | 24 import org.chromium.shape_detection.mojom.BarcodeDetection; |
27 import org.chromium.shape_detection.mojom.BarcodeDetectionResult; | 25 import org.chromium.shape_detection.mojom.BarcodeDetectionResult; |
| 26 import org.chromium.skia.mojom.ColorType; |
28 | 27 |
29 import java.nio.ByteBuffer; | 28 import java.nio.ByteBuffer; |
30 | 29 |
31 /** | 30 /** |
32 * Implementation of mojo BarcodeDetection, using Google Play Services vision pa
ckage. | 31 * Implementation of mojo BarcodeDetection, using Google Play Services vision pa
ckage. |
33 */ | 32 */ |
34 public class BarcodeDetectionImpl implements BarcodeDetection { | 33 public class BarcodeDetectionImpl implements BarcodeDetection { |
35 private static final String TAG = "BarcodeDetectionImpl"; | 34 private static final String TAG = "BarcodeDetectionImpl"; |
36 | 35 |
37 private final Context mContext; | 36 private final Context mContext; |
38 private BarcodeDetector mBarcodeDetector; | 37 private BarcodeDetector mBarcodeDetector; |
39 | 38 |
40 public BarcodeDetectionImpl(Context context) { | 39 public BarcodeDetectionImpl(Context context) { |
41 mContext = context; | 40 mContext = context; |
42 mBarcodeDetector = new BarcodeDetector.Builder(mContext).build(); | 41 mBarcodeDetector = new BarcodeDetector.Builder(mContext).build(); |
43 } | 42 } |
44 | 43 |
45 @Override | 44 @Override |
46 public void detect( | 45 public void detect(org.chromium.skia.mojom.Bitmap bitmapData, DetectResponse
callback) { |
47 SharedBufferHandle frameData, int width, int height, DetectResponse
callback) { | |
48 if (!ExternalAuthUtils.getInstance().canUseGooglePlayServices( | 46 if (!ExternalAuthUtils.getInstance().canUseGooglePlayServices( |
49 mContext, new UserRecoverableErrorHandler.Silent())) { | 47 mContext, new UserRecoverableErrorHandler.Silent())) { |
50 Log.e(TAG, "Google Play Services not available"); | 48 Log.e(TAG, "Google Play Services not available"); |
51 callback.call(new BarcodeDetectionResult[0]); | 49 callback.call(new BarcodeDetectionResult[0]); |
52 return; | 50 return; |
53 } | 51 } |
54 // The vision library will be downloaded the first time the API is used | 52 // The vision library will be downloaded the first time the API is used |
55 // on the device; this happens "fast", but it might have not completed, | 53 // on the device; this happens "fast", but it might have not completed, |
56 // bail in this case. Also, the API was disabled between and v.9.0 and | 54 // bail in this case. Also, the API was disabled between and v.9.0 and |
57 // v.9.2, see https://developers.google.com/android/guides/releases. | 55 // v.9.2, see https://developers.google.com/android/guides/releases. |
58 if (!mBarcodeDetector.isOperational()) { | 56 if (!mBarcodeDetector.isOperational()) { |
59 Log.e(TAG, "BarcodeDetector is not operational"); | 57 Log.e(TAG, "BarcodeDetector is not operational"); |
60 callback.call(new BarcodeDetectionResult[0]); | 58 callback.call(new BarcodeDetectionResult[0]); |
61 return; | 59 return; |
62 } | 60 } |
63 | 61 |
64 final long numPixels = (long) width * height; | 62 // TODO(junwei.fu): Consider supporting other bitmap pixel formats, |
65 // TODO(mcasas): https://crbug.com/670028 homogeneize overflow checking. | 63 // https://crbug.com/684921. |
66 if (!frameData.isValid() || width <= 0 || height <= 0 || numPixels > (Lo
ng.MAX_VALUE / 4)) { | 64 if (bitmapData.colorType != ColorType.RGBA_8888 |
| 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 } |
70 | 80 |
71 // Mapping |frameData| will fail if the intended mapped size is larger | 81 // Mapping |frameData| will fail if the intended mapped size is larger |
72 // than its actual capacity, which is limited by the appropriate | 82 // than its actual capacity, which is limited by the appropriate |
73 // mojo::edk::Configuration entry. | 83 // mojo::edk::Configuration entry. |
74 ByteBuffer imageBuffer = frameData.map(0, numPixels * 4, MapFlags.none()
); | 84 ByteBuffer imageBuffer = ByteBuffer.wrap(bitmapData.pixelData); |
75 if (imageBuffer.capacity() <= 0) { | 85 if (imageBuffer.capacity() <= 0) { |
76 callback.call(new BarcodeDetectionResult[0]); | 86 callback.call(new BarcodeDetectionResult[0]); |
77 return; | 87 return; |
78 } | 88 } |
79 | 89 |
80 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_88
88); | 90 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_88
88); |
81 bitmap.copyPixelsFromBuffer(imageBuffer); | 91 bitmap.copyPixelsFromBuffer(imageBuffer); |
82 | 92 |
83 Frame frame = null; | 93 Frame frame = null; |
84 try { | 94 try { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 public Factory(Context context) { | 143 public Factory(Context context) { |
134 mContext = context; | 144 mContext = context; |
135 } | 145 } |
136 | 146 |
137 @Override | 147 @Override |
138 public BarcodeDetection createImpl() { | 148 public BarcodeDetection createImpl() { |
139 return new BarcodeDetectionImpl(mContext); | 149 return new BarcodeDetectionImpl(mContext); |
140 } | 150 } |
141 } | 151 } |
142 } | 152 } |
OLD | NEW |