OLD | NEW |
---|---|
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.shape_detection; | 5 package org.chromium.shape_detection; |
6 | 6 |
7 import android.graphics.Bitmap; | 7 import android.graphics.Bitmap; |
8 import android.graphics.PointF; | 8 import android.graphics.PointF; |
9 import android.media.FaceDetector; | 9 import android.media.FaceDetector; |
10 import android.media.FaceDetector.Face; | 10 import android.media.FaceDetector.Face; |
11 import android.os.AsyncTask; | 11 import android.os.AsyncTask; |
12 | 12 |
13 import org.chromium.base.Log; | 13 import org.chromium.base.Log; |
14 import org.chromium.gfx.mojom.RectF; | 14 import org.chromium.gfx.mojom.RectF; |
15 import org.chromium.mojo.system.MojoException; | 15 import org.chromium.mojo.system.MojoException; |
16 import org.chromium.mojo.system.SharedBufferHandle; | |
17 import org.chromium.mojo.system.SharedBufferHandle.MapFlags; | |
18 import org.chromium.shape_detection.mojom.FaceDetection; | 16 import org.chromium.shape_detection.mojom.FaceDetection; |
19 import org.chromium.shape_detection.mojom.FaceDetectionResult; | 17 import org.chromium.shape_detection.mojom.FaceDetectionResult; |
20 import org.chromium.shape_detection.mojom.FaceDetectorOptions; | 18 import org.chromium.shape_detection.mojom.FaceDetectorOptions; |
21 import org.chromium.shape_detection.mojom.Landmark; | 19 import org.chromium.shape_detection.mojom.Landmark; |
20 import org.chromium.skia.mojom.ColorType; | |
22 | 21 |
23 import java.nio.ByteBuffer; | 22 import java.nio.ByteBuffer; |
24 | 23 |
25 /** | 24 /** |
26 * Android implementation of the FaceDetection service defined in | 25 * Android implementation of the FaceDetection service defined in |
27 * services/shape_detection/public/interfaces/facedetection.mojom | 26 * services/shape_detection/public/interfaces/facedetection.mojom |
28 */ | 27 */ |
29 public class FaceDetectionImpl implements FaceDetection { | 28 public class FaceDetectionImpl implements FaceDetection { |
30 private static final String TAG = "FaceDetectionImpl"; | 29 private static final String TAG = "FaceDetectionImpl"; |
31 private static final int MAX_FACES = 32; | 30 private static final int MAX_FACES = 32; |
32 private final boolean mFastMode; | 31 private final boolean mFastMode; |
33 private final int mMaxFaces; | 32 private final int mMaxFaces; |
34 | 33 |
35 FaceDetectionImpl(FaceDetectorOptions options) { | 34 FaceDetectionImpl(FaceDetectorOptions options) { |
36 mFastMode = options.fastMode; | 35 mFastMode = options.fastMode; |
37 mMaxFaces = Math.min(options.maxDetectedFaces, MAX_FACES); | 36 mMaxFaces = Math.min(options.maxDetectedFaces, MAX_FACES); |
38 } | 37 } |
39 | 38 |
40 @Override | 39 @Override |
41 public void detect(SharedBufferHandle frameData, final int width, final int height, | 40 public void detect(org.chromium.skia.mojom.Bitmap bitmapData, final DetectRe sponse callback) { |
42 final DetectResponse callback) { | 41 final int width = bitmapData.width; |
42 final int height = bitmapData.height; | |
43 final long numPixels = (long) width * height; | 43 final long numPixels = (long) width * height; |
44 // TODO(xianglu): https://crbug.com/670028 homogeneize overflow checking . | 44 // TODO(xianglu): https://crbug.com/670028 homogeneize overflow checking . |
45 if (!frameData.isValid() || width <= 0 || height <= 0 || numPixels > (Lo ng.MAX_VALUE / 4)) { | 45 if (bitmapData.pixelData == null || width <= 0 || height <= 0 |
46 || numPixels > (Long.MAX_VALUE / 4)) { | |
46 Log.d(TAG, "Invalid argument(s)."); | 47 Log.d(TAG, "Invalid argument(s)."); |
47 callback.call(new FaceDetectionResult[0]); | 48 callback.call(new FaceDetectionResult[0]); |
48 return; | 49 return; |
49 } | 50 } |
50 | 51 |
51 ByteBuffer imageBuffer = frameData.map(0, numPixels * 4, MapFlags.none() ); | 52 // TODO(junwei.fu): https://crbug.com/684921 support other bitmap pixel formats, |
52 if (imageBuffer.capacity() <= 0) { | 53 if (bitmapData.colorType != ColorType.RGBA_8888 |
53 Log.d(TAG, "Failed to map from SharedBufferHandle."); | 54 && bitmapData.colorType != ColorType.BGRA_8888) { |
55 Log.e(TAG, "Unsupported bitmap pixel format"); | |
54 callback.call(new FaceDetectionResult[0]); | 56 callback.call(new FaceDetectionResult[0]); |
55 return; | 57 return; |
56 } | 58 } |
57 | 59 |
60 ByteBuffer imageBuffer = ByteBuffer.wrap(bitmapData.pixelData); | |
61 if (imageBuffer.capacity() <= 0) { | |
62 Log.d(TAG, "Failed to wrap from Bitmap."); | |
63 callback.call(new FaceDetectionResult[0]); | |
64 return; | |
65 } | |
66 | |
67 // TODO(junwei.fu): Use |bitmapData| directly for |unPremultipliedBitmap | to spare a copy | |
68 // if the bitmap pixel format is RGB_565, the ARGB_8888 Bitmap doesn't n eed to be created | |
69 // in this case, https://crbug.com/684930. | |
58 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_88 88); | 70 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_88 88); |
mcasas
2017/05/15 22:40:23
This method should use
Frame frame = SharedBuffer
| |
59 | 71 |
60 // An int array is needed to construct a Bitmap. However the Bytebuffer | 72 // An int array is needed to construct a Bitmap. However the Bytebuffer |
61 // we get from |sharedBufferHandle| is directly allocated and does not | 73 // we get from |bitmapData| is directly allocated and does not have a su pporting array. |
62 // have a supporting array. Therefore we need to copy from |imageBuffer| | 74 // Therefore we need to copy from |imageBuffer| to create this intermedi ate Bitmap. |
63 // to create this intermediate Bitmap. | |
64 // TODO(xianglu): Consider worker pool as appropriate threads. | 75 // TODO(xianglu): Consider worker pool as appropriate threads. |
65 // http://crbug.com/655814 | 76 // http://crbug.com/655814 |
66 bitmap.copyPixelsFromBuffer(imageBuffer); | 77 bitmap.copyPixelsFromBuffer(imageBuffer); |
67 | 78 |
68 // A Bitmap must be in 565 format for findFaces() to work. See | 79 // A Bitmap must be in 565 format for findFaces() to work. See |
69 // http://androidxref.com/7.0.0_r1/xref/frameworks/base/media/java/andro id/media/FaceDetector.java#124 | 80 // http://androidxref.com/7.0.0_r1/xref/frameworks/base/media/java/andro id/media/FaceDetector.java#124 |
70 // | 81 // |
71 // It turns out that FaceDetector is not able to detect correctly if | 82 // It turns out that FaceDetector is not able to detect correctly if |
72 // simply using pixmap.setConfig(). The reason might be that findFaces() | 83 // simply using pixmap.setConfig(). The reason might be that findFaces() |
73 // needs non-premultiplied ARGB arrangement, while the alpha type in the | 84 // needs non-premultiplied ARGB arrangement, while the alpha type in the |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
115 } | 126 } |
116 | 127 |
117 @Override | 128 @Override |
118 public void close() {} | 129 public void close() {} |
119 | 130 |
120 @Override | 131 @Override |
121 public void onConnectionError(MojoException e) { | 132 public void onConnectionError(MojoException e) { |
122 close(); | 133 close(); |
123 } | 134 } |
124 } | 135 } |
OLD | NEW |