mirror of
https://github.com/opencv/opencv.git
synced 2024-11-25 03:30:34 +08:00
Merge pull request #846 from smart-mobile-software:tuto_crash
This commit is contained in:
commit
2faa2adae2
@ -208,6 +208,8 @@ public class JavaCameraView extends CameraBridgeViewBase implements PreviewCallb
|
||||
synchronized (this) {
|
||||
if (mCamera != null) {
|
||||
mCamera.stopPreview();
|
||||
mCamera.setPreviewCallback(null);
|
||||
|
||||
mCamera.release();
|
||||
}
|
||||
mCamera = null;
|
||||
|
@ -6,17 +6,16 @@ import java.util.List;
|
||||
import org.opencv.android.JavaCameraView;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.hardware.Camera;
|
||||
import android.hardware.Camera.PictureCallback;
|
||||
import android.hardware.Camera.Size;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.Log;
|
||||
|
||||
public class Tutorial3View extends JavaCameraView {
|
||||
public class Tutorial3View extends JavaCameraView implements PictureCallback {
|
||||
|
||||
private static final String TAG = "Sample::Tutorial3View";
|
||||
private String mPictureFileName;
|
||||
|
||||
public Tutorial3View(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
@ -56,26 +55,33 @@ public class Tutorial3View extends JavaCameraView {
|
||||
}
|
||||
|
||||
public void takePicture(final String fileName) {
|
||||
Log.i(TAG, "Tacking picture");
|
||||
PictureCallback callback = new PictureCallback() {
|
||||
Log.i(TAG, "Taking picture");
|
||||
this.mPictureFileName = fileName;
|
||||
// Postview and jpeg are sent in the same buffers if the queue is not empty when performing a capture.
|
||||
// Clear up buffers to avoid mCamera.takePicture to be stuck because of a memory issue
|
||||
mCamera.setPreviewCallback(null);
|
||||
|
||||
private String mPictureFileName = fileName;
|
||||
// PictureCallback is implemented by the current class
|
||||
mCamera.takePicture(null, null, this);
|
||||
}
|
||||
|
||||
@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();
|
||||
// The camera preview was automatically stopped. Start it again.
|
||||
mCamera.startPreview();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
};
|
||||
mCamera.setPreviewCallback(this);
|
||||
|
||||
// Write the image in a file (in jpeg format)
|
||||
try {
|
||||
FileOutputStream fos = new FileOutputStream(mPictureFileName);
|
||||
|
||||
fos.write(data);
|
||||
fos.close();
|
||||
|
||||
} catch (java.io.IOException e) {
|
||||
Log.e("PictureDemo", "Exception in photoCallback", e);
|
||||
}
|
||||
|
||||
mCamera.takePicture(null, null, callback);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user