From 109e308763f982b9e1cdf7a9c7c6d42596ca8e5a Mon Sep 17 00:00:00 2001 From: Alexander Smorkalov Date: Tue, 26 Jun 2012 12:57:43 +0000 Subject: [PATCH] Sample4-mixed was updated for OpenCV Manager model. --- .../samples/tutorial4/Sample4Mixed.java | 55 +++++++++++++++++-- .../opencv/samples/tutorial4/Sample4View.java | 5 -- 2 files changed, 51 insertions(+), 9 deletions(-) diff --git a/samples/android/tutorial-4-mixed/src/org/opencv/samples/tutorial4/Sample4Mixed.java b/samples/android/tutorial-4-mixed/src/org/opencv/samples/tutorial4/Sample4Mixed.java index 9bd36dc784..fd11f4e27b 100644 --- a/samples/android/tutorial-4-mixed/src/org/opencv/samples/tutorial4/Sample4Mixed.java +++ b/samples/android/tutorial-4-mixed/src/org/opencv/samples/tutorial4/Sample4Mixed.java @@ -1,5 +1,9 @@ package org.opencv.samples.tutorial4; +import org.opencv.android.BaseLoaderCallback; +import org.opencv.android.LoaderCallbackInterface; +import org.opencv.android.OpenCVLoader; + import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; @@ -18,6 +22,42 @@ public class Sample4Mixed extends Activity { private MenuItem mItemPreviewFeatures; private Sample4View mView; + private BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) { + @Override + public void onManagerConnected(int status) { + switch (status) { + case LoaderCallbackInterface.SUCCESS: + { + Log.i(TAG, "OpenCV loaded successfully"); + + // Load native library after(!) OpenCV initialization + System.loadLibrary("mixed_sample"); + + // Create and set View + mView = new Sample4View(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 Sample4Mixed() { Log.i(TAG, "Instantiated new " + this.getClass()); @@ -27,14 +67,15 @@ public class Sample4Mixed extends Activity { protected void onPause() { Log.i(TAG, "onPause"); super.onPause(); - mView.releaseCamera(); + if (null != mView) + mView.releaseCamera(); } @Override protected void onResume() { Log.i(TAG, "onResume"); super.onResume(); - if( !mView.openCamera() ) { + if((null != mView) && !mView.openCamera() ) { AlertDialog ad = new AlertDialog.Builder(this).create(); ad.setCancelable(false); // This blocks the 'BACK' button ad.setMessage("Fatal error: can't open camera!"); @@ -53,9 +94,15 @@ public class Sample4Mixed extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.i(TAG, "onCreate"); + requestWindowFeature(Window.FEATURE_NO_TITLE); - mView = new Sample4View(this); - setContentView(mView); + + Log.i(TAG, "Trying to load OpenCV library"); + if (!OpenCVLoader.initAsync(OpenCVLoader.OPEN_CV_VERSION_2_4_0, this, mOpenCVCallBack)) + { + Log.e(TAG, "Cannot connect to OpenCV Manager"); + finish(); + } } public boolean onCreateOptionsMenu(Menu menu) { diff --git a/samples/android/tutorial-4-mixed/src/org/opencv/samples/tutorial4/Sample4View.java b/samples/android/tutorial-4-mixed/src/org/opencv/samples/tutorial4/Sample4View.java index 3b1143373a..e4856f128f 100644 --- a/samples/android/tutorial-4-mixed/src/org/opencv/samples/tutorial4/Sample4View.java +++ b/samples/android/tutorial-4-mixed/src/org/opencv/samples/tutorial4/Sample4View.java @@ -104,11 +104,6 @@ class Sample4View extends SampleViewBase { public native void FindFeatures(long matAddrGr, long matAddrRgba); - static { - System.loadLibrary("opencv_java"); - System.loadLibrary("mixed_sample"); - } - public void setViewMode(int viewMode) { mViewMode = viewMode; }