Chromium Code Reviews| 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.content.browser.shapedetection; | 5 package org.chromium.content.browser.shapedetection; |
| 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 | 11 |
| 12 import org.chromium.base.Log; | 12 import org.chromium.base.Log; |
| 13 import org.chromium.gfx.mojom.RectF; | 13 import org.chromium.gfx.mojom.RectF; |
| 14 import org.chromium.mojo.system.MojoException; | 14 import org.chromium.mojo.system.MojoException; |
| 15 import org.chromium.mojo.system.SharedBufferHandle; | |
| 16 import org.chromium.mojo.system.SharedBufferHandle.MapFlags; | |
| 17 import org.chromium.shape_detection.mojom.FaceDetection; | 15 import org.chromium.shape_detection.mojom.FaceDetection; |
| 18 import org.chromium.shape_detection.mojom.FaceDetectionResult; | 16 import org.chromium.shape_detection.mojom.FaceDetectionResult; |
| 19 import org.chromium.shape_detection.mojom.FaceDetectorOptions; | 17 import org.chromium.shape_detection.mojom.FaceDetectorOptions; |
| 20 | 18 |
| 21 import java.nio.ByteBuffer; | 19 import java.nio.ByteBuffer; |
| 22 | 20 |
| 23 /** | 21 /** |
| 24 * Android implementation of the FaceDetection service defined in | 22 * Android implementation of the FaceDetection service defined in |
| 25 * services/shape_detection/public/interfaces/facedetection.mojom | 23 * services/shape_detection/public/interfaces/facedetection.mojom |
| 26 */ | 24 */ |
| 27 public class FaceDetectionImpl implements FaceDetection { | 25 public class FaceDetectionImpl implements FaceDetection { |
| 28 private static final String TAG = "FaceDetectionImpl"; | 26 private static final String TAG = "FaceDetectionImpl"; |
| 29 private static final int MAX_FACES = 32; | 27 private static final int MAX_FACES = 32; |
| 30 private final boolean mFastMode; | 28 private final boolean mFastMode; |
| 31 private final int mMaxFaces; | 29 private final int mMaxFaces; |
| 32 | 30 |
| 33 FaceDetectionImpl(FaceDetectorOptions options) { | 31 FaceDetectionImpl(FaceDetectorOptions options) { |
| 34 mFastMode = options.fastMode; | 32 mFastMode = options.fastMode; |
| 35 mMaxFaces = Math.min(options.maxDetectedFaces, MAX_FACES); | 33 mMaxFaces = Math.min(options.maxDetectedFaces, MAX_FACES); |
| 36 } | 34 } |
| 37 | 35 |
| 38 @Override | 36 @Override |
| 39 public void detect( | 37 public void detect(org.chromium.skia.mojom.Bitmap bitmapData, DetectResponse callback) { |
| 40 SharedBufferHandle frameData, int width, int height, DetectResponse callback) { | 38 int width = bitmapData.width; |
| 39 int height = bitmapData.height; | |
| 41 final long numPixels = (long) width * height; | 40 final long numPixels = (long) width * height; |
| 42 // TODO(xianglu): https://crbug.com/670028 homogeneize overflow checking . | 41 // TODO(xianglu): https://crbug.com/670028 homogeneize overflow checking . |
| 43 if (!frameData.isValid() || width <= 0 || height <= 0 || numPixels > (Lo ng.MAX_VALUE / 4)) { | 42 if (bitmapData.pixelData == null || width <= 0 || height <= 0 |
| 43 || numPixels > (Long.MAX_VALUE / 4)) { | |
| 44 Log.d(TAG, "Invalid argument(s)."); | 44 Log.d(TAG, "Invalid argument(s)."); |
| 45 callback.call(new FaceDetectionResult()); | 45 callback.call(new FaceDetectionResult()); |
| 46 return; | 46 return; |
| 47 } | 47 } |
| 48 | 48 |
| 49 ByteBuffer imageBuffer = frameData.map(0, numPixels * 4, MapFlags.none() ); | 49 ByteBuffer imageBuffer = ByteBuffer.wrap(bitmapData.pixelData); |
| 50 if (imageBuffer.capacity() <= 0) { | 50 if (imageBuffer.capacity() <= 0) { |
| 51 Log.d(TAG, "Failed to map from SharedBufferHandle."); | 51 Log.d(TAG, "Failed to wrap from Bitmap."); |
| 52 callback.call(new FaceDetectionResult()); | 52 callback.call(new FaceDetectionResult()); |
| 53 return; | 53 return; |
| 54 } | 54 } |
| 55 | 55 |
| 56 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_88 88); | 56 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_88 88); |
| 57 | 57 |
| 58 // An int array is needed to construct a Bitmap. However the Bytebuffer | 58 // An int array is needed to construct a Bitmap. However the Bytebuffer |
| 59 // we get from |sharedBufferHandle| is directly allocated and does not | 59 // we get from |sharedBufferHandle| is directly allocated and does not |
| 60 // have a supporting array. Therefore we need to copy from |imageBuffer| | 60 // have a supporting array. Therefore we need to copy from |imageBuffer| |
| 61 // to create this intermediate Bitmap. | 61 // to create this intermediate Bitmap. |
| 62 // TODO(xianglu): Consider worker pool as appropriate threads. | 62 // TODO(xianglu): Consider worker pool as appropriate threads. |
|
mcasas
2017/01/19 18:09:48
Please update this comment.
| |
| 63 // http://crbug.com/655814 | 63 // http://crbug.com/655814 |
| 64 bitmap.copyPixelsFromBuffer(imageBuffer); | 64 bitmap.copyPixelsFromBuffer(imageBuffer); |
|
mcasas
2017/01/19 18:09:48
Here you're copying |imageBuffer| into |bitmap| wh
| |
| 65 | 65 |
| 66 // A Bitmap must be in 565 format for findFaces() to work. See | 66 // A Bitmap must be in 565 format for findFaces() to work. See |
| 67 // http://androidxref.com/7.0.0_r1/xref/frameworks/base/media/java/andro id/media/FaceDetector.java#124 | 67 // http://androidxref.com/7.0.0_r1/xref/frameworks/base/media/java/andro id/media/FaceDetector.java#124 |
| 68 // | 68 // |
| 69 // It turns out that FaceDetector is not able to detect correctly if | 69 // It turns out that FaceDetector is not able to detect correctly if |
| 70 // simply using pixmap.setConfig(). The reason might be that findFaces() | 70 // simply using pixmap.setConfig(). The reason might be that findFaces() |
| 71 // needs non-premultiplied ARGB arrangement, while the alpha type in the | 71 // needs non-premultiplied ARGB arrangement, while the alpha type in the |
| 72 // original image is premultiplied. We can use getPixels() which does | 72 // original image is premultiplied. We can use getPixels() which does |
| 73 // the unmultiplication while copying to a new array. See | 73 // the unmultiplication while copying to a new array. See |
| 74 // http://androidxref.com/7.0.0_r1/xref/frameworks/base/graphics/java/an droid/graphics/Bitmap.java#538 | 74 // http://androidxref.com/7.0.0_r1/xref/frameworks/base/graphics/java/an droid/graphics/Bitmap.java#538 |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 104 } | 104 } |
| 105 | 105 |
| 106 @Override | 106 @Override |
| 107 public void close() {} | 107 public void close() {} |
| 108 | 108 |
| 109 @Override | 109 @Override |
| 110 public void onConnectionError(MojoException e) { | 110 public void onConnectionError(MojoException e) { |
| 111 close(); | 111 close(); |
| 112 } | 112 } |
| 113 } | 113 } |
| OLD | NEW |