| Author | SHA1 | Message | Date |
|---|---|---|---|
|
|
38a3454d01 | 6 years ago |
@@ -1,77 +0,0 @@ |
||
| 1 |
-package ai.pai.lensman.dslr; |
|
| 2 |
- |
|
| 3 |
-import com.android.common.utils.LogHelper; |
|
| 4 |
- |
|
| 5 |
-import java.util.Random; |
|
| 6 |
- |
|
| 7 |
-import ai.pai.lensman.BuildConfig; |
|
| 8 |
- |
|
| 9 |
-public class CameraJNIInterface {
|
|
| 10 |
- |
|
| 11 |
- |
|
| 12 |
- static {
|
|
| 13 |
- try {
|
|
| 14 |
- System.loadLibrary("hello_jni");
|
|
| 15 |
- }catch (Throwable e){
|
|
| 16 |
- LogHelper.d("czy","load library error-->"+e);
|
|
| 17 |
- e.printStackTrace(); |
|
| 18 |
- } |
|
| 19 |
- } |
|
| 20 |
- |
|
| 21 |
- private static CameraJNIInterface instance; |
|
| 22 |
- |
|
| 23 |
- private CameraJNIInterface(){
|
|
| 24 |
- |
|
| 25 |
- } |
|
| 26 |
- |
|
| 27 |
- public static synchronized CameraJNIInterface getInstance(){
|
|
| 28 |
- if(instance==null){
|
|
| 29 |
- instance = new CameraJNIInterface(); |
|
| 30 |
- } |
|
| 31 |
- return instance; |
|
| 32 |
- } |
|
| 33 |
- |
|
| 34 |
- public native int mygpcamerainit(); |
|
| 35 |
- public native int mygpcameraexit(); |
|
| 36 |
- public native String mygpcameragetsummary(); |
|
| 37 |
- public native String mygpcamerawaitforevent(String foldr_path); |
|
| 38 |
- |
|
| 39 |
- public int java_mygpcamerainit(){
|
|
| 40 |
- try {
|
|
| 41 |
- return mygpcamerainit(); |
|
| 42 |
- }catch (Throwable t){
|
|
| 43 |
- t.printStackTrace(); |
|
| 44 |
- } |
|
| 45 |
- if(BuildConfig.isTestMode){
|
|
| 46 |
- return 0; |
|
| 47 |
- }else{
|
|
| 48 |
- return -1; |
|
| 49 |
- } |
|
| 50 |
- } |
|
| 51 |
- public int java_mygpcameraexit(){
|
|
| 52 |
- try {
|
|
| 53 |
- return mygpcameraexit(); |
|
| 54 |
- }catch (Throwable t){
|
|
| 55 |
- t.printStackTrace(); |
|
| 56 |
- } |
|
| 57 |
- if(BuildConfig.isTestMode){
|
|
| 58 |
- return 0; |
|
| 59 |
- }else{
|
|
| 60 |
- return -1; |
|
| 61 |
- } |
|
| 62 |
- } |
|
| 63 |
- |
|
| 64 |
- public String java_mygpcamerawaitforevent(String folder_path){
|
|
| 65 |
- try{
|
|
| 66 |
- return mygpcamerawaitforevent(folder_path); |
|
| 67 |
- }catch (Throwable t){
|
|
| 68 |
- t.printStackTrace(); |
|
| 69 |
- } |
|
| 70 |
- if(BuildConfig.isTestMode){
|
|
| 71 |
- return new Random().nextInt(100)%2==0 ?"1.jpg":"2.jpg"; |
|
| 72 |
- }else{
|
|
| 73 |
- return "init error"; |
|
| 74 |
- } |
|
| 75 |
- } |
|
| 76 |
- |
|
| 77 |
-} |
@@ -4,11 +4,13 @@ package ai.pai.lensman.dslr; |
||
| 4 | 4 |
import android.app.Service; |
| 5 | 5 |
import android.content.Intent; |
| 6 | 6 |
import android.graphics.Bitmap; |
| 7 |
+import android.os.AsyncTask; |
|
| 7 | 8 |
import android.os.Bundle; |
| 8 | 9 |
import android.os.Handler; |
| 9 | 10 |
import android.os.IBinder; |
| 10 | 11 |
import android.os.Message; |
| 11 | 12 |
import android.os.Process; |
| 13 |
+import android.text.TextUtils; |
|
| 12 | 14 |
|
| 13 | 15 |
import com.android.common.utils.LogHelper; |
| 14 | 16 |
import com.remoteyourcam.usb.ptp.Camera; |
@@ -17,11 +19,13 @@ import com.remoteyourcam.usb.ptp.PtpService; |
||
| 17 | 19 |
import com.remoteyourcam.usb.ptp.model.LiveViewData; |
| 18 | 20 |
import com.remoteyourcam.usb.ptp.model.ObjectInfo; |
| 19 | 21 |
|
| 22 |
+import java.io.File; |
|
| 23 |
+import java.io.FileOutputStream; |
|
| 20 | 24 |
import java.util.ArrayList; |
| 21 | 25 |
|
| 22 | 26 |
import ai.pai.lensman.db.Preferences; |
| 23 | 27 |
|
| 24 |
-public class CameraService extends Service implements Handler.Callback, Camera.CameraListener, Camera.StorageInfoListener, Camera.RetrieveImageInfoListener{
|
|
| 28 |
+public class CameraService extends Service implements Handler.Callback, Camera.CameraListener, Camera.StorageInfoListener, Camera.RetrieveImageInfoListener {
|
|
| 25 | 29 |
|
| 26 | 30 |
private Handler mainHandler; |
| 27 | 31 |
private final static int MSG_CHECK_STORAGE = 100; |
@@ -46,6 +50,7 @@ public class CameraService extends Service implements Handler.Callback, Camera.C |
||
| 46 | 50 |
private PtpService ptp; |
| 47 | 51 |
private Camera camera; |
| 48 | 52 |
private int[] origin = new int[0]; |
| 53 |
+ private String sessionDir; |
|
| 49 | 54 |
|
| 50 | 55 |
@Override |
| 51 | 56 |
public void onCreate() {
|
@@ -61,27 +66,31 @@ public class CameraService extends Service implements Handler.Callback, Camera.C |
||
| 61 | 66 |
|
| 62 | 67 |
@Override |
| 63 | 68 |
public int onStartCommand(Intent intent, int flags, int startId) {
|
| 64 |
- LogHelper.d("czy","CameraService onStartCommand ");
|
|
| 65 |
- if(camera == null){
|
|
| 69 |
+ LogHelper.d("czy", "CameraService onStartCommand ");
|
|
| 70 |
+ if (camera == null) {
|
|
| 66 | 71 |
startCamera(intent); |
| 67 |
- }else if(intent!=null&&intent.getIntExtra(EXTRA_CMD,0)>0){
|
|
| 68 |
- int cmd = intent.getIntExtra(EXTRA_CMD,0); |
|
| 69 |
- if(cmd == CMD_EXIT_CAMERA_CONNECTION){
|
|
| 70 |
- LogHelper.d("czy","CameraService 收到停止进程任务");
|
|
| 72 |
+ } else if (intent != null && intent.getIntExtra(EXTRA_CMD, 0) > 0) {
|
|
| 73 |
+ int cmd = intent.getIntExtra(EXTRA_CMD, 0); |
|
| 74 |
+ if (cmd == CMD_EXIT_CAMERA_CONNECTION) {
|
|
| 75 |
+ LogHelper.d("czy", "CameraService 收到停止进程任务");
|
|
| 71 | 76 |
stopCamera(); |
| 72 |
- }else if(cmd == CMD_INIT_CAMERA_CONNECTION){
|
|
| 73 |
- if(camera != null){
|
|
| 77 |
+ } else if (cmd == CMD_INIT_CAMERA_CONNECTION) {
|
|
| 78 |
+ if (camera != null) {
|
|
| 74 | 79 |
Bundle bundle = new Bundle(); |
| 75 |
- bundle.putInt(EXTRA_STATUS_PART,MSG_CAMERA_INIT_SUCCESS); |
|
| 80 |
+ bundle.putInt(EXTRA_STATUS_PART, MSG_CAMERA_INIT_SUCCESS); |
|
| 76 | 81 |
sendCameraIntent(bundle); |
| 77 | 82 |
} |
| 78 |
- }else if(cmd == CMD_START_CAPTURE_PHOTO){
|
|
| 79 |
- if(camera !=null){
|
|
| 80 |
- LogHelper.d("czy","CameraService 收到开始拍摄任务");
|
|
| 81 |
- startCapture(); |
|
| 83 |
+ } else if (cmd == CMD_START_CAPTURE_PHOTO) {
|
|
| 84 |
+ if (camera != null) {
|
|
| 85 |
+ sessionDir = intent.getStringExtra(EXTRA_SESSION_DIR); |
|
| 86 |
+ if (!TextUtils.isEmpty(sessionDir)) {
|
|
| 87 |
+ startCapture(); |
|
| 88 |
+ LogHelper.d("czy", "CameraService 收到开始拍摄任务");
|
|
| 89 |
+ } |
|
| 90 |
+ |
|
| 82 | 91 |
} |
| 83 |
- }else if(cmd == CMD_STOP_CAPTURE_PHOTO){
|
|
| 84 |
- LogHelper.d("czy","CameraService 收到结束拍摄任务");
|
|
| 92 |
+ } else if (cmd == CMD_STOP_CAPTURE_PHOTO) {
|
|
| 93 |
+ LogHelper.d("czy", "CameraService 收到结束拍摄任务");
|
|
| 85 | 94 |
stopCapture(); |
| 86 | 95 |
} |
| 87 | 96 |
} |
@@ -94,13 +103,14 @@ public class CameraService extends Service implements Handler.Callback, Camera.C |
||
| 94 | 103 |
super.onDestroy(); |
| 95 | 104 |
} |
| 96 | 105 |
|
| 97 |
- private void startCamera(Intent intent){
|
|
| 98 |
- LogHelper.d("czy","CameraService 尝试与相机建立连接");
|
|
| 106 |
+ private void startCamera(Intent intent) {
|
|
| 107 |
+ LogHelper.d("czy", "CameraService 尝试与相机建立连接");
|
|
| 99 | 108 |
ptp.setCameraListener(this); |
| 100 | 109 |
ptp.initialize(this, intent); |
| 101 | 110 |
} |
| 102 |
- private void stopCamera(){
|
|
| 103 |
- LogHelper.d("czy","CameraService stopCameraService ");
|
|
| 111 |
+ |
|
| 112 |
+ private void stopCamera() {
|
|
| 113 |
+ LogHelper.d("czy", "CameraService stopCameraService ");
|
|
| 104 | 114 |
mainHandler.removeCallbacksAndMessages(null); |
| 105 | 115 |
ptp.setCameraListener(null); |
| 106 | 116 |
ptp.shutdown(); |
@@ -115,7 +125,7 @@ public class CameraService extends Service implements Handler.Callback, Camera.C |
||
| 115 | 125 |
mainHandler.sendEmptyMessage(MSG_CHECK_STORAGE); |
| 116 | 126 |
} |
| 117 | 127 |
|
| 118 |
- public void stopCapture(){
|
|
| 128 |
+ public void stopCapture() {
|
|
| 119 | 129 |
mainHandler.removeCallbacksAndMessages(null); |
| 120 | 130 |
origin = new int[0]; |
| 121 | 131 |
} |
@@ -126,13 +136,13 @@ public class CameraService extends Service implements Handler.Callback, Camera.C |
||
| 126 | 136 |
if (camera != null) {
|
| 127 | 137 |
mainHandler.removeMessages(MSG_CHECK_STORAGE); |
| 128 | 138 |
camera.retrieveImageHandles(CameraService.this, 0xFFFFFFFF, PtpConstants.ObjectFormat.EXIF_JPEG); |
| 129 |
- mainHandler.sendEmptyMessageDelayed(MSG_CHECK_STORAGE, Preferences.getInstance().getCameraQueryInterval()); |
|
| 139 |
+ mainHandler.sendEmptyMessageDelayed(MSG_CHECK_STORAGE, Preferences.getInstance().getCameraQueryInterval()); |
|
| 130 | 140 |
} |
| 131 | 141 |
} |
| 132 | 142 |
return false; |
| 133 | 143 |
} |
| 134 | 144 |
|
| 135 |
- private void sendCameraIntent(Bundle bundle){
|
|
| 145 |
+ private void sendCameraIntent(Bundle bundle) {
|
|
| 136 | 146 |
Intent intent = new Intent(ACTION_CAMERA_SERVICE_STATUS_CHANGE); |
| 137 | 147 |
intent.putExtras(bundle); |
| 138 | 148 |
sendBroadcast(intent); |
@@ -142,9 +152,9 @@ public class CameraService extends Service implements Handler.Callback, Camera.C |
||
| 142 | 152 |
@Override |
| 143 | 153 |
public void onCameraStarted(Camera camera) {
|
| 144 | 154 |
this.camera = camera; |
| 145 |
- LogHelper.d("czy","CameraService 相机初始化成功");
|
|
| 155 |
+ LogHelper.d("czy", "CameraService 相机初始化成功");
|
|
| 146 | 156 |
Bundle bundle = new Bundle(); |
| 147 |
- bundle.putInt(EXTRA_STATUS_PART,MSG_CAMERA_INIT_SUCCESS); |
|
| 157 |
+ bundle.putInt(EXTRA_STATUS_PART, MSG_CAMERA_INIT_SUCCESS); |
|
| 148 | 158 |
sendCameraIntent(bundle); |
| 149 | 159 |
} |
| 150 | 160 |
|
@@ -152,7 +162,7 @@ public class CameraService extends Service implements Handler.Callback, Camera.C |
||
| 152 | 162 |
public void onCameraStopped(Camera camera) {
|
| 153 | 163 |
this.camera = null; |
| 154 | 164 |
Bundle bundle = new Bundle(); |
| 155 |
- bundle.putInt(EXTRA_STATUS_PART,MSG_CAMERA_CONN_ERROR); |
|
| 165 |
+ bundle.putInt(EXTRA_STATUS_PART, MSG_CAMERA_CONN_ERROR); |
|
| 156 | 166 |
sendCameraIntent(bundle); |
| 157 | 167 |
} |
| 158 | 168 |
|
@@ -160,7 +170,7 @@ public class CameraService extends Service implements Handler.Callback, Camera.C |
||
| 160 | 170 |
public void onNoCameraFound() {
|
| 161 | 171 |
this.camera = null; |
| 162 | 172 |
Bundle bundle = new Bundle(); |
| 163 |
- bundle.putInt(EXTRA_STATUS_PART,MSG_CAMERA_CONN_ERROR); |
|
| 173 |
+ bundle.putInt(EXTRA_STATUS_PART, MSG_CAMERA_CONN_ERROR); |
|
| 164 | 174 |
sendCameraIntent(bundle); |
| 165 | 175 |
} |
| 166 | 176 |
|
@@ -168,7 +178,7 @@ public class CameraService extends Service implements Handler.Callback, Camera.C |
||
| 168 | 178 |
public void onError(String message) {
|
| 169 | 179 |
camera = null; |
| 170 | 180 |
Bundle bundle = new Bundle(); |
| 171 |
- bundle.putInt(EXTRA_STATUS_PART,MSG_CAMERA_CONN_ERROR); |
|
| 181 |
+ bundle.putInt(EXTRA_STATUS_PART, MSG_CAMERA_CONN_ERROR); |
|
| 172 | 182 |
sendCameraIntent(bundle); |
| 173 | 183 |
} |
| 174 | 184 |
|
@@ -205,8 +215,8 @@ public class CameraService extends Service implements Handler.Callback, Camera.C |
||
| 205 | 215 |
@Override |
| 206 | 216 |
public void onCapturedPictureReceived(int objectHandle, String filename, Bitmap thumbnail, Bitmap bitmap) {
|
| 207 | 217 |
|
| 208 |
- LogHelper.d("czy","CameraService fetchPhotoTask new photo found");
|
|
| 209 |
- saveBmpAndNotify(objectHandle,filename,bitmap); |
|
| 218 |
+ LogHelper.d("czy", "CameraService fetchPhotoTask new photo found");
|
|
| 219 |
+ saveBmpAndNotify(objectHandle, filename, bitmap); |
|
| 210 | 220 |
} |
| 211 | 221 |
|
| 212 | 222 |
@Override |
@@ -241,7 +251,7 @@ public class CameraService extends Service implements Handler.Callback, Camera.C |
||
| 241 | 251 |
|
| 242 | 252 |
@Override |
| 243 | 253 |
public void onObjectAdded(int handle) {
|
| 244 |
- LogHelper.d("czy","CameraService onObjectAdded: "+ handle);
|
|
| 254 |
+ LogHelper.d("czy", "CameraService onObjectAdded: " + handle);
|
|
| 245 | 255 |
} |
| 246 | 256 |
|
| 247 | 257 |
@Override |
@@ -261,12 +271,12 @@ public class CameraService extends Service implements Handler.Callback, Camera.C |
||
| 261 | 271 |
|
| 262 | 272 |
@Override |
| 263 | 273 |
public void onImageHandlesRetrieved(int[] handles) {
|
| 264 |
- LogHelper.d("czy","CameraService onImageHandlesRetrieved: "+ handles.length);
|
|
| 274 |
+ LogHelper.d("czy", "CameraService onImageHandlesRetrieved: " + handles.length);
|
|
| 265 | 275 |
|
| 266 |
- if(origin.length ==0){
|
|
| 276 |
+ if (origin.length == 0) {
|
|
| 267 | 277 |
origin = handles; |
| 268 |
- }else{
|
|
| 269 |
- if(origin.length == handles.length){
|
|
| 278 |
+ } else {
|
|
| 279 |
+ if (origin.length == handles.length) {
|
|
| 270 | 280 |
return; |
| 271 | 281 |
} |
| 272 | 282 |
ArrayList<Integer> diff = new ArrayList<>(); |
@@ -282,9 +292,9 @@ public class CameraService extends Service implements Handler.Callback, Camera.C |
||
| 282 | 292 |
diff.add(handles[i]); |
| 283 | 293 |
} |
| 284 | 294 |
} |
| 285 |
- if(camera != null && diff.size() >0){
|
|
| 286 |
- for(int k = 0; k< diff.size();k++){
|
|
| 287 |
- LogHelper.d("czy","CameraService fetchPhotoTask new photo found: "+ diff.get(k));
|
|
| 295 |
+ if (camera != null && diff.size() > 0) {
|
|
| 296 |
+ for (int k = 0; k < diff.size(); k++) {
|
|
| 297 |
+ LogHelper.d("czy", "CameraService fetchPhotoTask new photo found: " + diff.get(k));
|
|
| 288 | 298 |
// camera.retrievePicture(diff.get(k)); |
| 289 | 299 |
} |
| 290 | 300 |
} |
@@ -297,12 +307,59 @@ public class CameraService extends Service implements Handler.Callback, Camera.C |
||
| 297 | 307 |
|
| 298 | 308 |
} |
| 299 | 309 |
|
| 300 |
- private void saveBmpAndNotify(int objectHandle, String filename, Bitmap bmp){
|
|
| 310 |
+ private void saveBmpAndNotify(int objectHandle, String filename, Bitmap bmp) {
|
|
| 311 |
+ |
|
| 312 |
+ new SaveBitmapTask(objectHandle, filename, bmp).execute(); |
|
| 313 |
+ } |
|
| 314 |
+ |
|
| 315 |
+ class SaveBitmapTask extends AsyncTask<Void, Integer, Integer> {
|
|
| 316 |
+ private String filename; |
|
| 317 |
+ private Bitmap bitmap; |
|
| 318 |
+ private int handle; |
|
| 319 |
+ |
|
| 320 |
+ SaveBitmapTask(int objectHandle, String filename, Bitmap bmp) {
|
|
| 321 |
+ this.filename = filename; |
|
| 322 |
+ this.bitmap = bmp; |
|
| 323 |
+ this.handle = objectHandle; |
|
| 324 |
+ } |
|
| 301 | 325 |
|
| 302 |
-// Bundle bundle = new Bundle(); |
|
| 303 |
-// bundle.putInt(EXTRA_STATUS_PART, MSG_CAMERA_NEW_PHOTO_FOUND); |
|
| 304 |
-// bundle.putString(EXTRA_DATA_PART,filename); |
|
| 305 |
-// sendCameraIntent(bundle); |
|
| 326 |
+ private boolean isCancelled = false; |
|
| 327 |
+ |
|
| 328 |
+ public void cancel() {
|
|
| 329 |
+ isCancelled = true; |
|
| 330 |
+ } |
|
| 331 |
+ |
|
| 332 |
+ @Override |
|
| 333 |
+ protected Integer doInBackground(Void... params) {
|
|
| 334 |
+ try {
|
|
| 335 |
+ File dir = new File(sessionDir); |
|
| 336 |
+ if(!dir.exists()){
|
|
| 337 |
+ dir.mkdir(); |
|
| 338 |
+ } |
|
| 339 |
+ File file = new File(dir,handle +".jpg"); |
|
| 340 |
+ FileOutputStream fos = new FileOutputStream(file); |
|
| 341 |
+ bitmap.compress(Bitmap.CompressFormat.JPEG,100,fos); |
|
| 342 |
+ fos.flush(); |
|
| 343 |
+ fos.close(); |
|
| 344 |
+ return 0; |
|
| 345 |
+ } catch (Throwable t) {
|
|
| 346 |
+ t.printStackTrace(); |
|
| 347 |
+ return -1; |
|
| 348 |
+ } |
|
| 349 |
+ } |
|
| 350 |
+ |
|
| 351 |
+ @Override |
|
| 352 |
+ protected void onPostExecute(Integer result) {
|
|
| 353 |
+ if (isCancelled) {
|
|
| 354 |
+ return; |
|
| 355 |
+ } |
|
| 356 |
+ if (result >= 0) {
|
|
| 357 |
+ Bundle bundle = new Bundle(); |
|
| 358 |
+ bundle.putInt(EXTRA_STATUS_PART, MSG_CAMERA_NEW_PHOTO_FOUND); |
|
| 359 |
+ bundle.putString(EXTRA_DATA_PART, filename); |
|
| 360 |
+ sendCameraIntent(bundle); |
|
| 361 |
+ } |
|
| 362 |
+ } |
|
| 306 | 363 |
} |
| 307 | 364 |
|
| 308 | 365 |
} |
@@ -20,7 +20,6 @@ import ai.pai.lensman.dslr.CameraService; |
||
| 20 | 20 |
import ai.pai.lensman.service.Constants; |
| 21 | 21 |
|
| 22 | 22 |
import static ai.pai.lensman.dslr.CameraService.ACTION_CAMERA_SERVICE_STATUS_CHANGE; |
| 23 |
-import static ai.pai.lensman.dslr.CameraService.CMD_EXIT_CAMERA_CONNECTION; |
|
| 24 | 23 |
import static ai.pai.lensman.dslr.CameraService.CMD_INIT_CAMERA_CONNECTION; |
| 25 | 24 |
import static ai.pai.lensman.dslr.CameraService.CMD_START_CAPTURE_PHOTO; |
| 26 | 25 |
import static ai.pai.lensman.dslr.CameraService.CMD_STOP_CAPTURE_PHOTO; |