diff --git a/plugin.xml b/plugin.xml index 66adcbc..dccb0a6 100644 --- a/plugin.xml +++ b/plugin.xml @@ -27,6 +27,7 @@ + diff --git a/src/android/CameraLauncher.java b/src/android/CameraLauncher.java index 7d6d34f..791948b 100644 --- a/src/android/CameraLauncher.java +++ b/src/android/CameraLauncher.java @@ -16,7 +16,6 @@ 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 { @@ -35,9 +34,20 @@ public class CameraLauncher extends CordovaPlugin { Intent intent = new Intent(this.cordova.getActivity(), CameraActivity.class); - Bundle imgBackgroundBase64 = new Bundle(); - imgBackgroundBase64.putString("imgBackgroundBase64", args.getString(0)); - intent.putExtras(imgBackgroundBase64); + byte[] imgBackgroundBase64; + try { + imgBackgroundBase64 = Base64.decode(args.getString(0), Base64.NO_WRAP); + } catch (IllegalArgumentException e) { + this.callbackContext.error( + generateError( + CameraLauncher.RESULT_ERROR, + "Error decode base64 picture." + ) + ); + + return false; + } + TransferBigData.setImgBackgroundBase64(imgBackgroundBase64); cordova.startActivityForResult((CordovaPlugin) this, intent, CameraLauncher.REQUEST_CODE); diff --git a/src/android/customCamera/res/layout/activity_camera_view.xml b/src/android/customCamera/res/layout/activity_camera_view.xml index b60c4ab..542df23 100644 --- a/src/android/customCamera/res/layout/activity_camera_view.xml +++ b/src/android/customCamera/res/layout/activity_camera_view.xml @@ -11,20 +11,14 @@ android:layout_alignParentTop="true" > - - - - + android:layout_alignParentTop="true" + android:alpha="0.2" + android:scaleType="fitXY" /> 1) { - // If we touch with more than one finger - if (action == MotionEvent.ACTION_POINTER_2_DOWN) { - distanceBetweenFingers = getFingerSpacing(event); - } else if (action == MotionEvent.ACTION_MOVE && params.isZoomSupported()) { - mCamera.cancelAutoFocus(); - handleZoom(event, params, distanceBetweenFingers); - } - } else { - // If we touch with one finger -> auto-focus - if (action == MotionEvent.ACTION_UP) { - handleFocus(event, params); - } - } + Camera.Parameters params = mCamera.getParameters(); + int action = event.getAction(); + + if (event.getPointerCount() > 1) { + // If we touch with more than one finger + if (action == MotionEvent.ACTION_POINTER_2_DOWN) { + distanceBetweenFingers = getFingerSpacing(event); + } else if (action == MotionEvent.ACTION_MOVE && params.isZoomSupported()) { + mCamera.cancelAutoFocus(); + handleZoom(event, params, distanceBetweenFingers); + } + } else { + // If we touch with one finger -> auto-focus + if (action == MotionEvent.ACTION_UP) { + handleFocus(event, params); + } + } } return true; } @@ -252,7 +252,7 @@ public class CameraActivity extends Activity { * @param maxZoom int the max zoom of the device * @param zoom int the current zoom */ - private void setZoomProgress(int maxZoom, int zoom){ + private void setZoomProgress(int maxZoom, int zoom) { SeekBar niveauZoom = (SeekBar) findViewById(R.id.niveauZoom); niveauZoom.setMax(maxZoom); niveauZoom.setProgress(zoom*2); @@ -267,13 +267,13 @@ public class CameraActivity extends Activity { */ public void handleFocus(MotionEvent event, Camera.Parameters params) { if (photoTaken == false) { - List supportedFocusModes = params.getSupportedFocusModes(); - if (supportedFocusModes != null && supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) { - mCamera.autoFocus(new Camera.AutoFocusCallback() { - @Override - public void onAutoFocus(boolean b, Camera camera) {} - }); - } + List supportedFocusModes = params.getSupportedFocusModes(); + if (supportedFocusModes != null && supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) { + mCamera.autoFocus(new Camera.AutoFocusCallback() { + @Override + public void onAutoFocus(boolean b, Camera camera) {} + }); + } } } @@ -283,13 +283,7 @@ public class CameraActivity extends Activity { */ public void showMiniature(View view) { // Picture for the background. - ImageView imageView = (ImageView) findViewById(R.id.normal); - if (!photoTaken) { - imageView.setScaleType(ImageView.ScaleType.FIT_END); - } - else { - imageView.setScaleType(ImageView.ScaleType.FIT_START); - } + ImageView imageView = (ImageView) findViewById(R.id.background); // Button for show miniature picture. final Button miniature = (Button) view; @@ -305,14 +299,11 @@ public class CameraActivity extends Activity { imageView.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { modeMiniature = false; - ImageView imageView = (ImageView) findViewById(R.id.normal); - imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE); + ImageView imageView = (ImageView) findViewById(R.id.background); // resize miniature. - LayoutParams paramsReagrandissement = (LayoutParams) imageView.getLayoutParams(); imageView.setClickable(false); - paramsReagrandissement.width = -1; - paramsReagrandissement.height = -1; - imageView.setLayoutParams(paramsReagrandissement); + setBackground(); + miniature.setVisibility(View.VISIBLE); } }); @@ -325,19 +316,18 @@ public class CameraActivity extends Activity { * @param ImageView imageView Reference to the background image. * @param Boolean Resize Should we resize or not ? Only when click on "miniature" */ - public void setParamsMiniature(ImageView imageView, boolean resize){ - FrameLayout.LayoutParams paramsMiniature = new FrameLayout.LayoutParams(imageView.getWidth(), imageView.getHeight()); - if (resize == true){ + public void setParamsMiniature(ImageView imageView, boolean resize) { + RelativeLayout.LayoutParams paramsMiniature = new RelativeLayout.LayoutParams(imageView.getWidth(), imageView.getHeight()); + if (resize == true) { paramsMiniature.width = imageView.getWidth()/4; paramsMiniature.height = imageView.getHeight()/4; } - if (!photoTaken){ - paramsMiniature.gravity = Gravity.BOTTOM; + if (!photoTaken) { + paramsMiniature.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE); + }else { + paramsMiniature.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE); } - else { - paramsMiniature.gravity = Gravity.TOP; - } - imageView.setLayoutParams(paramsMiniature); + imageView.setLayoutParams(paramsMiniature); } /** @@ -419,9 +409,8 @@ public class CameraActivity extends Activity { photoTaken = true; // If miniature mode when photo is taken, the miniature goes to the top - if(modeMiniature){ - ImageView imageView = (ImageView) findViewById(R.id.normal); - imageView.setScaleType(ImageView.ScaleType.FIT_START); + if (modeMiniature) { + ImageView imageView = (ImageView) findViewById(R.id.background); setParamsMiniature(imageView, false); } @@ -433,7 +422,7 @@ public class CameraActivity extends Activity { @Override public void onClick(View v) { try { - photoTaken = false; + photoTaken = false; // Get path picture to storage. String pathPicture = Environment.getExternalStorageDirectory().getPath()+"/"+Environment.DIRECTORY_DCIM+"/Camera/"; pathPicture = pathPicture+String.format("%d.jpeg", System.currentTimeMillis()); @@ -456,21 +445,20 @@ public class CameraActivity extends Activity { decline.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - photoTaken = false; + photoTaken = false; ((LinearLayout.LayoutParams) params).gravity = Gravity.BOTTOM; - miniature.setLayoutParams(params); - - // If mode miniature and photo is declined, the miniature goes back to the bottom - if(modeMiniature) { - ImageView imageView = (ImageView) findViewById(R.id.normal); - imageView.setScaleType(ImageView.ScaleType.FIT_END); + miniature.setLayoutParams(params); + + // If mode miniature and photo is declined, the miniature goes back to the bottom + if (modeMiniature) { + ImageView imageView = (ImageView) findViewById(R.id.background); setParamsMiniature(imageView, false); - } - - keepPhoto.setVisibility(View.INVISIBLE); - photo.setVisibility(View.VISIBLE); + } + + keepPhoto.setVisibility(View.INVISIBLE); + photo.setVisibility(View.VISIBLE); niveauZoom.setVisibility(View.VISIBLE); - mCamera.startPreview(); + mCamera.startPreview(); } }); }; @@ -506,4 +494,46 @@ public class CameraActivity extends Activity { this.setResult(3); this.finish(); } + + /** + * To set background in the view. + */ + protected void setBackground() { + // Get the base64 picture for the background only if it's exist. + byte[] imgBackgroundBase64 = TransferBigData.getImgBackgroundBase64(); + if (imgBackgroundBase64 != null) { + // Get picture. + Bitmap imgBackgroundBitmap = BitmapFactory.decodeByteArray(imgBackgroundBase64, 0, imgBackgroundBase64.length); + + // Get sizes screen. + Display defaultDisplay = getWindowManager().getDefaultDisplay(); + DisplayMetrics displayMetrics = new DisplayMetrics(); + defaultDisplay.getMetrics(displayMetrics); + int displayWidthPx = (int) displayMetrics.widthPixels; + int displayHeightPx = (int) displayMetrics.heightPixels; + + // Get sizes picture. + int widthBackground = (int) (imgBackgroundBitmap.getWidth() * displayMetrics.density); + int heightBackground = (int) (imgBackgroundBitmap.getHeight() * displayMetrics.density); + + // Change size ImageView. + RelativeLayout.LayoutParams paramsMiniature = new RelativeLayout.LayoutParams(widthBackground, heightBackground); + float ratioX = (float) displayWidthPx / (float) widthBackground; + float ratioY = (float) displayHeightPx / (float) heightBackground; + if (ratioX < ratioY && ratioX < 1) { + paramsMiniature.width = (int) displayWidthPx; + paramsMiniature.height = (int) (ratioX * heightBackground); + } else if (ratioX >= ratioY && ratioY < 1) { + paramsMiniature.width = (int) (ratioY * widthBackground); + paramsMiniature.height = (int) displayHeightPx; + } + + // set image at the view. + ImageView imageView = (ImageView) findViewById(R.id.background); + imageView.setImageBitmap(imgBackgroundBitmap); + + paramsMiniature.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE); + imageView.setLayoutParams(paramsMiniature); + } + } } diff --git a/src/android/customCamera/src/org/geneanet/customcamera/TransferBigData.java b/src/android/customCamera/src/org/geneanet/customcamera/TransferBigData.java new file mode 100644 index 0000000..d742960 --- /dev/null +++ b/src/android/customCamera/src/org/geneanet/customcamera/TransferBigData.java @@ -0,0 +1,24 @@ +package org.geneanet.customcamera; + +/** + * Use to transfer big data between activities. + */ +public class TransferBigData { + protected static byte[] imgBackgroundBase64 = null; + + /** + * Get bytes to represent background picture. + * @return byte[] + */ + public static byte[] getImgBackgroundBase64() { + return TransferBigData.imgBackgroundBase64; + } + + /** + * Set bytes to represent background picture. + * @param byte[] imgBackgroundBase64 + */ + public static void setImgBackgroundBase64(byte[] imgBackgroundBase64) { + TransferBigData.imgBackgroundBase64 = imgBackgroundBase64; + } +} diff --git a/www/customCamera.js b/www/customCamera.js index 830df8e..a916fef 100644 --- a/www/customCamera.js +++ b/www/customCamera.js @@ -19,8 +19,8 @@ customCameraExport.prototype.startCamera = function(imgBackgroundBase64, success successFct(data); }; - var failFctCallback = function(data) { - failFct(data.code, data.message); + var failFctCallback = function(message) { + failFct(message); }; exec(