Customize Camera Camera Continuous Shooting in Android

Last time we introduced the use of Camera to take pictures to achieve the effect of a custom camera. This time we are based on the last project to achieve continuous shooting effect. Last blog path was offered

The use of custom camera Camera in Android

Okay, let's start with the topic.

Based on the last project, first we add some required global variables.

//连拍属性
private boolean isShooting = false;//是否正在连拍
private int shooting_num = 0;//已经拍摄的相片数量
private ArrayList<String> mShootingArray;//连拍的相片保存路径队列

Then we write a method to perform continuous shooting

/**
 * 执行连拍动作。外部调用该方法完成连拍
 */
public void doTakeShooting(){
    mShootingArray = new ArrayList<>();
    isShooting = true;
    shooting_num = 0;
}

Then modify the method triggered by the click event

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.bt_take_photo:
            //点击拍照
            //doTakePicture();
            //点击连拍
            doTakeShooting();
            break;
    }
}

Then we let go of a line of code in the last project. You may find that there is such a sentence in the surfaceChanged callback method of the SurfaceHolder.Callback callback last time

//设置相机的预览监听器。注意这里的setPreviewCallback给连拍功能用
//            mCamera.setPreviewCallback(mPr);

We just open this comment.

Then we add this monitor, the code is as follows

    /**
     * 定义一个画面预览的回调监听器,在此可捕获动态的连续图片
     */
    private Camera.PreviewCallback mPr = new Camera.PreviewCallback() {
        @Override
        public void onPreviewFrame(byte[] data, Camera camera) {
            if (!isShooting){
                return;
            }
            //获取相机的参数信息
            Camera.Parameters parameters = camera.getParameters();
            //获得预览数据的格式
            int imageFormat = parameters.getPreviewFormat();
            int width = parameters.getPreviewSize().width;
            int height = parameters.getPreviewSize().height;
            Rect rect = new Rect(0,0,width,height);
            //创建一个YUV格式的图像对象
            YuvImage yuvImage = new YuvImage(data,imageFormat,width,height,null);
            try {
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                yuvImage.compressToJpeg(rect,80,bos);
                //从字节数组中解析出位图数据
                Bitmap raw = BitmapFactory.decodeByteArray(bos.toByteArray(),0,bos.size());
                //旋转位图
                Bitmap bitmap = getRotateBitmap(raw,(mCameraType == CAMERA_BEHIND)?90:-90);
                //获取本次拍摄的照片路径
                List<String> listPath = new ArrayList<>();
                listPath.add("myCamera");
                listPath.add("photos");
                String path = PathGetUtil.getLongwayPath(MainActivity.this, listPath);
                File fileDir = new File(path);
                if (!fileDir.exists()) {
                    fileDir.mkdirs();
                }
                File filePic = new File(path, "ww" + System.currentTimeMillis() + ".jpg");
                if (!filePic.exists()) {
                    try {
                        filePic.createNewFile();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                saveImage(filePic.getPath(), bitmap);
                //在此进入预览画面
                camera.startPreview();
                shooting_num++;
                mShootingArray.add(path);
                if (shooting_num>8){
                    //每次连拍9张
                    isShooting = false;
                    ToastUtil.toastWord(MainActivity.this,"已完成连拍");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

In this way, when we click to take a picture, we will directly take nine pictures in a row.

Guess you like

Origin blog.csdn.net/weixin_38322371/article/details/115161027