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

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, 11 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.services.service_manager.InterfaceFactory; 24 import org.chromium.services.service_manager.InterfaceFactory;
26 import org.chromium.shape_detection.mojom.BarcodeDetection; 25 import org.chromium.shape_detection.mojom.BarcodeDetection;
27 import org.chromium.shape_detection.mojom.BarcodeDetectionResult; 26 import org.chromium.shape_detection.mojom.BarcodeDetectionResult;
27 import org.chromium.skia.mojom.ColorType;
28 28
29 import java.nio.ByteBuffer; 29 import java.nio.ByteBuffer;
30 30
31 /** 31 /**
32 * Implementation of mojo BarcodeDetection, using Google Play Services vision pa ckage. 32 * Implementation of mojo BarcodeDetection, using Google Play Services vision pa ckage.
33 */ 33 */
34 public class BarcodeDetectionImpl implements BarcodeDetection { 34 public class BarcodeDetectionImpl implements BarcodeDetection {
35 private static final String TAG = "BarcodeDetectionImpl"; 35 private static final String TAG = "BarcodeDetectionImpl";
36 36
37 private final Context mContext; 37 private final Context mContext;
38 private BarcodeDetector mBarcodeDetector; 38 private BarcodeDetector mBarcodeDetector;
39 39
40 public BarcodeDetectionImpl(Context context) { 40 public BarcodeDetectionImpl(Context context) {
41 mContext = context; 41 mContext = context;
42 mBarcodeDetector = new BarcodeDetector.Builder(mContext).build(); 42 mBarcodeDetector = new BarcodeDetector.Builder(mContext).build();
43 } 43 }
44 44
45 private Config validConfig(int colorType) {
46 switch (colorType) {
47 case ColorType.ALPHA_8:
48 return Config.ALPHA_8;
49 case ColorType.RGB_565:
50 return Config.RGB_565;
51 case ColorType.ARGB_4444:
52 return Config.ARGB_4444;
53 case ColorType.RGBA_8888:
54 case ColorType.BGRA_8888:
55 return Config.ARGB_8888;
56 }
57 return null;
58 }
59
45 @Override 60 @Override
46 public void detect( 61 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( 62 if (!ExternalAuthUtils.getInstance().canUseGooglePlayServices(
49 mContext, new UserRecoverableErrorHandler.Silent())) { 63 mContext, new UserRecoverableErrorHandler.Silent())) {
50 Log.e(TAG, "Google Play Services not available"); 64 Log.e(TAG, "Google Play Services not available");
51 callback.call(new BarcodeDetectionResult[0]); 65 callback.call(new BarcodeDetectionResult[0]);
52 return; 66 return;
53 } 67 }
54 // The vision library will be downloaded the first time the API is used 68 // 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, 69 // 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 70 // 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. 71 // v.9.2, see https://developers.google.com/android/guides/releases.
58 if (!mBarcodeDetector.isOperational()) { 72 if (!mBarcodeDetector.isOperational()) {
59 Log.e(TAG, "BarcodeDetector is not operational"); 73 Log.e(TAG, "BarcodeDetector is not operational");
60 callback.call(new BarcodeDetectionResult[0]); 74 callback.call(new BarcodeDetectionResult[0]);
61 return; 75 return;
62 } 76 }
63 77
78 int width = bitmapData.width;
79 int height = bitmapData.height;
80 Config bitmapConfig = validConfig(bitmapData.colorType);
mcasas 2017/01/24 20:48:31 The problem is that, even though the code reads as
64 final long numPixels = (long) width * height; 81 final long numPixels = (long) width * height;
65 // TODO(mcasas): https://crbug.com/670028 homogeneize overflow checking. 82 // TODO(mcasas): https://crbug.com/670028 homogeneize overflow checking.
66 if (!frameData.isValid() || width <= 0 || height <= 0 || numPixels > (Lo ng.MAX_VALUE / 4)) { 83 if (bitmapData.pixelData == null || width <= 0 || height <= 0
84 || numPixels > (Long.MAX_VALUE / 4) || bitmapConfig == null) {
67 callback.call(new BarcodeDetectionResult[0]); 85 callback.call(new BarcodeDetectionResult[0]);
68 return; 86 return;
69 } 87 }
70 88
71 // Mapping |frameData| will fail if the intended mapped size is larger 89 // Mapping |frameData| will fail if the intended mapped size is larger
72 // than its actual capacity, which is limited by the appropriate 90 // than its actual capacity, which is limited by the appropriate
73 // mojo::edk::Configuration entry. 91 // mojo::edk::Configuration entry.
74 ByteBuffer imageBuffer = frameData.map(0, numPixels * 4, MapFlags.none() ); 92 ByteBuffer imageBuffer = ByteBuffer.wrap(bitmapData.pixelData);
75 if (imageBuffer.capacity() <= 0) { 93 if (imageBuffer.capacity() <= 0) {
76 callback.call(new BarcodeDetectionResult[0]); 94 callback.call(new BarcodeDetectionResult[0]);
77 return; 95 return;
78 } 96 }
79 97
80 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_88 88); 98 Bitmap bitmap = Bitmap.createBitmap(width, height, bitmapConfig);
81 bitmap.copyPixelsFromBuffer(imageBuffer); 99 bitmap.copyPixelsFromBuffer(imageBuffer);
82 100
83 Frame frame = null; 101 Frame frame = null;
84 try { 102 try {
85 // This constructor implies a pixel format conversion to YUV. 103 // This constructor implies a pixel format conversion to YUV.
86 frame = new Frame.Builder().setBitmap(bitmap).build(); 104 frame = new Frame.Builder().setBitmap(bitmap).build();
87 } catch (IllegalArgumentException | IllegalStateException ex) { 105 } catch (IllegalArgumentException | IllegalStateException ex) {
88 Log.e(TAG, "Frame.Builder().setBitmap() or build(): " + ex); 106 Log.e(TAG, "Frame.Builder().setBitmap() or build(): " + ex);
89 callback.call(new BarcodeDetectionResult[0]); 107 callback.call(new BarcodeDetectionResult[0]);
90 return; 108 return;
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 public Factory(Context context) { 151 public Factory(Context context) {
134 mContext = context; 152 mContext = context;
135 } 153 }
136 154
137 @Override 155 @Override
138 public BarcodeDetection createImpl() { 156 public BarcodeDetection createImpl() {
139 return new BarcodeDetectionImpl(mContext); 157 return new BarcodeDetectionImpl(mContext);
140 } 158 }
141 } 159 }
142 } 160 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698