按照固定时间间隔拍照(Android相机API)

4

我正在尝试以固定时间间隔拍摄多张照片,但是在第一张照片在surfaceView上预览后,我会收到一个"takePicture Failed"异常。

这是我的takePictures()方法,在按下按钮时调用:

public void takePictures() {
        if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
            camera = Camera.open();

            if (camera != null) {
                try {
                    camera.setPreviewDisplay(surfaceView.getHolder());
                    camera.startPreview();

                    camera.takePicture(null, null, new CustomPictureCallbcak(this, cacheDir, imageView, 3, 5000));
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        else {
            Toast.makeText(this, "No camera found.", Toast.LENGTH_SHORT).show();
        }
    }

以下是CustomPictureCallback类中的onPictureTaken()方法:

@Override
    public void onPictureTaken(byte[] data, Camera camera) {
        //get date info for file name
        SimpleDateFormat sdf = new SimpleDateFormat("ddmmyyyyhhmmss");
        String date = sdf.format(new Date());
        String fileDir = createImageFileName(date);

        //write the image to cache 
        writeImageToCache(fileDir, data);

        //display file name in a toast notification
        Toast.makeText(c, fileDir, Toast.LENGTH_SHORT).show();

        //show picture on imageview
        imageView.setImageBitmap(BitmapFactory.decodeByteArray(data, 0, data.length));

        //retake images
        this.camera = camera;
        while (numOfImagesAlreadyTaken <= numOfImages) {
            Thread thread = new Thread() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    super.run();
                    try {
                        numOfImagesAlreadyTaken++;
                        CustomPictureCallbcak.this.camera.stopPreview();
                        sleep(delay);
                        CustomPictureCallbcak.this.camera.takePicture(null, null, CustomPictureCallbcak.this);  
                    }
                    catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };

            thread.start();
        }

            Toast.makeText(c, "Total images taken: " + numOfImagesAlreadyTaken, Toast.LENGTH_SHORT).show();

            //release camera
            camera.release();
            camera = null;

    }

你在哪里抛出异常? - Aleksander Lidtke
@AleksanderLidtke,在第一张照片被拍摄后,也就是新线程启动时。 - Manas Bajaj
1
为什么要在 OnPictureTaken 中创建拍照线程呢?将 while 循环放在 takePictures 中并保持回调方法相对简单不是更容易和更清晰吗?我已经编写了一段代码,就像你试图做的那样,它从一开始就可以正常工作。 - Aleksander Lidtke
@AleksanderLidtke,非常感谢,它起作用了。请看一下我添加的解决方案。 - Manas Bajaj
1
没问题,很高兴能帮到你 :) - Aleksander Lidtke
1个回答

4

正如Aleksander Lidtke所建议的,我在takePictures()方法中创建了一个单独的线程,并在其中放置了一个while循环:

public void takePictures(final int numOfPictures, final int delay) {

    if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {

        Thread thread = new Thread() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                super.run();
                while (numOfPicturesAlreadyTaken <= numOfPictures) {
                    try {
                        camera = Camera.open();
                        camera.setPreviewDisplay(surfaceView.getHolder());
                        camera.startPreview();
                        camera.takePicture(null, null, new CustomPictureCallbcak(MainActivity.this, cacheDir, imageView));
                        numOfPicturesAlreadyTaken++;
                        sleep(delay);
                    }
                    catch (Exception e) {
                        e.printStackTrace();
                        Log.d("TEST", e.getMessage());
                    }
                }
            }
        };
        thread.start();
    }
    else {
        Toast.makeText(this, "No camera found.", Toast.LENGTH_SHORT).show();
    }
}

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