1. 程式人生 > >CameraView Android 相機預覽控制元件

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();
                }
            });
});

實現效果:

在這裡插入圖片描述