我的目标是创建一个ImageView
小部件,在我检测到的每张脸上绘制我的脸。
为了学习FaceDetector
和ImageView
课程,我现在只是尝试为ImageView
位图中检测到的每张脸部绘制一个白色矩形。
这是我到目前为止所做的:
public class ScottView extends ImageView{
private static final int maxFaces = 5;
private static final double eyeWidthfaceWidthRatio = 1.46;
private static final double faceWidthFaceHeightRatio = 1.75;
private Face[] detectedFaces;
public ScottView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public ScottView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TODO Auto-generated constructor stub
}
public ScottView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
detectedFaces = new FaceDetector.Face[maxFaces];
buildDrawingCache();
Bitmap converted = convertTo565(getDrawingCache());
FaceDetector detector = new FaceDetector(converted.getWidth(), converted.getHeight(), maxFaces);
detector.findFaces(converted, detectedFaces);
PointF point = new PointF();
Paint paint = new Paint();
paint.setColor(Color.WHITE);
RectF rect;
rect = new RectF(0, 0, 1000, 1000);
canvas.drawRect(rect, paint);
super.onDraw(canvas);
}
private Bitmap convertTo565(final Bitmap origin) {
if (origin == null) {
return null;
}
Bitmap bitmap = origin;
if (bitmap.getConfig() != Bitmap.Config.RGB_565) {
bitmap = bitmap.copy(Bitmap.Config.RGB_565, true);
}
if ((bitmap.getWidth() & 0x1) != 0) {
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth() & ~0x1,
bitmap.getHeight());
}
return bitmap;
}
}
正如你所看到的,我放弃了用矩形覆盖每张脸;现在我只想绘制一个矩形,因为这对我来说非常具有挑战性
当我在我制作的测试应用程序中测试时,我没有看到任何白色矩形......
在我绘制它之后,我是否需要对canvas
做一些特别的事情?
另外,我对onDraw
中的画布本身感到困惑...它是代表整个屏幕还是只代表图像本身?