Ask Your Question
0

Tutorial3-Camera Control: picture freezed after takePicture()

asked 2013-05-02 10:19:40 -0600

AAZZ gravatar image

updated 2013-05-05 08:23:49 -0600

[Update: Please see my update answer below this text]

When a picture is taken with the "OpenCV Tutorial3 - Camera Control" app the taken picture is freezed on the screen and the preview doesn't come back. I tested it with Smartphone Android 2.2.2 and Tablet Android 3.2.1 device: Both the same.

The relevant code:

Tutorial3Activity.java:

@SuppressLint("SimpleDateFormat")
@Override
public boolean onTouch(View v, MotionEvent event) {
    Log.i(TAG,"onTouch event");
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
    String currentDateandTime = sdf.format(new Date());
    String fileName = Environment.getExternalStorageDirectory().getPath() +
                           "/sample_picture_" + currentDateandTime + ".jpg";
    mOpenCvCameraView.takePicture(fileName);
    Toast.makeText(this, fileName + " saved", Toast.LENGTH_SHORT).show();
    return false;
}

Tutorial3View.java:

public void takePicture(final String fileName) {
    Log.i(TAG, "Tacking picture");
    PictureCallback callback = new PictureCallback() {

        private String mPictureFileName = fileName;

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            Log.i(TAG, "Saving a bitmap to file");
            Bitmap picture = BitmapFactory.decodeByteArray(data, 0, data.length);
            try {
                FileOutputStream out = new FileOutputStream(mPictureFileName);
                picture.compress(Bitmap.CompressFormat.JPEG, 90, out);
                picture.recycle();
                mCamera.startPreview();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return;
        }
    };

    mCamera.takePicture(null, null, callback);
}

What can I do to come back to the preview screen after taken a picture?

There is a mCamera.startPreview() command in the takePicture method but it seems not to have the effect to set the preview screen.

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
0

answered 2015-06-15 08:05:52 -0600

Just add

    mCamera.startPreview();
    mCamera.setPreviewCallback(this);

after your call to

   mCamera.takePicture(null, null, this);

I hope this helps.

edit flag offensive delete link more
0

answered 2013-05-05 08:20:11 -0600

AAZZ gravatar image

updated 2013-05-05 08:38:56 -0600

I've read the example has to been fixed:

https://github.com/Itseez/opencv/pull/838/files#r3914912

http://code.opencv.org/issues/2961

https://code.google.com/p/android/issues/detail?id=13966

I've changed my code to this:

public class Tutorial3View extends JavaCameraView implements PictureCallback {
public void takePicture(final String fileName) {
    //Log.i(TAG, "Taking picture");
    this.mPictureFileName = fileName;
    // Call to garbage collector to avoid bug http://code.opencv.org/issues/2961 
    System.gc();
    // PictureCallback is implemented by the current class
    mCamera.setPreviewCallback(null);
    mCamera.setOneShotPreviewCallback(null);
    mCamera.takePicture(null, null, this);
}


@Override
public void onPictureTaken(byte[] data, Camera camera) {
        //Log.i(TAG, "Saving a bitmap to file");
        // The camera preview was automatically stopped. Start it again.
        mCamera.startPreview();

        // Write the image in a file (in jpeg format)
        try {
          FileOutputStream fos = new FileOutputStream(mPictureFileName);

          fos.write(data);
          fos.close();

          //mCamera.startPreview();
        } catch (java.io.IOException e) {
          Log.e("PictureDemo", "Exception in photoCallback", e);
        }

}
}

But I have still the same problem: After taken the first picture the screen freezes and no preview is shown.

What can be done to fix the problem?

edit flag offensive delete link more

Comments

see the https://github.com/Itseez/opencv/pull/846

looks like you forgot to set back setPreviewCallback(this).

Andrey Pavlenko gravatar imageAndrey Pavlenko ( 2013-05-11 04:33:54 -0600 )edit

Question Tools

Stats

Asked: 2013-05-02 10:19:40 -0600

Seen: 7,728 times

Last updated: May 05 '13