Sample 2 was updated for OpenCV Manager model.

This commit is contained in:
Alexander Smorkalov 2012-06-26 12:31:04 +00:00
parent 207f4fc53c
commit 521b89e69d

View File

@ -1,5 +1,9 @@
package org.opencv.samples.tutorial2; package org.opencv.samples.tutorial2;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.DialogInterface; import android.content.DialogInterface;
@ -24,6 +28,38 @@ public class Sample2NativeCamera extends Activity {
private Sample2View mView; private Sample2View mView;
private BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
{
Log.i(TAG, "OpenCV loaded successfully");
// Create and set View
mView = new Sample2View(mAppContext);
setContentView(mView);
// Check native OpenCV camera
if( !mView.openCamera() ) {
AlertDialog ad = new AlertDialog.Builder(mAppContext).create();
ad.setCancelable(false); // This blocks the 'BACK' button
ad.setMessage("Fatal error: can't open camera!");
ad.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
finish();
}
});
ad.show();
}
} break;
default:
{
super.onManagerConnected(status);
} break;
}
}
};
public Sample2NativeCamera() { public Sample2NativeCamera() {
Log.i(TAG, "Instantiated new " + this.getClass()); Log.i(TAG, "Instantiated new " + this.getClass());
} }
@ -32,6 +68,7 @@ public class Sample2NativeCamera extends Activity {
protected void onPause() { protected void onPause() {
Log.i(TAG, "onPause"); Log.i(TAG, "onPause");
super.onPause(); super.onPause();
if (null != mView)
mView.releaseCamera(); mView.releaseCamera();
} }
@ -39,7 +76,7 @@ public class Sample2NativeCamera extends Activity {
protected void onResume() { protected void onResume() {
Log.i(TAG, "onResume"); Log.i(TAG, "onResume");
super.onResume(); super.onResume();
if( !mView.openCamera() ) { if((null != mView) && !mView.openCamera() ) {
AlertDialog ad = new AlertDialog.Builder(this).create(); AlertDialog ad = new AlertDialog.Builder(this).create();
ad.setCancelable(false); // This blocks the 'BACK' button ad.setCancelable(false); // This blocks the 'BACK' button
ad.setMessage("Fatal error: can't open camera!"); ad.setMessage("Fatal error: can't open camera!");
@ -59,8 +96,12 @@ public class Sample2NativeCamera extends Activity {
Log.i(TAG, "onCreate"); Log.i(TAG, "onCreate");
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); requestWindowFeature(Window.FEATURE_NO_TITLE);
mView = new Sample2View(this); Log.i(TAG, "Trying to load OpenCV library");
setContentView(mView); if (!OpenCVLoader.initAsync(OpenCVLoader.OPEN_CV_VERSION_2_4_0, this, mOpenCVCallBack))
{
Log.e(TAG, "Cannot connect to OpenCV Manager");
finish();
}
} }
@Override @Override