From bef6de902551d8203a0fa9aab5773aa5f8c78c1c Mon Sep 17 00:00:00 2001 From: poiuytrez Date: Wed, 1 May 2013 16:58:15 +0200 Subject: [PATCH] Fix tuto3 picture taken crash on all devices A modification of the JavaCameraView is needed to avoid a crash when the app is exited. It is a good practice to remove the callback after the stopPreview method. --- modules/java/generator/src/java/android+JavaCameraView.java | 2 ++ .../src/org/opencv/samples/tutorial3/Tutorial3View.java | 5 +++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/modules/java/generator/src/java/android+JavaCameraView.java b/modules/java/generator/src/java/android+JavaCameraView.java index f07b7d2ca8..dec6cac823 100644 --- a/modules/java/generator/src/java/android+JavaCameraView.java +++ b/modules/java/generator/src/java/android+JavaCameraView.java @@ -179,6 +179,8 @@ public class JavaCameraView extends CameraBridgeViewBase implements PreviewCallb synchronized (this) { if (mCamera != null) { mCamera.stopPreview(); + mCamera.setPreviewCallback(null); + mCamera.release(); } mCamera = null; diff --git a/samples/android/tutorial-3-cameracontrol/src/org/opencv/samples/tutorial3/Tutorial3View.java b/samples/android/tutorial-3-cameracontrol/src/org/opencv/samples/tutorial3/Tutorial3View.java index ef15cb5ee0..38d613bba4 100644 --- a/samples/android/tutorial-3-cameracontrol/src/org/opencv/samples/tutorial3/Tutorial3View.java +++ b/samples/android/tutorial-3-cameracontrol/src/org/opencv/samples/tutorial3/Tutorial3View.java @@ -57,8 +57,8 @@ 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(); + // Clear up buffers to avoid bug http://code.opencv.org/issues/2961 + mCamera.setPreviewCallback(null); // PictureCallback is implemented by the current class mCamera.takePicture(null, null, this); @@ -69,6 +69,7 @@ public class Tutorial3View extends JavaCameraView implements PictureCallback { Log.i(TAG, "Saving a bitmap to file"); // The camera preview was automatically stopped. Start it again. mCamera.startPreview(); + mCamera.setPreviewCallback(this); // Write the image in a file (in jpeg format) try {