Android Studio - 在相机预览上绘制人脸检测(实时人脸检测)

时间:2016-10-07 17:22:11

标签: java android-studio face-detection android-vision

我创建了一个显示手机正面相机的基本应用程序。 我需要使用Google Servies面部检测器在脸部周围绘制矩形 (在FrameLayout上,包含相机预览数据)。 我知道面部检测器的基本用法,我知道如何在图像视图中检测面部。有人可以帮我在我的应用程序的框架布局上应用面部检测吗?非常感谢。

我的代码:

public class MainActivity extends AppCompatActivity {

private ImageSurfaceView mImageSurfaceView;
private Camera camera;
private FrameLayout cameraPreviewLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    cameraPreviewLayout = (FrameLayout)findViewById(R.id.camera_preview);

    camera = checkDeviceCamera();

    mImageSurfaceView = new ImageSurfaceView(MainActivity.this, camera);
    mImageSurfaceView.draw(canvas);
    cameraPreviewLayout.addView(mImageSurfaceView);
}


private Camera checkDeviceCamera() {
    Camera mCamera = null;
    try {
        mCamera = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT); // set the cameras to the front (selfi)
    } catch (Exception e) {
        e.printStackTrace();
    }
    return mCamera;
} 
}

ImageSurfaceView类:

public class ImageSurfaceView extends SurfaceView implements   SurfaceHolder.Callback {

private Camera camera;
private SurfaceHolder surfaceHolder;

public ImageSurfaceView(Context context, Camera camera) {
    super(context);
    this.camera = camera;

    this.surfaceHolder = getHolder();
    this.surfaceHolder.addCallback(this);
}

@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
    try {
        this.camera.setPreviewDisplay(surfaceHolder);
        this.camera.startPreview();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
}

@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
    this.camera.startPreview();
    this.camera.release();
}  

0 个答案:

没有答案