2

I have written some Android code were I basically only take a picture with the built in camera, save it, and then try to load the saved file to do some stuff with it.

Camera camera = mPreview.getmCameraInstance();
Camera.PictureCallback pictureCallback =
    GetPictureCallback(mPreview.getmCameraInstance());
camera.takePicture(null, null, pictureCallback);
Mat image = Highgui.imread(String.valueOf(file)); //Load Image here

However, my application crashes because it will not find a file in the line where i load my image. I do not understand this, because I thought that my code would execute in a sequential manner, and then the takePicture(...) in which I store my file would already be done.

Can you help me with this?

EDIT

When I put a Thread.sleep(100) before, I can open the image, as it has been successfully written at that time.

Buddy
  • 10,874
  • 5
  • 41
  • 58
PKlumpp
  • 4,913
  • 8
  • 36
  • 64

1 Answers1

1

You need to wait until your callback has been called in order to read the image.

What is GetPictureCallback? If it's your own code, then move the image reading into the generated callback... if not, the you can do something like this:

Camera camera = mPreview.getmCameraInstance();
final Camera.PictureCallback otherPictureCallback =
    GetPictureCallback(mPreview.getmCameraInstance());
camera.takePicture(null, null, new Camera.PictureCallback {
    void onPictureTaken(byte[] data, Camera camera) {
        otherPictureCallback.onPictureTaken(data, camera);
        Mat image = Highgui.imread(String.valueOf(file)); //Load Image here     
    }
});
Buddy
  • 10,874
  • 5
  • 41
  • 58