From d698df4c3fe3ecabb79278c71c96b17e3908c830 Mon Sep 17 00:00:00 2001 From: Christophe BOUCAUT Date: Fri, 28 Nov 2014 17:12:17 +0100 Subject: [PATCH 1/3] =?UTF-8?q?Mise=20en=20place=20d'un=20background=20dyn?= =?UTF-8?q?amique=20pass=C3=A9=20depuis=20le=20JS.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/android/CameraLauncher.java | 21 +++++- .../res/layout/activity_camera_view.xml | 5 +- .../geneanet/customcamera/CameraActivity.java | 70 ++++++++++++++++--- www/customCamera.js | 4 +- 4 files changed, 81 insertions(+), 19 deletions(-) diff --git a/src/android/CameraLauncher.java b/src/android/CameraLauncher.java index 7d6d34f..1e3cbb5 100644 --- a/src/android/CameraLauncher.java +++ b/src/android/CameraLauncher.java @@ -18,6 +18,7 @@ import java.io.IOException; import android.content.Intent; import android.os.Bundle; import android.util.Base64; +import android.util.Log; public class CameraLauncher extends CordovaPlugin { @@ -35,9 +36,23 @@ 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; + } + + Bundle imgBackground = new Bundle(); + imgBackground.putByteArray("imgBackgroundBase64", imgBackgroundBase64); + intent.putExtras(imgBackground); 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 4241124..9e9d11d 100644 --- a/src/android/customCamera/res/layout/activity_camera_view.xml +++ b/src/android/customCamera/res/layout/activity_camera_view.xml @@ -17,12 +17,11 @@ android:layout_height="fill_parent" > + android:scaleType="fitXY" /> diff --git a/src/android/customCamera/src/org/geneanet/customcamera/CameraActivity.java b/src/android/customCamera/src/org/geneanet/customcamera/CameraActivity.java index 7d47843..67eac15 100644 --- a/src/android/customCamera/src/org/geneanet/customcamera/CameraActivity.java +++ b/src/android/customCamera/src/org/geneanet/customcamera/CameraActivity.java @@ -12,11 +12,14 @@ import android.util.Log; import android.app.Activity; import android.content.Intent; import android.content.res.Configuration; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; import android.hardware.Camera; import android.hardware.Camera.PictureCallback; import android.hardware.Camera.ShutterCallback; import android.os.Bundle; import android.os.Environment; +import android.util.DisplayMetrics; import android.view.Display; import android.view.Gravity; import android.view.MotionEvent; @@ -57,11 +60,7 @@ public class CameraActivity extends Activity { setContentView(R.layout.activity_camera_view); - // Get the base64 picture for the background only if it's exist. - Bundle currentBundle = this.getIntent().getExtras(); - if (currentBundle != null) { - String imgBackgroundBase64 = currentBundle.getString("imgBackgroundBase64"); - } + setBackground(); // The opacity bar SeekBar switchOpacity = (SeekBar) findViewById(R.id.switchOpacity); @@ -73,7 +72,7 @@ public class CameraActivity extends Activity { @Override public void onProgressChanged(SeekBar seekBar, int progresValue, boolean fromUser) { progress = progresValue; - ImageView imageView = (ImageView) findViewById(R.id.normal); + ImageView imageView = (ImageView) findViewById(R.id.background); float newOpacity = (float) (0.2+progress*0.1); imageView.setAlpha(newOpacity); } @@ -227,7 +226,7 @@ public class CameraActivity extends Activity { */ public void showMiniature(View view) { // Picture for the background. - final ImageView imageView = (ImageView) findViewById(R.id.normal); + final ImageView imageView = (ImageView) findViewById(R.id.background); // Button for show miniature picture. final Button miniature = (Button) view; @@ -251,10 +250,7 @@ public class CameraActivity extends Activity { modeMiniature = false; // resize miniature. - LayoutParams paramsReagrandissement = (LayoutParams) imageView.getLayoutParams(); - paramsReagrandissement.width = -1; - paramsReagrandissement.height = -1; - imageView.setLayoutParams(paramsReagrandissement); + setBackground(); // imageView.setAlpha(imageView.getAlpha()); miniature.setVisibility(View.VISIBLE); @@ -395,4 +391,56 @@ 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. + Bundle currentBundle = this.getIntent().getExtras(); + if (currentBundle != null) { + // Get picture. + byte[] imgBackgroundBase64 = currentBundle.getByteArray("imgBackgroundBase64"); + 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. + FrameLayout.LayoutParams paramsMiniature = new FrameLayout.LayoutParams(widthBackground, heightBackground); + if (heightBackground > displayHeightPx && widthBackground < displayWidthPx) { + // Picture's height greater than device's height AND device's width greater than picture's width. + paramsMiniature.width = (int) (displayHeightPx * widthBackground / heightBackground); + paramsMiniature.height = (int) displayHeightPx; + } else if (heightBackground < displayHeightPx && widthBackground > displayWidthPx) { + // Picture's width greater than device's width AND device's height greater than picture's height. + paramsMiniature.width = (int) displayWidthPx; + paramsMiniature.height = (int) (displayWidthPx * heightBackground / widthBackground); + } else if (heightBackground > displayHeightPx && widthBackground > displayWidthPx) { + // Picture's width & Picture's height greater than device's width & device's height. + if (heightBackground > widthBackground) { + // Picture's height greater than Picture's width. + paramsMiniature.width = (int) (displayHeightPx * widthBackground / heightBackground); + paramsMiniature.height = (int) displayHeightPx; + } else { + // Picture's width greater than Picture's height. + paramsMiniature.width = (int) displayWidthPx; + paramsMiniature.height = (int) (displayWidthPx * heightBackground / widthBackground); + } + } + + // set image at the view. + ImageView imageView = (ImageView) findViewById(R.id.background); + imageView.setImageBitmap(imgBackgroundBitmap); + imageView.setLayoutParams(paramsMiniature); + } + } } 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( From 252a7952743fadb6f4715f5ae3b0f083a877432a Mon Sep 17 00:00:00 2001 From: Christophe BOUCAUT Date: Mon, 8 Dec 2014 12:28:34 +0100 Subject: [PATCH 2/3] =?UTF-8?q?Modification=20pour=20permettre=20de=20cent?= =?UTF-8?q?rer=20l'image=20re=C3=A7ue=20avec=20la=20meilleure=20taille=20p?= =?UTF-8?q?ossible.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../res/layout/activity_camera_view.xml | 16 +-- .../geneanet/customcamera/CameraActivity.java | 115 ++++++++---------- 2 files changed, 56 insertions(+), 75 deletions(-) diff --git a/src/android/customCamera/res/layout/activity_camera_view.xml b/src/android/customCamera/res/layout/activity_camera_view.xml index 314c463..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; } @@ -251,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); @@ -266,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,12 +284,6 @@ public class CameraActivity extends Activity { public void showMiniature(View view) { // Picture for the background. ImageView imageView = (ImageView) findViewById(R.id.background); - if (!photoTaken) { - imageView.setScaleType(ImageView.ScaleType.FIT_END); - } - else { - imageView.setScaleType(ImageView.ScaleType.FIT_START); - } // Button for show miniature picture. final Button miniature = (Button) view; @@ -305,15 +300,9 @@ public class CameraActivity extends Activity { public void onClick(View v) { modeMiniature = false; ImageView imageView = (ImageView) findViewById(R.id.background); - imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE); // resize miniature. - setBackground(); - - LayoutParams paramsReagrandissement = (LayoutParams) imageView.getLayoutParams(); imageView.setClickable(false); - paramsReagrandissement.width = -1; - paramsReagrandissement.height = -1; - imageView.setLayoutParams(paramsReagrandissement); + setBackground(); miniature.setVisibility(View.VISIBLE); } @@ -327,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); } /** @@ -421,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){ + if (modeMiniature) { ImageView imageView = (ImageView) findViewById(R.id.background); - imageView.setScaleType(ImageView.ScaleType.FIT_START); setParamsMiniature(imageView, false); } @@ -435,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()); @@ -458,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) { + 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); - imageView.setScaleType(ImageView.ScaleType.FIT_END); 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(); } }); }; @@ -532,7 +518,7 @@ public class CameraActivity extends Activity { int heightBackground = (int) (imgBackgroundBitmap.getHeight() * displayMetrics.density); // Change size ImageView. - FrameLayout.LayoutParams paramsMiniature = new FrameLayout.LayoutParams(widthBackground, heightBackground); + RelativeLayout.LayoutParams paramsMiniature = new RelativeLayout.LayoutParams(widthBackground, heightBackground); if (heightBackground > displayHeightPx && widthBackground < displayWidthPx) { // Picture's height greater than device's height AND device's width greater than picture's width. paramsMiniature.width = (int) (displayHeightPx * widthBackground / heightBackground); @@ -557,7 +543,8 @@ public class CameraActivity extends Activity { // set image at the view. ImageView imageView = (ImageView) findViewById(R.id.background); imageView.setImageBitmap(imgBackgroundBitmap); - imageView.setLayoutParams(paramsMiniature); + paramsMiniature.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE); + imageView.setLayoutParams(paramsMiniature); } } } From aaca25e395f7ddbe41cf93a8444c1fa5ffeac536 Mon Sep 17 00:00:00 2001 From: Christophe BOUCAUT Date: Thu, 11 Dec 2014 12:22:14 +0100 Subject: [PATCH 3/3] =?UTF-8?q?Correction=20sur=20le=20redimensionnement?= =?UTF-8?q?=20de=20l'image.=20Mise=20en=20place=20d'une=20classe=20pour=20?= =?UTF-8?q?le=20transfert=20des=20images=20trop=20grosse=20entre=20activit?= =?UTF-8?q?=C3=A9e.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin.xml | 1 + src/android/CameraLauncher.java | 7 +---- .../geneanet/customcamera/CameraActivity.java | 31 ++++++------------- .../customcamera/TransferBigData.java | 24 ++++++++++++++ 4 files changed, 36 insertions(+), 27 deletions(-) create mode 100644 src/android/customCamera/src/org/geneanet/customcamera/TransferBigData.java 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 1e3cbb5..791948b 100644 --- a/src/android/CameraLauncher.java +++ b/src/android/CameraLauncher.java @@ -16,9 +16,7 @@ import java.io.InputStreamReader; import java.io.IOException; import android.content.Intent; -import android.os.Bundle; import android.util.Base64; -import android.util.Log; public class CameraLauncher extends CordovaPlugin { @@ -49,10 +47,7 @@ public class CameraLauncher extends CordovaPlugin { return false; } - - Bundle imgBackground = new Bundle(); - imgBackground.putByteArray("imgBackgroundBase64", imgBackgroundBase64); - intent.putExtras(imgBackground); + TransferBigData.setImgBackgroundBase64(imgBackgroundBase64); cordova.startActivityForResult((CordovaPlugin) this, intent, CameraLauncher.REQUEST_CODE); diff --git a/src/android/customCamera/src/org/geneanet/customcamera/CameraActivity.java b/src/android/customCamera/src/org/geneanet/customcamera/CameraActivity.java index 76b7bbc..825eb45 100644 --- a/src/android/customCamera/src/org/geneanet/customcamera/CameraActivity.java +++ b/src/android/customCamera/src/org/geneanet/customcamera/CameraActivity.java @@ -500,10 +500,9 @@ public class CameraActivity extends Activity { */ protected void setBackground() { // Get the base64 picture for the background only if it's exist. - Bundle currentBundle = this.getIntent().getExtras(); - if (currentBundle != null) { + byte[] imgBackgroundBase64 = TransferBigData.getImgBackgroundBase64(); + if (imgBackgroundBase64 != null) { // Get picture. - byte[] imgBackgroundBase64 = currentBundle.getByteArray("imgBackgroundBase64"); Bitmap imgBackgroundBitmap = BitmapFactory.decodeByteArray(imgBackgroundBase64, 0, imgBackgroundBase64.length); // Get sizes screen. @@ -519,30 +518,20 @@ public class CameraActivity extends Activity { // Change size ImageView. RelativeLayout.LayoutParams paramsMiniature = new RelativeLayout.LayoutParams(widthBackground, heightBackground); - if (heightBackground > displayHeightPx && widthBackground < displayWidthPx) { - // Picture's height greater than device's height AND device's width greater than picture's width. - paramsMiniature.width = (int) (displayHeightPx * widthBackground / heightBackground); - paramsMiniature.height = (int) displayHeightPx; - } else if (heightBackground < displayHeightPx && widthBackground > displayWidthPx) { - // Picture's width greater than device's width AND device's height greater than picture's height. + float ratioX = (float) displayWidthPx / (float) widthBackground; + float ratioY = (float) displayHeightPx / (float) heightBackground; + if (ratioX < ratioY && ratioX < 1) { paramsMiniature.width = (int) displayWidthPx; - paramsMiniature.height = (int) (displayWidthPx * heightBackground / widthBackground); - } else if (heightBackground > displayHeightPx && widthBackground > displayWidthPx) { - // Picture's width & Picture's height greater than device's width & device's height. - if (heightBackground > widthBackground) { - // Picture's height greater than Picture's width. - paramsMiniature.width = (int) (displayHeightPx * widthBackground / heightBackground); - paramsMiniature.height = (int) displayHeightPx; - } else { - // Picture's width greater than Picture's height. - paramsMiniature.width = (int) displayWidthPx; - paramsMiniature.height = (int) (displayWidthPx * heightBackground / widthBackground); - } + 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; + } +}