scana
scana

Reputation: 2876

Only last photo is saved when using camera.takePicture() multiple times

I think that my problem is described quite well in the title.

Basically, what I`m trying to do is this:

takeFocusedPhoto("a");
doSomethingElse();
takeFocusedPhoto("b");
doSomethingElse();
takeFocusedPhoto("c");
doSomethingElse();
takeFocusedPhoto("d");

But my code only manage to save the last one (d.jpg).

doSomethingElse() is actualy a function to rotate platform on which my cell phone stands (I want to take picture of every wall in the room.

Is there an easy way to fix this? I`d appreciate any kind of help or suggestion.

The code:

/** Takes photo with default settings */
public void takePhoto(){

    camera.startPreview();

    camera.takePicture(null, rawCallback, jpegCallback);

    camera.stopPreview();

}
/** Takes photo using Auto Focus function */
public void takeFocusedPhoto(String name){

    filename=name;
    isAutoFocused=false;
    camera.startPreview();

        AutoFocusClbk afcb = new AutoFocusClbk();
        camera.autoFocus(afcb);

        if(isAutoFocused){
            Log.d(TAG, "takeFocusedPhoto - AutoFocused");   
        }
        else{

            Log.d(TAG, "takeFocusedPhoto - NOT AutoFocused");

        }

        camera.takePicture(null, rawCallback, jpegCallback);


    camera.stopPreview();

}
/** Releases the interface */
public void closeCamera(){

    camera.release();

}
/** Shutter */
/*ShutterCallback shutterCallback = new ShutterCallback() {
    public void onShutter() {
        Log.d(TAG, "onShutter'd");
    }
};*/

/** Handles data for raw picture */
PictureCallback rawCallback = new PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
        Log.d(TAG, "onPictureTaken - RAW");
    }
};
/** Handles data for jpeg picture */
PictureCallback jpegCallback = new PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
        FileOutputStream outStream = null;
        try {
            //write the file to sdcard
            outStream = new FileOutputStream(String.format("/sdcard/"+filename+".jpg"));    
            outStream.write(data);
            outStream.close();
            Log.d(TAG, "onPictureTaken - SUCCESS! - wrote bytes: " + data.length);
        } catch (FileNotFoundException e) {
            Log.d(TAG, "onPictureTaken - FAILURE - FileNotFound");
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
        Log.d(TAG, "onPictureTaken - JPEG");
    }
};
/** AutoFocus Callback */
private class AutoFocusClbk implements Camera.AutoFocusCallback{

    public void onAutoFocus(boolean success, Camera camera){

        isAutoFocused = success;
        Log.d(TAG, "onAutoFocus - AutoFocused: "+success);
    }

};
}

Upvotes: 3

Views: 1194

Answers (1)

sgarman
sgarman

Reputation: 6182

The call to takePicture is asynchronous, that is why you pass in a callback, which, is fired when the photo data is ready. When you call takeFocusedPhoto() back to back, all of them are getting fired BEFORE even the first takePicture is. Looking at the method body of takeFocusedPhoto you are setting the class member variable filename equal to the name argument. What this means is that filename is getting set to a, b, c and then d before jpegCallback gets fired, which means your are saving each photo to d. You are going to need a better way to get the filename in jpegCallback.

You could take the photos synchronously by placing the call to the next photo in jpegCallback and just keep a count on where you are. If you don't know how many photos you are going to have you could create an array and create a filename on demand in jpeg photo using a random string (i use time in mili) and add the filename to the array for use later.

Upvotes: 3

Related Questions