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