2016-04-17 12 views
-1
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で顔を検出しています。この顔検出コードは、私に眼の距離などの正の出力を与えていますそれは顔に長方形のボックスを表示していない誰もこれについて助けることができますか?顔に赤い矩形のボックスが表示されない

答えて

0

独自のdrawCanvasをコーディングする代わりに、カスタムビューのonDrawメソッドをオーバーライドするだけです。

この方法では、顔が検出されているかどうかを確認します。その場合は、それらをループし、onDraw関数のパラメータとして与えられたcanvasを使用して、各面にビットマップを描画します。

単にあなたが顔

を発見した後、あなたは次のようにクラスのようなものを使用することができthis.invalidateを呼び出して、あなたのビューにonDrawメソッドをトリガーします。

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); 
     } 
    } 
    } 
} 
関連する問題