Index: services/shape_detection/android/java/src/org/chromium/shape_detection/BitmapUtils.java |
diff --git a/services/shape_detection/android/java/src/org/chromium/shape_detection/BitmapUtils.java b/services/shape_detection/android/java/src/org/chromium/shape_detection/BitmapUtils.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..da32a2c3149ea0d3fd85a928482e6a7baca00110 |
--- /dev/null |
+++ b/services/shape_detection/android/java/src/org/chromium/shape_detection/BitmapUtils.java |
@@ -0,0 +1,54 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package org.chromium.shape_detection; |
+ |
+import android.graphics.Bitmap; |
+ |
+import com.google.android.gms.vision.Frame; |
+ |
+import org.chromium.skia.mojom.ColorType; |
+ |
+import java.nio.ByteBuffer; |
+ |
+/** |
+ * Utility class to convert a Bitmap to a GMS core YUV Frame. |
+ */ |
+public class BitmapUtils { |
+ public static Bitmap createBitmap(org.chromium.skia.mojom.Bitmap bitmapData) { |
+ int width = bitmapData.width; |
+ int height = bitmapData.height; |
+ final long numPixels = (long) width * height; |
+ // TODO(mcasas): https://crbug.com/670028 homogeneize overflow checking. |
+ if (bitmapData.pixelData == null || width <= 0 || height <= 0 |
+ || numPixels > (Long.MAX_VALUE / 4)) { |
+ return null; |
+ } |
+ |
+ // TODO(junwei.fu): https://crbug.com/684921 support other bitmap pixel formats. |
+ if (bitmapData.colorType != ColorType.RGBA_8888 |
+ && bitmapData.colorType != ColorType.BGRA_8888) { |
+ return null; |
+ } |
+ |
+ ByteBuffer imageBuffer = ByteBuffer.wrap(bitmapData.pixelData); |
+ if (imageBuffer.capacity() <= 0) { |
+ return null; |
+ } |
+ |
+ Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); |
+ bitmap.copyPixelsFromBuffer(imageBuffer); |
+ |
+ return bitmap; |
+ } |
+ |
+ public static Frame convertToFrame(org.chromium.skia.mojom.Bitmap bitmapData) { |
mcasas
2017/05/19 17:20:22
This method is called convertToX() whereas the pre
|
+ try { |
+ // This constructor implies a pixel format conversion to YUV. |
+ return new Frame.Builder().setBitmap(createBitmap(bitmapData)).build(); |
+ } catch (IllegalArgumentException | IllegalStateException ex) { |
+ return null; |
+ } |
+ } |
+} |
mcasas
2017/05/19 17:20:22
We landed recently a Junit test for the predecesso
|