Commit 60bf0187 authored by poiuytrez's avatar poiuytrez

Fix android tutorial 3 second picture taken bug

parent a9a26950
...@@ -6,17 +6,16 @@ import java.util.List; ...@@ -6,17 +6,16 @@ import java.util.List;
import org.opencv.android.JavaCameraView; import org.opencv.android.JavaCameraView;
import android.content.Context; import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera; import android.hardware.Camera;
import android.hardware.Camera.PictureCallback; import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.Size; import android.hardware.Camera.Size;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.Log; import android.util.Log;
public class Tutorial3View extends JavaCameraView { public class Tutorial3View extends JavaCameraView implements PictureCallback {
private static final String TAG = "Sample::Tutorial3View"; private static final String TAG = "Sample::Tutorial3View";
private String mPictureFileName;
public Tutorial3View(Context context, AttributeSet attrs) { public Tutorial3View(Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs);
...@@ -56,26 +55,31 @@ public class Tutorial3View extends JavaCameraView { ...@@ -56,26 +55,31 @@ public class Tutorial3View extends JavaCameraView {
} }
public void takePicture(final String fileName) { public void takePicture(final String fileName) {
Log.i(TAG, "Tacking picture"); Log.i(TAG, "Taking picture");
PictureCallback callback = new PictureCallback() { this.mPictureFileName = fileName;
// Call to garbage collector to avoid bug http://code.opencv.org/issues/2961
private String mPictureFileName = fileName; System.gc();
@Override // PictureCallback is implemented by the current class
public void onPictureTaken(byte[] data, Camera camera) { mCamera.takePicture(null, null, this);
Log.i(TAG, "Saving a bitmap to file"); }
Bitmap picture = BitmapFactory.decodeByteArray(data, 0, data.length);
try { @Override
FileOutputStream out = new FileOutputStream(mPictureFileName); public void onPictureTaken(byte[] data, Camera camera) {
picture.compress(Bitmap.CompressFormat.JPEG, 90, out); Log.i(TAG, "Saving a bitmap to file");
picture.recycle(); // The camera preview was automatically stopped. Start it again.
mCamera.startPreview(); mCamera.startPreview();
} catch (Exception e) {
e.printStackTrace(); // Write the image in a file (in jpeg format)
} try {
} FileOutputStream fos = new FileOutputStream(mPictureFileName);
};
fos.write(data);
mCamera.takePicture(null, null, callback); fos.close();
} catch (java.io.IOException e) {
Log.e("PictureDemo", "Exception in photoCallback", e);
}
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment