Android获取前置摄像头实现静默拍照

Android获取前置摄像头静默拍照

在实现入侵警报功能时需要获取系统前置摄像头实现后台静默拍照,并将数据存储到数据库中以备用户查看。具体步骤如下:

1.获取使用相机权限

ActivityCompat.requestPermissions(SomeActivity.this,new String[]{Manifest.permission.CAMERA},1); 

2.在拍照所在activity对应的xml文件中添加宽高为0.1dp的SurfaceView

<SurfaceView
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:id="@+id/camera_surfaceview"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_width="0.1dp"
        android:layout_height="0.1dp"
        />

3.在activity中进行摄像头获取、初始化等相关操作

1、初始化

SurfaceViewHolder myHolder = surfaceViewHolder.getHolder();
myHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

2、获取摄像头

Camera camera = getCamera();
/**
     * 获取前置摄像头
     * @return
     */
    public Camera getCamera() {
        camera = null;
        Camera.CameraInfo info = new Camera.CameraInfo();
        int cnt = Camera.getNumberOfCameras();
        for (int i = 0;i<cnt;i++) {
            Camera.getCameraInfo(i,info);

            if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                try {
                    camera = Camera.open(i);
                } catch (RuntimeException e) {
                    e.printStackTrace();
                }
            }
        }
        return camera;
    }

3、调用摄像头进行拍照

                if (camera != null) {
                            try {
                                camera.setPreviewDisplay(myHolder);
                                camera.startPreview();
                                camera.takePicture(null, null, jpeg);
                                camera.setErrorCallback(callback);
                            } catch(IOException e) {
                                e.printStackTrace();
                                camera.stopPreview();
                                camera.release();
                                camera=null;
                            }

                        } else {
                // camera为空 
                }
//onCreate()函数外部
 final Camera.PictureCallback jpeg = (data, camera) -> {
        //根据拍照所得数据创建位图

        Bitmap bitmap = BitmapFactory.decodeByteArray(data,0,data.length);
        //执行照片存储等相关操作,注意bitmap的回收
        //...
        camera.stopPreview();
    };


    Camera.ErrorCallback callback = new Camera.ErrorCallback() {
        @Override
        public void onError(int error, Camera camera) {
            switch (error) {
                case Camera.CAMERA_ERROR_SERVER_DIED:
                    Log.e("error", "Camera.CAMERA_ERROR_SERVER_DIED");
                    //这里重新初始化Camera即可

                    break;
                case Camera.CAMERA_ERROR_UNKNOWN:
                    Log.e("error", "Camera.CAMERA_ERROR_UNKNOWN");
                    break;
            }
        }
    };

4.在onDestroy()中进行Camera对象的回收处理

@Override
public void onDestroy() {
    super.onDestroy();
    if (camera != null) {
        camera.release();
        camera = null;
    }
}

Android获取前置摄像头实现静默拍照

上一篇:uni-app多种设置全局变量及全局变量重新赋值


下一篇:Hadoop知识点总结