1
1
package org .opencv .samples .fd ;
2
2
3
+ import org .opencv .android .BaseLoaderCallback ;
4
+ import org .opencv .android .LoaderCallbackInterface ;
5
+ import org .opencv .android .OpenCVLoader ;
6
+
3
7
import android .app .Activity ;
4
8
import android .app .AlertDialog ;
5
9
import android .content .DialogInterface ;
@@ -18,8 +22,47 @@ public class FdActivity extends Activity {
18
22
private MenuItem mItemFace20 ;
19
23
private MenuItem mItemType ;
20
24
21
- private FdView mView ;
25
+ private FdView mView ;
22
26
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
+
23
66
private int mDetectorType = 0 ;
24
67
private String [] mDetectorName ;
25
68
@@ -30,18 +73,19 @@ public FdActivity() {
30
73
mDetectorName [FdView .NATIVE_DETECTOR ] = "Native (tracking)" ;
31
74
}
32
75
33
- @ Override
76
+ @ Override
34
77
protected void onPause () {
35
78
Log .i (TAG , "onPause" );
36
79
super .onPause ();
37
- mView .releaseCamera ();
80
+ if (mView != null )
81
+ mView .releaseCamera ();
38
82
}
39
83
40
84
@ Override
41
85
protected void onResume () {
42
86
Log .i (TAG , "onResume" );
43
87
super .onResume ();
44
- if ( !mView .openCamera () ) {
88
+ if ( mView != null && !mView .openCamera () ) {
45
89
AlertDialog ad = new AlertDialog .Builder (this ).create ();
46
90
ad .setCancelable (false ); // This blocks the 'BACK' button
47
91
ad .setMessage ("Fatal error: can't open camera!" );
@@ -61,10 +105,13 @@ public void onCreate(Bundle savedInstanceState) {
61
105
Log .i (TAG , "onCreate" );
62
106
super .onCreate (savedInstanceState );
63
107
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
+ }
68
115
}
69
116
70
117
@ Override
0 commit comments