amera != null) {
+ sessionDir = intent.getStringExtra(EXTRA_SESSION_DIR);
+ if (!TextUtils.isEmpty(sessionDir)) {
+ startCapture();
+ LogHelper.d("czy", "CameraService 收到开始拍摄任务");
+ }
+
}
- }else if(cmd == CMD_STOP_CAPTURE_PHOTO){
- LogHelper.d("czy","CameraService 收到结束拍摄任务");
+ } else if (cmd == CMD_STOP_CAPTURE_PHOTO) {
+ LogHelper.d("czy", "CameraService 收到结束拍摄任务");
stopCapture();
}
}
@@ -94,13 +103,14 @@ public class CameraService extends Service implements Handler.Callback, Camera.C
super.onDestroy();
}
- private void startCamera(Intent intent){
- LogHelper.d("czy","CameraService 尝试与相机建立连接");
+ private void startCamera(Intent intent) {
+ LogHelper.d("czy", "CameraService 尝试与相机建立连接");
ptp.setCameraListener(this);
ptp.initialize(this, intent);
}
- private void stopCamera(){
- LogHelper.d("czy","CameraService stopCameraService ");
+
+ private void stopCamera() {
+ LogHelper.d("czy", "CameraService stopCameraService ");
mainHandler.removeCallbacksAndMessages(null);
ptp.setCameraListener(null);
ptp.shutdown();
@@ -115,7 +125,7 @@ public class CameraService extends Service implements Handler.Callback, Camera.C
mainHandler.sendEmptyMessage(MSG_CHECK_STORAGE);
}
- public void stopCapture(){
+ public void stopCapture() {
mainHandler.removeCallbacksAndMessages(null);
origin = new int[0];
}
@@ -126,13 +136,13 @@ public class CameraService extends Service implements Handler.Callback, Camera.C
if (camera != null) {
mainHandler.removeMessages(MSG_CHECK_STORAGE);
camera.retrieveImageHandles(CameraService.this, 0xFFFFFFFF, PtpConstants.ObjectFormat.EXIF_JPEG);
- mainHandler.sendEmptyMessageDelayed(MSG_CHECK_STORAGE, Preferences.getInstance().getCameraQueryInterval());
+ mainHandler.sendEmptyMessageDelayed(MSG_CHECK_STORAGE, Preferences.getInstance().getCameraQueryInterval());
}
}
return false;
}
- private void sendCameraIntent(Bundle bundle){
+ private void sendCameraIntent(Bundle bundle) {
Intent intent = new Intent(ACTION_CAMERA_SERVICE_STATUS_CHANGE);
intent.putExtras(bundle);
sendBroadcast(intent);
@@ -142,9 +152,9 @@ public class CameraService extends Service implements Handler.Callback, Camera.C
@Override
public void onCameraStarted(Camera camera) {
this.camera = camera;
- LogHelper.d("czy","CameraService 相机初始化成功");
+ LogHelper.d("czy", "CameraService 相机初始化成功");
Bundle bundle = new Bundle();
- bundle.putInt(EXTRA_STATUS_PART,MSG_CAMERA_INIT_SUCCESS);
+ bundle.putInt(EXTRA_STATUS_PART, MSG_CAMERA_INIT_SUCCESS);
sendCameraIntent(bundle);
}
@@ -152,7 +162,7 @@ public class CameraService extends Service implements Handler.Callback, Camera.C
public void onCameraStopped(Camera camera) {
this.camera = null;
Bundle bundle = new Bundle();
- bundle.putInt(EXTRA_STATUS_PART,MSG_CAMERA_CONN_ERROR);
+ bundle.putInt(EXTRA_STATUS_PART, MSG_CAMERA_CONN_ERROR);
sendCameraIntent(bundle);
}
@@ -160,7 +170,7 @@ public class CameraService extends Service implements Handler.Callback, Camera.C
public void onNoCameraFound() {
this.camera = null;
Bundle bundle = new Bundle();
- bundle.putInt(EXTRA_STATUS_PART,MSG_CAMERA_CONN_ERROR);
+ bundle.putInt(EXTRA_STATUS_PART, MSG_CAMERA_CONN_ERROR);
sendCameraIntent(bundle);
}
@@ -168,7 +178,7 @@ public class CameraService extends Service implements Handler.Callback, Camera.C
public void onError(String message) {
camera = null;
Bundle bundle = new Bundle();
- bundle.putInt(EXTRA_STATUS_PART,MSG_CAMERA_CONN_ERROR);
+ bundle.putInt(EXTRA_STATUS_PART, MSG_CAMERA_CONN_ERROR);
sendCameraIntent(bundle);
}
@@ -205,8 +215,8 @@ public class CameraService extends Service implements Handler.Callback, Camera.C
@Override
public void onCapturedPictureReceived(int objectHandle, String filename, Bitmap thumbnail, Bitmap bitmap) {
- LogHelper.d("czy","CameraService fetchPhotoTask new photo found");
- saveBmpAndNotify(objectHandle,filename,bitmap);
+ LogHelper.d("czy", "CameraService fetchPhotoTask new photo found");
+ saveBmpAndNotify(objectHandle, filename, bitmap);
}
@Override
@@ -241,7 +251,7 @@ public class CameraService extends Service implements Handler.Callback, Camera.C
@Override
public void onObjectAdded(int handle) {
- LogHelper.d("czy","CameraService onObjectAdded: "+ handle);
+ LogHelper.d("czy", "CameraService onObjectAdded: " + handle);
}
@Override
@@ -261,12 +271,12 @@ public class CameraService extends Service implements Handler.Callback, Camera.C
@Override
public void onImageHandlesRetrieved(int[] handles) {
- LogHelper.d("czy","CameraService onImageHandlesRetrieved: "+ handles.length);
+ LogHelper.d("czy", "CameraService onImageHandlesRetrieved: " + handles.length);
- if(origin.length ==0){
+ if (origin.length == 0) {
origin = handles;
- }else{
- if(origin.length == handles.length){
+ } else {
+ if (origin.length == handles.length) {
return;
}
ArrayList<Integer> diff = new ArrayList<>();
@@ -282,9 +292,9 @@ public class CameraService extends Service implements Handler.Callback, Camera.C
diff.add(handles[i]);
}
}
- if(camera != null && diff.size() >0){
- for(int k = 0; k< diff.size();k++){
- LogHelper.d("czy","CameraService fetchPhotoTask new photo found: "+ diff.get(k));
+ if (camera != null && diff.size() > 0) {
+ for (int k = 0; k < diff.size(); k++) {
+ LogHelper.d("czy", "CameraService fetchPhotoTask new photo found: " + diff.get(k));
// camera.retrievePicture(diff.get(k));
}
}
@@ -297,12 +307,59 @@ public class CameraService extends Service implements Handler.Callback, Camera.C
}
- private void saveBmpAndNotify(int objectHandle, String filename, Bitmap bmp){
+ private void saveBmpAndNotify(int objectHandle, String filename, Bitmap bmp) {
+
+ new SaveBitmapTask(objectHandle, filename, bmp).execute();
+ }
+
+ class SaveBitmapTask extends AsyncTask<Void, Integer, Integer> {
+ private String filename;
+ private Bitmap bitmap;
+ private int handle;
+
+ SaveBitmapTask(int objectHandle, String filename, Bitmap bmp) {
+ this.filename = filename;
+ this.bitmap = bmp;
+ this.handle = objectHandle;
+ }
-// Bundle bundle = new Bundle();
-// bundle.putInt(EXTRA_STATUS_PART, MSG_CAMERA_NEW_PHOTO_FOUND);
-// bundle.putString(EXTRA_DATA_PART,filename);
-// sendCameraIntent(bundle);
+ private boolean isCancelled = false;
+
+ public void cancel() {
+ isCancelled = true;
+ }
+
+ @Override
+ protected Integer doInBackground(Void... params) {
+ try {
+ File dir = new File(sessionDir);
+ if(!dir.exists()){
+ dir.mkdir();
+ }
+ File file = new File(dir,handle +".jpg");
+ FileOutputStream fos = new FileOutputStream(file);
+ bitmap.compress(Bitmap.CompressFormat.JPEG,100,fos);
+ fos.flush();
+ fos.close();
+ return 0;
+ } catch (Throwable t) {
+ t.printStackTrace();
+ return -1;
+ }
+ }
+
+ @Override
+ protected void onPostExecute(Integer result) {
+ if (isCancelled) {
+ return;
+ }
+ if (result >= 0) {
+ Bundle bundle = new Bundle();
+ bundle.putInt(EXTRA_STATUS_PART, MSG_CAMERA_NEW_PHOTO_FOUND);
+ bundle.putString(EXTRA_DATA_PART, filename);
+ sendCameraIntent(bundle);
+ }
+ }
}
}
@@ -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; |