首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检测到脸上的显示圈- Android

问检测到脸上的显示圈- Android
EN

Stack Overflow用户
提问于 2015-11-24 15:08:38
回答 1查看 1.6K关注 0票数 0

我做了视频采集和人脸检测应用。我希望在检测到的脸上显示一个圆圈,并在Myview类中使用以下方法,检查它是否有效:

代码语言:javascript
复制
  protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

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

    }

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-24 15:14:26

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

代码语言:javascript
复制
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/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33897128

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档