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

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

Issue 2629433003: ShapeDetection: use mojom::Bitmap for mojo interface. (Closed)
Patch Set: ShapeDetection: use mojom::Bitmap for mojo interface. Created 3 years, 10 months 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
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.Bitmap.Config;
9 import android.graphics.Point; 10 import android.graphics.Point;
10 import android.graphics.Rect; 11 import android.graphics.Rect;
11 import android.util.SparseArray; 12 import android.util.SparseArray;
12 13
13 import com.google.android.gms.vision.Frame; 14 import com.google.android.gms.vision.Frame;
14 import com.google.android.gms.vision.barcode.Barcode; 15 import com.google.android.gms.vision.barcode.Barcode;
15 import com.google.android.gms.vision.barcode.BarcodeDetector; 16 import com.google.android.gms.vision.barcode.BarcodeDetector;
16 17
17 import org.chromium.base.Log; 18 import org.chromium.base.Log;
18 import org.chromium.chrome.browser.externalauth.ExternalAuthUtils; 19 import org.chromium.chrome.browser.externalauth.ExternalAuthUtils;
19 import org.chromium.chrome.browser.externalauth.UserRecoverableErrorHandler; 20 import org.chromium.chrome.browser.externalauth.UserRecoverableErrorHandler;
20 import org.chromium.gfx.mojom.PointF; 21 import org.chromium.gfx.mojom.PointF;
21 import org.chromium.gfx.mojom.RectF; 22 import org.chromium.gfx.mojom.RectF;
22 import org.chromium.mojo.system.MojoException; 23 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.shape_detection.mojom.BarcodeDetection; 24 import org.chromium.shape_detection.mojom.BarcodeDetection;
26 import org.chromium.shape_detection.mojom.BarcodeDetectionResult; 25 import org.chromium.shape_detection.mojom.BarcodeDetectionResult;
26 import org.chromium.skia.mojom.ColorType;
27 27
28 import java.nio.ByteBuffer; 28 import java.nio.ByteBuffer;
29 29
30 /** 30 /**
31 * Implementation of mojo BarcodeDetection, using Google Play Services vision pa ckage. 31 * Implementation of mojo BarcodeDetection, using Google Play Services vision pa ckage.
32 */ 32 */
33 public class BarcodeDetectionImpl implements BarcodeDetection { 33 public class BarcodeDetectionImpl implements BarcodeDetection {
34 private static final String TAG = "BarcodeDetectionImpl"; 34 private static final String TAG = "BarcodeDetectionImpl";
35 35
36 private final Context mContext; 36 private final Context mContext;
37 private BarcodeDetector mBarcodeDetector; 37 private BarcodeDetector mBarcodeDetector;
38 private Config mBitmapConfig;
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
45 private boolean validConfig(int colorType) {
46 switch (colorType) {
47 case ColorType.ALPHA_8:
48 mBitmapConfig = Config.ALPHA_8;
49 return true;
50 case ColorType.RGB_565:
51 mBitmapConfig = Config.RGB_565;
52 return true;
53 case ColorType.ARGB_4444:
54 mBitmapConfig = Config.ARGB_4444;
55 return true;
56 case ColorType.RGBA_8888:
57 mBitmapConfig = Config.ARGB_8888;
58 return true;
59 }
60 return false;
61 }
62
44 @Override 63 @Override
45 public void detect( 64 public void detect(org.chromium.skia.mojom.Bitmap bitmapData, DetectResponse callback) {
46 SharedBufferHandle frameData, int width, int height, DetectResponse callback) {
47 if (!ExternalAuthUtils.getInstance().canUseGooglePlayServices( 65 if (!ExternalAuthUtils.getInstance().canUseGooglePlayServices(
48 mContext, new UserRecoverableErrorHandler.Silent())) { 66 mContext, new UserRecoverableErrorHandler.Silent())) {
49 Log.e(TAG, "Google Play Services not available"); 67 Log.e(TAG, "Google Play Services not available");
50 callback.call(new BarcodeDetectionResult[0]); 68 callback.call(new BarcodeDetectionResult[0]);
51 return; 69 return;
52 } 70 }
53 // The vision library will be downloaded the first time the API is used 71 // The vision library will be downloaded the first time the API is used
54 // on the device; this happens "fast", but it might have not completed, 72 // on the device; this happens "fast", but it might have not completed,
55 // bail in this case. Also, the API was disabled between and v.9.0 and 73 // bail in this case. Also, the API was disabled between and v.9.0 and
56 // v.9.2, see https://developers.google.com/android/guides/releases. 74 // v.9.2, see https://developers.google.com/android/guides/releases.
57 if (!mBarcodeDetector.isOperational()) { 75 if (!mBarcodeDetector.isOperational()) {
58 Log.e(TAG, "BarcodeDetector is not operational"); 76 Log.e(TAG, "BarcodeDetector is not operational");
59 callback.call(new BarcodeDetectionResult[0]); 77 callback.call(new BarcodeDetectionResult[0]);
60 return; 78 return;
61 } 79 }
62 80
81 int width = bitmapData.width;
82 int height = bitmapData.height;
63 final long numPixels = (long) width * height; 83 final long numPixels = (long) width * height;
64 // TODO(mcasas): https://crbug.com/670028 homogeneize overflow checking. 84 // TODO(mcasas): https://crbug.com/670028 homogeneize overflow checking.
65 if (!frameData.isValid() || width <= 0 || height <= 0 || numPixels > (Lo ng.MAX_VALUE / 4)) { 85 if (bitmapData.pixelData == null || width <= 0 || height <= 0
86 || numPixels > (Long.MAX_VALUE / 4) || !validConfig(bitmapData.c olorType)) {
mcasas 2017/01/24 00:52:05 I doubt that any detector (this or TextDetectionIm
junwei 2017/01/25 06:14:50 I updated l.57 to handle BGRA_8888 the same as RGB
66 callback.call(new BarcodeDetectionResult[0]); 87 callback.call(new BarcodeDetectionResult[0]);
67 return; 88 return;
68 } 89 }
69 90
70 // Mapping |frameData| will fail if the intended mapped size is larger 91 // Mapping |frameData| will fail if the intended mapped size is larger
71 // than its actual capacity, which is limited by the appropriate 92 // than its actual capacity, which is limited by the appropriate
72 // mojo::edk::Configuration entry. 93 // mojo::edk::Configuration entry.
73 ByteBuffer imageBuffer = frameData.map(0, numPixels * 4, MapFlags.none() ); 94 ByteBuffer imageBuffer = ByteBuffer.wrap(bitmapData.pixelData);
74 if (imageBuffer.capacity() <= 0) { 95 if (imageBuffer.capacity() <= 0) {
75 callback.call(new BarcodeDetectionResult[0]); 96 callback.call(new BarcodeDetectionResult[0]);
76 return; 97 return;
77 } 98 }
78 99
79 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_88 88); 100 Bitmap bitmap = Bitmap.createBitmap(width, height, mBitmapConfig);
80 bitmap.copyPixelsFromBuffer(imageBuffer); 101 bitmap.copyPixelsFromBuffer(imageBuffer);
81 102
82 Frame frame = null; 103 Frame frame = null;
83 try { 104 try {
84 // This constructor implies a pixel format conversion to YUV. 105 // This constructor implies a pixel format conversion to YUV.
85 frame = new Frame.Builder().setBitmap(bitmap).build(); 106 frame = new Frame.Builder().setBitmap(bitmap).build();
86 } catch (IllegalArgumentException | IllegalStateException ex) { 107 } catch (IllegalArgumentException | IllegalStateException ex) {
87 Log.e(TAG, "Frame.Builder().setBitmap() or build(): " + ex); 108 Log.e(TAG, "Frame.Builder().setBitmap() or build(): " + ex);
88 callback.call(new BarcodeDetectionResult[0]); 109 callback.call(new BarcodeDetectionResult[0]);
89 return; 110 return;
(...skipping 27 matching lines...) Expand all
117 public void close() { 138 public void close() {
118 mBarcodeDetector.release(); 139 mBarcodeDetector.release();
119 } 140 }
120 141
121 @Override 142 @Override
122 public void onConnectionError(MojoException e) { 143 public void onConnectionError(MojoException e) {
123 close(); 144 close();
124 } 145 }
125 146
126 } 147 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698