paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(5);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.RGB_565;
bitmap = new BitmapFactory().decodeResource(getResources(),R.drawable.danish,options);
imageHeight = image_1.getHeight();
imageWidth = image_1.getWidth();
face = new Face[numberofFaces];
faceDetector = new FaceDetector(imageWidth,imageHeight,numberofFaces);
foundfaces = faceDetector.findFaces(bitmap,face);
if (foundfaces > 0)
{
Toast.makeText(this,"Found Face",Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(this,"No face found",Toast.LENGTH_LONG).show();
}
Canvas canvas = new Canvas();
drawCanvas(canvas);
}
private void drawCanvas(Canvas canvas) {
canvas.drawBitmap(bitmap,0,0,null);
for(int i=0;i<foundfaces;i++)
{
Face faces = face[i];
PointF midPoint = new PointF();
faces.getMidPoint(midPoint);
eyedistance = faces.eyesDistance();
canvas.drawRect((int)midPoint.x - eyedistance,(int) midPoint.y - eyedistance, (int)midPoint.x + eyedistance,(int)midPoint.y + eyedistance,paint);
}
Toast.makeText(this,"Eye Distance: "+eyedistance,Toast.LENGTH_LONG).show();
}
我正在做一个人脸检测项目并通过Android Face Libraryyy检测一个人脸...这个人脸检测代码给了我一个正面的眼距和类似的东西输出,但没有在脸上显示一个矩形框Can有人对此有所帮助吗?
答案 0 :(得分:0)
只需覆盖自定义视图的onDraw
方法,而不是编写自己的drawCanvas。
在此方法中,检查是否找到了Faces。如果是这样,请遍历它们,并使用canvas
作为onDraw
函数的参数在每张脸上绘制位图。
要在您的视图中触发onDraw
方法,只需在找到Faces后调用this.invalidate
您可以使用类似课程的内容。
用法将是:
MyView view = (MyView) findViewById(R.id.myview); //id in your xml
view.setImageResource(R.id.your_image);
view.computeFaces();
这就是电话MyView
:
public class MyView extends View {
private Face[] foundFaces;
public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyView(Context context) {
super(context);
}
public void computeFaces() {
//In here, do the Face finding processing, and store the found Faces in the foundFaces variable.
if (numberOfFaces> 0) {
this.invalidate();
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if ((foundFaces != null) && (foundFaces.length > 0)) {
for (Face f : foundFaces) {
canvas.drawBitmap(
//your square bitmap,
//x position of the face,
//y position of the face,
//your define paint);
}
}
}
}