15
15
*/
16
16
package com .androidmontreal .opencv ;
17
17
18
-
19
18
import android .app .Activity ;
20
19
import android .hardware .Camera ;
21
20
import android .hardware .Camera .PictureCallback ;
22
21
import android .util .Log ;
23
22
import android .view .View ;
24
23
import android .widget .Button ;
25
24
import android .widget .TextView ;
25
+ import android .os .AsyncTask ;
26
26
import android .os .Bundle ;
27
27
28
28
29
29
public class AndroidOpenCVforHackathonsActivity extends Activity implements PictureCallback
30
30
{
31
31
private static final String TAG = "OpenCVforHackathons" ;
32
32
33
+ // OpenCV results view
34
+ private TextView textView1 ;
35
+ private boolean stopUpdatingInBackground = false ;
36
+
33
37
34
38
/** Called when the activity is first created. */
35
39
@ Override
@@ -43,10 +47,41 @@ public void onCreate(Bundle savedInstanceState)
43
47
* function.
44
48
*/
45
49
setContentView (R .layout .main );
46
- TextView tv = (TextView ) findViewById (R .id .textview1 );
47
- tv .setText ( stringFromJNI () );
50
+ textView1 = (TextView ) findViewById (R .id .textview1 );
51
+ textView1 .setText ( stringFromJNI () );
48
52
49
53
}
54
+ public void getOpenCVResult (View view ){
55
+ UpdateFromOpenCVTask getOpenCVResults = new UpdateFromOpenCVTask ();
56
+ getOpenCVResults .execute (new String [] { "in execute" });
57
+
58
+ }
59
+ private class UpdateFromOpenCVTask extends AsyncTask <String , Void , String > {
60
+ @ Override
61
+ protected String doInBackground (String ... urls ) {
62
+ Log .d (TAG ,"Pausing 1 sec before calling again." );
63
+ try {
64
+ new Thread ().sleep (1000 );
65
+ } catch (InterruptedException e ) {
66
+ e .printStackTrace ();
67
+ }
68
+ if (!(stopUpdatingInBackground )){
69
+ // Loop every 1 sec
70
+ getOpenCVResult (textView1 );
71
+ }
72
+ String response = "Hi from the AsyncThread" ;
73
+ response = ((AndroidOpenCVforHackathonsApp ) getApplication ()).getLastMessage ();
74
+ return response ;
75
+ }
76
+ @ Override
77
+ protected void onPostExecute (String result ) {
78
+ /*
79
+ * The result comes from the above doInBackground method,
80
+ * but this runs on the UI thread and so we can set a field in the UI.
81
+ */
82
+ textView1 .setText (result );
83
+ }
84
+ }
50
85
51
86
@ Override
52
87
protected void onDestroy () {
@@ -63,13 +98,16 @@ public void onLowMemory() {
63
98
@ Override
64
99
protected void onPause () {
65
100
Log .d (TAG , "====onPause====" );
66
- super .onPause ();
101
+ stopUpdatingInBackground = true ;
102
+ super .onPause ();
67
103
}
68
104
69
105
@ Override
70
106
protected void onResume () {
71
107
Log .d (TAG , "==onResume==" );
72
108
super .onResume ();
109
+ stopUpdatingInBackground = false ;
110
+ getOpenCVResult (textView1 );
73
111
}
74
112
75
113
@ Override
0 commit comments