diff --git a/src/android/CameraLauncher.java b/src/android/CameraLauncher.java index 6bc6450..cbaff6f 100644 --- a/src/android/CameraLauncher.java +++ b/src/android/CameraLauncher.java @@ -7,26 +7,75 @@ import org.apache.cordova.PluginResult; import org.json.JSONArray; import org.json.JSONException; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.IOException; + import android.content.Intent; import android.os.Bundle; +import android.util.Base64; public class CameraLauncher extends CordovaPlugin { + + protected CallbackContext callbackContext; + public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { if (action.equals("startCamera")) { + this.callbackContext = callbackContext; + Intent intent = new Intent(this.cordova.getActivity(), CameraView.class); Bundle imgBackgroundBase64 = new Bundle(); imgBackgroundBase64.putString("imgBackgroundBase64", args.getString(0)); intent.putExtras(imgBackgroundBase64); - cordova.getActivity().startActivity(intent); - - PluginResult r = new PluginResult(PluginResult.Status.OK, "base64retour"); - callbackContext.sendPluginResult(r); + cordova.startActivityForResult((CordovaPlugin) this, intent, 123456789); return true; } return false; } + + public void onActivityResult(int requestCode, int resultCode, Intent intent) { + if (requestCode == 123456789 && resultCode == 1) { + String pathPicture = intent.getStringExtra("pathPicture"); + // Log.d("customCamera", pathPicture); + try { + File fl = new File(pathPicture); + byte[] ret = loadFile(fl); + + byte[] output = Base64.encode(ret, Base64.NO_WRAP); + String js_out = new String(output); + + this.callbackContext.success(js_out); + } catch (Exception e) { + this.callbackContext.error("Error to get content file."); + } + } + } + + private static byte[] loadFile(File file) throws IOException { + InputStream is = new FileInputStream(file); + + long length = file.length(); + byte[] bytes = new byte[(int)length]; + + int offset = 0; + int numRead = 0; + while (offset < bytes.length + && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) { + offset += numRead; + } + + if (offset < bytes.length) { + throw new IOException("Could not completely read file "+file.getName()); + } + + is.close(); + return bytes; + } } diff --git a/src/android/customCamera/src/org/geneanet/customcamera/CameraView.java b/src/android/customCamera/src/org/geneanet/customcamera/CameraView.java index e033f6e..2fb4216 100644 --- a/src/android/customCamera/src/org/geneanet/customcamera/CameraView.java +++ b/src/android/customCamera/src/org/geneanet/customcamera/CameraView.java @@ -15,6 +15,7 @@ import android.content.res.Configuration; import android.hardware.Camera; import android.hardware.Camera.PictureCallback; import android.hardware.Camera.ShutterCallback; +import android.content.Intent; //import android.media.AudioManager; //import android.media.MediaPlayer; //import android.media.SoundPool; @@ -268,12 +269,9 @@ public class CameraView extends Activity { /*****************************************************/ /** METHOD TO DESTROY THE VIEW (HERE, THE ACTIVITY) **/ /*****************************************************/ - protected void onDestroy(){ + protected void onDestroy() { super.onDestroy(); - if(mCamera!=null){ - mCamera.stopPreview(); - mCamera = null; - } + CustomCamera.clearCameraAccess(); } /*************************************************/ @@ -306,6 +304,7 @@ public class CameraView extends Activity { /** METHOD TO TAKE PICTURE **/ /****************************/ public void takePhoto(View view){ + final CameraView cameraViewCurrent = this; /** To custom sound when you shot with the camera - optionnal**/ // sounds = new SoundPool(10, AudioManager.STREAM_MUSIC,0); // sSound = sounds.load(this.getApplicationContext(), R.raw.r2d2, 1); @@ -333,20 +332,19 @@ public class CameraView extends Activity { final Button photo = (Button)findViewById(R.id.capture); // Button miniature = (Button)findViewById(R.id.miniature); photo.setVisibility(View.INVISIBLE); - mCamera.stopPreview(); + mCamera.stopPreview(); accepter.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { - String pathImg = Environment.getExternalStorageDirectory().getPath()+"/"+Environment.DIRECTORY_DCIM+"/Camera/"; - pathImg = pathImg+String.format("%d.jpeg", System.currentTimeMillis()); - outStream = new FileOutputStream(pathImg); + String pathPicture = Environment.getExternalStorageDirectory().getPath()+"/"+Environment.DIRECTORY_DCIM+"/Camera/"; + pathPicture = pathPicture+String.format("%d.jpeg", System.currentTimeMillis()); + outStream = new FileOutputStream(pathPicture); outStream.write(data); - outStream.close(); - keepPhoto.setVisibility(View.INVISIBLE); - photo.setVisibility(View.VISIBLE); - mCamera.startPreview(); + outStream.close(); + cameraViewCurrent.setResult(1, new Intent().putExtra("pathPicture", pathPicture)); + cameraViewCurrent.finish(); } catch (IOException e) { e.printStackTrace(); } diff --git a/src/android/customCamera/src/org/geneanet/customcamera/CustomCamera.java b/src/android/customCamera/src/org/geneanet/customcamera/CustomCamera.java index fc661f9..b5a851d 100644 --- a/src/android/customCamera/src/org/geneanet/customcamera/CustomCamera.java +++ b/src/android/customCamera/src/org/geneanet/customcamera/CustomCamera.java @@ -33,4 +33,12 @@ public class CustomCamera { return c; // returns null if camera is unavailable } + + public static void clearCameraAccess() { + if (CustomCamera.mCamera != null) { + CustomCamera.mCamera.stopPreview(); + // CustomCamera.mCamera.release(); + CustomCamera.mCamera = null; + } + } }