CameraView Android 相机预览控件

Github地址:https://github.com/google/cameraview

该控件为Google开源,非官方,只为了开发人员轻松集成Camera功能。

混淆:

发布release版本时,请在主module中得proguard-rules.pro文件中加入 -ignorewarnings,否则会出现waring警告导致构建失败。

-ignorewarnings

权限:

<uses-permission android:name="android.permission.CAMERA" />

XML:

?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btn_take_picture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:text="拍照" />

    <Button
        android:id="@+id/btn_start_camera"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn_take_picture"
        android:background="@android:color/white"
        android:text="开启相机" />

    <Button
        android:id="@+id/btn_stop_camera"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn_start_camera"
        android:background="@android:color/white"
        android:text="关闭相机" />

    <com.google.android.cameraview.CameraView
        android:id="@+id/camera_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:keepScreenOn="true"
        app:aspectRatio="4:3"
        app:autoFocus="true"
        app:facing="back"
        app:flash="auto" />
RelativeLayout>

代码:

mCameraView = (CameraView) findViewById(R.id.camera_view);
//为控件设置回调方法,相机开启,停止预览的时候会走该回调方法。
mCameraView.addCallback(new CameraView.Callback() {
    @Override
    public void onCameraOpened(CameraView cameraView) {
        //相机打开调用
        Toast.makeText(MainActivity.this, "打开相机回调", Toast.LENGTH_SHORT).show();
    }
    @Override
    public void onCameraClosed(CameraView cameraView) {
        //相机关闭调用
        Toast.makeText(MainActivity.this, "关闭相机回调", Toast.LENGTH_SHORT).show();
    }
    @Override
    public void onPictureTaken(CameraView cameraView, final byte[] data) {
        //data 相机拍摄得到得图片,可以通过字节流写入文件中保存。
        //FileIOUtils.writeFileFromBytesByStream(filePath, data);
        Toast.makeText(MainActivity.this, "抓取图片回调   " + data.toString(), Toast.LENGTH_SHORT).show();
    }
});
//拍照
findViewById(R.id.btn_take_picture).setOnClickListener(v -> {
    new RxPermissions(this)
            .requestEach(Manifest.permission.CAMERA)
            .subscribe(permission -> {
                if (permission.granted) {
                    if (mCameraView.isCameraOpened()) {
                        mCameraView.takePicture();
                    }else {
                        Toast.makeText(MainActivity.this, "请开启相机后再进行拍照 ", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(MainActivity.this, "请打开相机权限", Toast.LENGTH_SHORT).show();
                }
            });
});
//启动相机并预览
findViewById(R.id.btn_start_camera).setOnClickListener(v -> {
    new RxPermissions(this)
            .requestEach(Manifest.permission.CAMERA)
            .subscribe(permission -> {
                if (permission.granted) {
                    if (!mCameraView.isCameraOpened()) {
                        mCameraView.start();
                    }
                } else {
                    Toast.makeText(MainActivity.this, "请打开相机权限", Toast.LENGTH_SHORT).show();
                }
            });
});
//关闭相机
findViewById(R.id.btn_stop_camera).setOnClickListener(v -> {
    new RxPermissions(this)
            .requestEach(Manifest.permission.CAMERA)
            .subscribe(permission -> {
                if (permission.granted) {
                    if (mCameraView.isCameraOpened()) {
                        mCameraView.stop();
                    }
                } else {
                    Toast.makeText(MainActivity.this, "请打开相机权限", Toast.LENGTH_SHORT).show();
                }
            });
});

实现效果:

CameraView Android 相机预览控件_第1张图片

你可能感兴趣的:(CameraView Android 相机预览控件)