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.Bitmap.Config; | |
9 import android.graphics.Rect; | 10 import android.graphics.Rect; |
10 import android.util.SparseArray; | 11 import android.util.SparseArray; |
11 | 12 |
12 import com.google.android.gms.vision.Frame; | 13 import com.google.android.gms.vision.Frame; |
13 import com.google.android.gms.vision.text.TextBlock; | 14 import com.google.android.gms.vision.text.TextBlock; |
14 import com.google.android.gms.vision.text.TextRecognizer; | 15 import com.google.android.gms.vision.text.TextRecognizer; |
15 | 16 |
16 import org.chromium.base.Log; | 17 import org.chromium.base.Log; |
17 import org.chromium.chrome.browser.externalauth.ExternalAuthUtils; | 18 import org.chromium.chrome.browser.externalauth.ExternalAuthUtils; |
18 import org.chromium.chrome.browser.externalauth.UserRecoverableErrorHandler; | 19 import org.chromium.chrome.browser.externalauth.UserRecoverableErrorHandler; |
19 import org.chromium.gfx.mojom.RectF; | 20 import org.chromium.gfx.mojom.RectF; |
20 import org.chromium.mojo.system.MojoException; | 21 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.shape_detection.mojom.TextDetection; | 22 import org.chromium.shape_detection.mojom.TextDetection; |
24 import org.chromium.shape_detection.mojom.TextDetectionResult; | 23 import org.chromium.shape_detection.mojom.TextDetectionResult; |
24 import org.chromium.skia.mojom.ColorType; | |
25 | 25 |
26 import java.nio.ByteBuffer; | 26 import java.nio.ByteBuffer; |
27 | 27 |
28 /** | 28 /** |
29 * Implementation of mojo TextDetection, using Google Play Services vision packa ge. | 29 * Implementation of mojo TextDetection, using Google Play Services vision packa ge. |
30 */ | 30 */ |
31 public class TextDetectionImpl implements TextDetection { | 31 public class TextDetectionImpl implements TextDetection { |
32 private static final String TAG = "TextDetectionImpl"; | 32 private static final String TAG = "TextDetectionImpl"; |
33 | 33 |
34 private final Context mContext; | 34 private final Context mContext; |
35 private TextRecognizer mTextRecognizer; | 35 private TextRecognizer mTextRecognizer; |
36 private Config mBitmapConfig; | |
36 | 37 |
37 public TextDetectionImpl(Context context) { | 38 public TextDetectionImpl(Context context) { |
38 mContext = context; | 39 mContext = context; |
39 mTextRecognizer = new TextRecognizer.Builder(mContext).build(); | 40 mTextRecognizer = new TextRecognizer.Builder(mContext).build(); |
40 } | 41 } |
41 | 42 |
43 private boolean validConfig(int colorType) { | |
44 switch (colorType) { | |
45 case ColorType.ALPHA_8: | |
46 mBitmapConfig = Config.ALPHA_8; | |
47 return true; | |
48 case ColorType.RGB_565: | |
49 mBitmapConfig = Config.RGB_565; | |
50 return true; | |
51 case ColorType.ARGB_4444: | |
52 mBitmapConfig = Config.ARGB_4444; | |
53 return true; | |
54 case ColorType.RGBA_8888: | |
55 mBitmapConfig = Config.ARGB_8888; | |
56 return true; | |
57 } | |
58 return false; | |
59 } | |
60 | |
42 @Override | 61 @Override |
43 public void detect( | 62 public void detect(org.chromium.skia.mojom.Bitmap bitmapData, DetectResponse callback) { |
44 SharedBufferHandle frameData, int width, int height, DetectResponse callback) { | |
45 if (!ExternalAuthUtils.getInstance().canUseGooglePlayServices( | 63 if (!ExternalAuthUtils.getInstance().canUseGooglePlayServices( |
46 mContext, new UserRecoverableErrorHandler.Silent())) { | 64 mContext, new UserRecoverableErrorHandler.Silent())) { |
47 Log.e(TAG, "Google Play Services not available"); | 65 Log.e(TAG, "Google Play Services not available"); |
48 callback.call(new TextDetectionResult[0]); | 66 callback.call(new TextDetectionResult[0]); |
49 return; | 67 return; |
50 } | 68 } |
51 // The vision library will be downloaded the first time the API is used | 69 // The vision library will be downloaded the first time the API is used |
52 // on the device; this happens "fast", but it might have not completed, | 70 // on the device; this happens "fast", but it might have not completed, |
53 // bail in this case. Also, the API was disabled between and v.9.0 and | 71 // bail in this case. Also, the API was disabled between and v.9.0 and |
54 // v.9.2, see https://developers.google.com/android/guides/releases. | 72 // v.9.2, see https://developers.google.com/android/guides/releases. |
55 if (!mTextRecognizer.isOperational()) { | 73 if (!mTextRecognizer.isOperational()) { |
56 Log.e(TAG, "TextDetector is not operational"); | 74 Log.e(TAG, "TextDetector is not operational"); |
57 callback.call(new TextDetectionResult[0]); | 75 callback.call(new TextDetectionResult[0]); |
58 return; | 76 return; |
59 } | 77 } |
60 | 78 |
79 int width = bitmapData.width; | |
80 int height = bitmapData.height; | |
61 final long numPixels = (long) width * height; | 81 final long numPixels = (long) width * height; |
62 // TODO(xianglu): https://crbug.com/670028 homogeneize overflow checking . | 82 // TODO(xianglu): https://crbug.com/670028 homogeneize overflow checking . |
63 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) || !validConfig(bitmapData.c olorType)) { | |
mcasas
2017/01/24 00:52:05
Same comments here as in BarcodeDetectionImpl.
| |
64 callback.call(new TextDetectionResult[0]); | 85 callback.call(new TextDetectionResult[0]); |
65 return; | 86 return; |
66 } | 87 } |
67 | 88 |
68 // Mapping |frameData| will fail if the intended mapped size is larger | 89 // Mapping |frameData| will fail if the intended mapped size is larger |
69 // than its actual capacity, which is limited by the appropriate | 90 // than its actual capacity, which is limited by the appropriate |
70 // mojo::edk::Configuration entry. | 91 // mojo::edk::Configuration entry. |
71 ByteBuffer imageBuffer = frameData.map(0, numPixels * 4, MapFlags.none() ); | 92 ByteBuffer imageBuffer = ByteBuffer.wrap(bitmapData.pixelData); |
72 if (imageBuffer.capacity() <= 0) { | 93 if (imageBuffer.capacity() <= 0) { |
73 callback.call(new TextDetectionResult[0]); | 94 callback.call(new TextDetectionResult[0]); |
74 return; | 95 return; |
75 } | 96 } |
76 | 97 |
77 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_88 88); | 98 Bitmap bitmap = Bitmap.createBitmap(width, height, mBitmapConfig); |
78 bitmap.copyPixelsFromBuffer(imageBuffer); | 99 bitmap.copyPixelsFromBuffer(imageBuffer); |
79 | 100 |
80 Frame frame = null; | 101 Frame frame = null; |
81 try { | 102 try { |
82 // This constructor implies a pixel format conversion to YUV. | 103 // This constructor implies a pixel format conversion to YUV. |
83 frame = new Frame.Builder().setBitmap(bitmap).build(); | 104 frame = new Frame.Builder().setBitmap(bitmap).build(); |
84 } catch (IllegalArgumentException | IllegalStateException ex) { | 105 } catch (IllegalArgumentException | IllegalStateException ex) { |
85 Log.e(TAG, "Frame.Builder().setBitmap() or build(): " + ex); | 106 Log.e(TAG, "Frame.Builder().setBitmap() or build(): " + ex); |
86 callback.call(new TextDetectionResult[0]); | 107 callback.call(new TextDetectionResult[0]); |
87 return; | 108 return; |
(...skipping 18 matching lines...) Expand all Loading... | |
106 @Override | 127 @Override |
107 public void close() { | 128 public void close() { |
108 mTextRecognizer.release(); | 129 mTextRecognizer.release(); |
109 } | 130 } |
110 | 131 |
111 @Override | 132 @Override |
112 public void onConnectionError(MojoException e) { | 133 public void onConnectionError(MojoException e) { |
113 close(); | 134 close(); |
114 } | 135 } |
115 } | 136 } |
OLD | NEW |