在Android相机中一次性拍摄多张照片

3

我希望能够使用Android相机一次捕获多张图像。当用户点击AlertDialog上的“是”按钮时,我可以捕获多张图像。但是我希望在不按任何按钮的情况下捕获图像。我需要自动捕获四张图像。

captureButton.setOnClickListener( new OnClickListener() {
        public void onClick(View v) {
mCamera.takePicture(shutterCallback, null, mPicture);
            new Handler().postDelayed(new Runnable() {
                public void run() {

                    if(count==4 || count > 4) //static variable count = 0
                        {

                        showAlertMSGOnUIThread("Warning","You have taken 4 photos");

                       }

                    else if(count<4)
                    {

                        final AlertDialog.Builder builder1 = new AlertDialog.Builder(ScanBill.this);


                        final TextView msg = new TextView(ScanBill.this);
                        final TextView title = new TextView(ScanBill.this);
                        title.setText("Warning");
                        title.setPadding(0, 10, 0, 10);
                        title.setTextSize(20);
                        title.setGravity(Gravity.CENTER);
                        title.setTextColor(Color.WHITE);
                        title.setBackgroundColor(Color.parseColor("#FF0D4C6A"));
                        msg.setText("Picture taken successfully would you like to Retake?");
                        msg.setPadding(0, 10, 0, 10);
                        msg.setGravity(Gravity.CENTER);
                        msg.setTextSize(18);
                        // msg.setBackgroundColor(Color.parseColor("#FF0D4C6A"));
                        Utilities.writeIntoLog("UI Init successfull");

                        AlertDialog.Builder builder = new AlertDialog.Builder(
                                ScanBill.this);
                        builder.setCustomTitle(title);
                        builder.setView(msg).setPositiveButton("YES",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                            int id) {
                                        dialog.dismiss();

                                        onCreate(null);

                                    }
                                });


                        builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,
                                    int id) {

                                dialog.dismiss();
                            }
                        });
                        AlertDialog alert = builder.create();
                        alert.setCanceledOnTouchOutside(false);
                        alert.show();
                        Utilities.writeIntoLog("Alert Display Successfull");



                    }


                }
            }, 1000); 



        }
    });

这是我的PictureCallback实现:

PictureCallback mPicture = new PictureCallback() {

    public void onPictureTaken(byte[] data, Camera camera) {

        //File pictureFile = getOutputMediaFile();
        //if (pictureFile == null){
            //return;
        //}
        System.out.println("PictureCallback() method is called");
        FileOutputStream outStream = null;
        try {
            String file_path = Environment
            .getExternalStorageDirectory()
            + "/ScanBills/"+customerName;
            // write to local sandbox file system
            //                  outStream = CameraDemo.this.openFileOutput(String.format("%d.jpg", System.currentTimeMillis()), 0); 
            // Or write to sdcard
            File dir = new File(file_path);
            if (!dir.exists())
                dir.mkdirs();

            File file = new File(dir, "Bill_ "+count+".JPEG");
            outStream = new FileOutputStream(file); 
            outStream.write(data);
            outStream.close();
            count=count+1;
            System.out.println("Picture count :"+count);

        } catch (FileNotFoundException e) {

        } catch (IOException e) {

        }

    }

};

一旦我按下拍摄按钮,我希望能够拍摄4张照片,而不会出现任何警报消息或对话框。这种情况是否可能? 提前感谢。

只需从“else”部分中删除您的对话框代码,然后只使用“onCreate(null);”。 - Sagar Maiyad
我已经尝试过了。但它只拍了一张照片。我需要自动拍摄四张照片。 - Seetha
你需要循环实现这个。 - Sagar Maiyad
我已经尝试过了,但它没有调用mCamera.takePicture(shutterCallback, null, mPicture)这个函数。在这里出现了拍照失败的错误。 - Seetha
1个回答

0
在您的按钮点击事件中尝试以下类似的代码:
new Thread(new Runnable() {

            @Override
            public void run() {
                while(count < 4) {
                    FileNm = "Image" + count + ".jpg";
                       mCamera.takePicture(null, mPictureCallback, mPictureCallback);
                       count++;
                   try {
                    Thread.sleep(3000);
                } catch (InterruptedException exception) {
                    exception.printStackTrace();
                }
            }
        }
}).start();

1
这个不起作用。它没有显示第二张照片的相机预览。它只拍了一张照片。 - Seetha
@Seetha 是的,现在我编辑了我的答案。我忘记在 while 循环中添加文件名了...只需在尝试保存所拍摄图像时使用该文件名即可。 - Sagar Maiyad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接