【问题标题】:Want to capture multiple images at once in android camera想在android相机中一次捕捉多张图像
【发布时间】:2013-10-15 10:33:21
【问题描述】:

我想使用 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 张照片(图像),没有任何警告消息或对话框。这可能吗? 提前致谢。

【问题讨论】:

  • 只需从其他部分删除您的对话框代码..然后使用 onCreate(null);
  • 我试过了。但它只拍了一张照片。我需要自动拍摄四张照片。
  • 你需要循环。
  • 我试过但它没有调用 mCamera.takePicture(shutterCallback, null, mPicture);这个功能。拍照失败的错误来这里

标签: android android-camera surfaceview android-framelayout


【解决方案1】:

在您的按钮点击事件中尝试类似下面的内容..

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

【讨论】:

  • 这不起作用。它没有显示第二张照片的相机预览。它只拍了一张照片
  • @Seetha 是的,现在我编辑了我的答案。我忘了在 while 循环中添加文件名。只要您尝试保存拍摄的图像,只需使用该文件名即可。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-19
  • 1970-01-01
  • 2012-11-03
  • 2020-03-17
  • 1970-01-01
  • 2016-02-29
相关资源
最近更新 更多