Skip to content

Commit 0a58d8f

Browse files
author
Andrey Pavlenko
committed
moving to OpenCVLoader API
1 parent 5e0160d commit 0a58d8f

File tree

2 files changed

+56
-9
lines changed

2 files changed

+56
-9
lines changed

samples/android/face-detection/jni/Android.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ LOCAL_SRC_FILES := DetectionBasedTracker_jni.cpp
1616
LOCAL_C_INCLUDES += $(LOCAL_PATH)
1717
LOCAL_LDLIBS += -llog -ldl
1818

19-
LOCAL_MODULE := detection_based_tacker
19+
LOCAL_MODULE := detection_based_tracker
2020

2121
include $(BUILD_SHARED_LIBRARY)

samples/android/face-detection/src/org/opencv/samples/fd/FdActivity.java

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package org.opencv.samples.fd;
22

3+
import org.opencv.android.BaseLoaderCallback;
4+
import org.opencv.android.LoaderCallbackInterface;
5+
import org.opencv.android.OpenCVLoader;
6+
37
import android.app.Activity;
48
import android.app.AlertDialog;
59
import android.content.DialogInterface;
@@ -18,8 +22,47 @@ public class FdActivity extends Activity {
1822
private MenuItem mItemFace20;
1923
private MenuItem mItemType;
2024

21-
private FdView mView;
25+
private FdView mView;
2226

27+
private BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) {
28+
@Override
29+
public void onManagerConnected(int status) {
30+
switch (status) {
31+
case LoaderCallbackInterface.SUCCESS:
32+
{
33+
Log.i(TAG, "OpenCV loaded successfully");
34+
35+
// Load native libs after OpenCV initialization
36+
System.loadLibrary("detection_based_tracker");
37+
38+
// Create and set View
39+
mView = new FdView(mAppContext);
40+
mView.setDetectorType(mDetectorType);
41+
mView.setMinFaceSize(0.2f);
42+
setContentView(mView);
43+
// Check native OpenCV camera
44+
if( !mView.openCamera() ) {
45+
AlertDialog ad = new AlertDialog.Builder(mAppContext).create();
46+
ad.setCancelable(false); // This blocks the 'BACK' button
47+
ad.setMessage("Fatal error: can't open camera!");
48+
ad.setButton("OK", new DialogInterface.OnClickListener() {
49+
public void onClick(DialogInterface dialog, int which) {
50+
dialog.dismiss();
51+
finish();
52+
}
53+
});
54+
ad.show();
55+
}
56+
} break;
57+
default:
58+
{
59+
super.onManagerConnected(status);
60+
} break;
61+
}
62+
}
63+
};
64+
65+
2366
private int mDetectorType = 0;
2467
private String[] mDetectorName;
2568

@@ -30,18 +73,19 @@ public FdActivity() {
3073
mDetectorName[FdView.NATIVE_DETECTOR] = "Native (tracking)";
3174
}
3275

33-
@Override
76+
@Override
3477
protected void onPause() {
3578
Log.i(TAG, "onPause");
3679
super.onPause();
37-
mView.releaseCamera();
80+
if (mView != null)
81+
mView.releaseCamera();
3882
}
3983

4084
@Override
4185
protected void onResume() {
4286
Log.i(TAG, "onResume");
4387
super.onResume();
44-
if( !mView.openCamera() ) {
88+
if( mView != null && !mView.openCamera() ) {
4589
AlertDialog ad = new AlertDialog.Builder(this).create();
4690
ad.setCancelable(false); // This blocks the 'BACK' button
4791
ad.setMessage("Fatal error: can't open camera!");
@@ -61,10 +105,13 @@ public void onCreate(Bundle savedInstanceState) {
61105
Log.i(TAG, "onCreate");
62106
super.onCreate(savedInstanceState);
63107
requestWindowFeature(Window.FEATURE_NO_TITLE);
64-
mView = new FdView(this);
65-
mView.setDetectorType(mDetectorType);
66-
mView.setMinFaceSize(0.2f);
67-
setContentView(mView);
108+
109+
Log.i(TAG, "Trying to load OpenCV library");
110+
if (!OpenCVLoader.initAsync(OpenCVLoader.OPEN_CV_VERSION_2_4_0, this, mOpenCVCallBack))
111+
{
112+
Log.e(TAG, "Cannot connect to OpenCV Manager");
113+
finish();
114+
}
68115
}
69116

70117
@Override

0 commit comments

Comments
 (0)