@@ -140,6 +140,9 @@ |
||
140 | 140 |
|
141 | 141 |
<service android:name=".service.OrderDealService"/> |
142 | 142 |
|
143 |
+ <service android:name=".dslr.CameraService" |
|
144 |
+ android:process=":camera"/> |
|
145 |
+ |
|
143 | 146 |
</application> |
144 | 147 |
|
145 | 148 |
</manifest> |
@@ -3,13 +3,177 @@ package ai.pai.lensman.dslr; |
||
3 | 3 |
|
4 | 4 |
import android.app.Service; |
5 | 5 |
import android.content.Intent; |
6 |
+import android.os.AsyncTask; |
|
7 |
+import android.os.Bundle; |
|
6 | 8 |
import android.os.IBinder; |
9 |
+import android.os.Process; |
|
10 |
+import android.text.TextUtils; |
|
11 |
+ |
|
12 |
+import com.android.common.utils.LogHelper; |
|
13 |
+ |
|
14 |
+import java.util.Timer; |
|
15 |
+import java.util.TimerTask; |
|
7 | 16 |
|
8 | 17 |
public class CameraService extends Service { |
9 | 18 |
|
19 |
+ private boolean isInitExecuted; |
|
20 |
+ private CameraInitTask cameraInitTask; |
|
21 |
+ |
|
22 |
+ private Timer photoCaptureTimer; |
|
23 |
+ private String sessionWorkingDirPath; |
|
24 |
+ private boolean isLastQueryReturned = true; |
|
25 |
+ |
|
26 |
+ public static final String ACTION_CAMERA_SERVICE_STATUS_CHANGE = "action.ai.pai.lensman.dslr.cameraservice"; |
|
27 |
+ public static final String EXTRA_STATUS_PART = "status"; |
|
28 |
+ public static final String EXTRA_DATA_PART = "data"; |
|
29 |
+ |
|
30 |
+ public static final int MSG_CAMERA_INIT_SUCCESS = 8000; |
|
31 |
+ public static final int MSG_CAMERA_CONN_ERROR = 8001; |
|
32 |
+ public static final int MSG_CAMERA_NEW_PHOTO_FOUND = 8002; |
|
33 |
+ |
|
34 |
+ public static final int CMD_INIT_CAMERA_CONNECTION = 9000; |
|
35 |
+ public static final int CMD_EXIT_CAMERA_CONNECTION = 9001; |
|
36 |
+ public static final int CMD_START_CAPTURE_PHOTO = 9002; |
|
37 |
+ |
|
38 |
+ private static final String MSG_TYPE_CAMERA_ERROR = "camero error";//相机错误,可能是线松动了。先exit,然后重新init |
|
39 |
+ private static final String MSG_TYPE_NOT_INIT = "not init"; //需要初始化 |
|
40 |
+ private static final String MSG_TYPE_TIME_OUT = "time out"; //继续调用waitforevent |
|
41 |
+ |
|
42 |
+ @Override |
|
43 |
+ public void onCreate() { |
|
44 |
+ super.onCreate(); |
|
45 |
+ } |
|
46 |
+ |
|
10 | 47 |
@Override |
11 | 48 |
public IBinder onBind(Intent intent) { |
12 | 49 |
return null; |
13 | 50 |
} |
14 | 51 |
|
52 |
+ @Override |
|
53 |
+ public int onStartCommand(Intent intent, int flags, int startId) { |
|
54 |
+ if(intent!=null&&intent.getIntExtra("cmd",0)>0){ |
|
55 |
+ int cmd = intent.getIntExtra("cmd",0); |
|
56 |
+ if(cmd == CMD_EXIT_CAMERA_CONNECTION){ |
|
57 |
+ stopCameraService(); |
|
58 |
+ return START_NOT_STICKY; |
|
59 |
+ }else if(cmd == CMD_INIT_CAMERA_CONNECTION){ |
|
60 |
+ if(!isInitExecuted){ |
|
61 |
+ cameraInitTask = new CameraInitTask(); |
|
62 |
+ cameraInitTask.execute(); |
|
63 |
+ } |
|
64 |
+ }else if(cmd == CMD_START_CAPTURE_PHOTO){ |
|
65 |
+ if(isInitExecuted){ |
|
66 |
+ sessionWorkingDirPath = intent.getStringExtra("session_dir"); |
|
67 |
+ if(!TextUtils.isEmpty(sessionWorkingDirPath)){ |
|
68 |
+ startCapture(); |
|
69 |
+ } |
|
70 |
+ } |
|
71 |
+ } |
|
72 |
+ } |
|
73 |
+ |
|
74 |
+ return super.onStartCommand(intent, flags, startId); |
|
75 |
+ } |
|
76 |
+ |
|
77 |
+ @Override |
|
78 |
+ public void onDestroy() { |
|
79 |
+ super.onDestroy(); |
|
80 |
+ } |
|
81 |
+ |
|
82 |
+ private void stopCameraService(){ |
|
83 |
+ if(cameraInitTask!=null){ |
|
84 |
+ cameraInitTask.cancel(); |
|
85 |
+ } |
|
86 |
+ if(photoCaptureTimer !=null){ |
|
87 |
+ photoCaptureTimer.cancel(); |
|
88 |
+ photoCaptureTimer = null; |
|
89 |
+ } |
|
90 |
+ stopSelf(); |
|
91 |
+ Process.killProcess(Process.myPid()); |
|
92 |
+ System.exit(0); |
|
93 |
+ } |
|
94 |
+ |
|
95 |
+ class CameraInitTask extends AsyncTask<Void,Integer,Integer>{ |
|
96 |
+ |
|
97 |
+ private boolean isCancelled = false; |
|
98 |
+ |
|
99 |
+ public void cancel(){ |
|
100 |
+ isCancelled = true; |
|
101 |
+ } |
|
102 |
+ |
|
103 |
+ @Override |
|
104 |
+ protected Integer doInBackground(Void... params) { |
|
105 |
+ isInitExecuted = true; |
|
106 |
+ return CameraJNIInterface.getInstance().java_mygpcamerainit(); |
|
107 |
+ } |
|
108 |
+ |
|
109 |
+ @Override |
|
110 |
+ protected void onPostExecute(Integer result) { |
|
111 |
+ |
|
112 |
+ if(isCancelled){ |
|
113 |
+ return; |
|
114 |
+ } |
|
115 |
+ if(result>=0){ |
|
116 |
+ Bundle bundle = new Bundle(); |
|
117 |
+ bundle.putInt(EXTRA_STATUS_PART,MSG_CAMERA_INIT_SUCCESS); |
|
118 |
+ sendCameraIntent(bundle); |
|
119 |
+ }else{ |
|
120 |
+ Bundle bundle = new Bundle(); |
|
121 |
+ bundle.putInt(EXTRA_STATUS_PART, MSG_CAMERA_CONN_ERROR); |
|
122 |
+ sendCameraIntent(bundle); |
|
123 |
+ stopCameraService(); |
|
124 |
+ } |
|
125 |
+ } |
|
126 |
+ } |
|
127 |
+ public void startCapture() { |
|
128 |
+ if(photoCaptureTimer !=null){ |
|
129 |
+ photoCaptureTimer.cancel(); |
|
130 |
+ photoCaptureTimer = null; |
|
131 |
+ } |
|
132 |
+ photoCaptureTimer = new Timer(); |
|
133 |
+ photoCaptureTimer.schedule(new TimerTask() { |
|
134 |
+ @Override |
|
135 |
+ public void run() { |
|
136 |
+ fetchPhotoTask(); |
|
137 |
+ } |
|
138 |
+ },1000,10); |
|
139 |
+ } |
|
140 |
+ |
|
141 |
+ private void fetchPhotoTask(){ |
|
142 |
+ if(!isLastQueryReturned){ |
|
143 |
+ LogHelper.d("czy","fetchPhotoTask last query not finished,return "); |
|
144 |
+ return; |
|
145 |
+ } |
|
146 |
+ |
|
147 |
+ isLastQueryReturned = false; |
|
148 |
+ String eventMsg = CameraJNIInterface.getInstance().java_mygpcamerawaitforevent(sessionWorkingDirPath); |
|
149 |
+ LogHelper.d("czy","mygpcamerawaitforevent return result = "+eventMsg); |
|
150 |
+ if(eventMsg!=null && eventMsg.length()>0){ |
|
151 |
+ if(MSG_TYPE_NOT_INIT.equalsIgnoreCase(eventMsg)||MSG_TYPE_CAMERA_ERROR.equalsIgnoreCase(eventMsg)){ |
|
152 |
+ Bundle bundle = new Bundle(); |
|
153 |
+ bundle.putInt(EXTRA_STATUS_PART, MSG_CAMERA_CONN_ERROR); |
|
154 |
+ sendCameraIntent(bundle); |
|
155 |
+ stopCameraService(); |
|
156 |
+ }else if(MSG_TYPE_TIME_OUT.equalsIgnoreCase(eventMsg)){ |
|
157 |
+ |
|
158 |
+ }else{ |
|
159 |
+ String sub = eventMsg.substring(0,1); |
|
160 |
+ if(TextUtils.isDigitsOnly(sub)){ |
|
161 |
+ LogHelper.d("czy","fetchPhotoTask new photo found"); |
|
162 |
+ Bundle bundle = new Bundle(); |
|
163 |
+ bundle.putInt(EXTRA_STATUS_PART, MSG_CAMERA_NEW_PHOTO_FOUND); |
|
164 |
+ bundle.putString(EXTRA_DATA_PART,eventMsg); |
|
165 |
+ sendCameraIntent(bundle); |
|
166 |
+ } |
|
167 |
+ } |
|
168 |
+ } |
|
169 |
+ |
|
170 |
+ isLastQueryReturned = true; |
|
171 |
+ } |
|
172 |
+ |
|
173 |
+ private void sendCameraIntent(Bundle bundle){ |
|
174 |
+ Intent intent = new Intent(ACTION_CAMERA_SERVICE_STATUS_CHANGE); |
|
175 |
+ intent.putExtras(bundle); |
|
176 |
+ sendBroadcast(intent); |
|
177 |
+ } |
|
178 |
+ |
|
15 | 179 |
} |