所以我试图以固定的时间间隔拍摄多张照片,但是在surfaceView上预览第一张照片后我得到了一个"takePicture Failed" Exception

这是按下按钮时调用的 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();
        }
    }

这是 onPictureTaken()CustomPictureCallback 方法:
@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 所建议的,我在 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();
    }
}

关于java - 定期拍照(Android Camera API),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24135045/

10-11 07:58