【问题标题】:Display Circle on Face Detected- Android [duplicate]在检测到人脸时显示圆圈 - Android [重复]
【发布时间】:2016-02-27 02:22:41
【问题描述】:

我已经制作了视频捕捉和人脸检测应用程序。我想在检测到的人脸上显示一个圆圈,并在 Myview 类中有以下方法,检查它是否有效:

  protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawCircle(50f,50f,50f, paint);

    }

我有带有 CameraPreview 的相对布局。我在哪里可以调用 MyView 类来获得所需的功能?

【问题讨论】:

  • 改进的语法和格式。
  • 谢谢布鲁斯。你成功了!

标签: android android-studio android-camera face-detection


【解决方案1】:

您可以在使用画布检测到的面部上显示圆圈这是一个示例

import com.google.android.gms.vision.face.Face;

/**
* Created by echessa on 8/31/15.
*/
public class CustomView extends View {

private Bitmap mBitmap;
private SparseArray<Face> mFaces;

public CustomView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

/**
 * Sets the bitmap background and the associated face detections.
 */
void setContent(Bitmap bitmap, SparseArray<Face> faces) {
    mBitmap = bitmap;
    mFaces = faces;
    invalidate();
}

/**
 * Draws the bitmap background and the associated face landmarks.
 */
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if ((mBitmap != null) && (mFaces != null)) {
        double scale = drawBitmap(canvas);
        drawFaceRectangle(canvas, scale);
    }
}

/**
 * Draws the bitmap background, scaled to the device size.  Returns the scale for future use in
 * positioning the facial landmark graphics.
 */
private double drawBitmap(Canvas canvas) {
    double viewWidth = canvas.getWidth();
    double viewHeight = canvas.getHeight();
    double imageWidth = mBitmap.getWidth();
    double imageHeight = mBitmap.getHeight();
    double scale = Math.min(viewWidth / imageWidth, viewHeight / imageHeight);

    Rect destBounds = new Rect(0, 0, (int)(imageWidth * scale), (int)(imageHeight * scale));
    canvas.drawBitmap(mBitmap, null, destBounds, null);
    return scale;
}

/**
 * Draws a rectangle around each detected face
 */
private void drawFaceRectangle(Canvas canvas, double scale) {
    Paint paint = new Paint();
    paint.setColor(Color.GREEN);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(5);

    for (int i = 0; i < mFaces.size(); ++i) {
        Face face = mFaces.valueAt(i);
        canvas.drawRect((float)(face.getPosition().x * scale),
            (float)(face.getPosition().y * scale),
            (float)((face.getPosition().x + face.getWidth()) * scale),
            (float)((face.getPosition().y + face.getHeight()) * scale),
            paint);
    }
}

}

更多信息http://www.sitepoint.com/face-detection-in-android-with-google-play-services/

【讨论】:

  • 我的相机预览的RelativeLauout 将如何与此自定义视图集成。想不通
猜你喜欢
  • 2012-03-05
  • 1970-01-01
  • 2013-09-24
  • 2016-06-24
  • 2020-11-25
  • 2019-02-27
  • 2021-08-27
  • 2014-07-21
  • 1970-01-01
相关资源
最近更新 更多