diff --git a/plugin.xml b/plugin.xml index b2d9869..56eccd9 100644 --- a/plugin.xml +++ b/plugin.xml @@ -33,6 +33,7 @@ + diff --git a/src/android/customCamera/src/org/geneanet/customcamera/BitmapUtils.java b/src/android/customCamera/src/org/geneanet/customcamera/BitmapUtils.java new file mode 100644 index 0000000..731847b --- /dev/null +++ b/src/android/customCamera/src/org/geneanet/customcamera/BitmapUtils.java @@ -0,0 +1,66 @@ +package org.geneanet.customcamera; + +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.BitmapFactory.Options; + +public class BitmapUtils { + /** + * Determine the original size of picture. + * + * @param bytes[] imgBackgroundBase64 + * + * @return Options Return the options object with sizes set. + */ + public static Options determineOriginalSizePicture(byte[] imgBackgroundBase64) { + Options options = new Options(); + options.inJustDecodeBounds = true; + BitmapFactory.decodeByteArray(imgBackgroundBase64, 0, imgBackgroundBase64.length, options); + + return options; + } + + /** + * Determine the best value of inSampleSize in the option object to adapt the picture at the screen size. + * + * @param Options options Option object to set inSampleSize. + * @param int destWidth Width destination. + * @param int destHeight Height destination. + */ + public static void determineInSampleSize(Options options, int destWidth, int destHeight) { + // Raw height and width of image + final int height = options.outHeight; + final int width = options.outWidth; + int inSampleSize = 1; + + if (height > destHeight || width > destWidth) { + + final int halfHeight = height / 2; + final int halfWidth = width / 2; + + // Calculate the largest inSampleSize value that is a power of 2 and keeps both + // height and width larger than the requested height and width. + // More informations: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html#load-bitmap + while ((halfHeight / inSampleSize) > destHeight + && (halfWidth / inSampleSize) > destWidth) { + inSampleSize *= 2; + } + } + + options.inSampleSize = inSampleSize; + } + + /** + * Decode a byte array to generate a base64 adapted at the destination's size. + * + * @param bytes[] imgBackgroundBase64 + * @param Options options + * + * @return Bitmap + */ + public static Bitmap decodeOptimalPictureFromByteArray(byte[] imgBackgroundBase64, Options options) { + options.inJustDecodeBounds = false; + + return BitmapFactory.decodeByteArray(imgBackgroundBase64, 0, imgBackgroundBase64.length, options); + } +} diff --git a/src/android/customCamera/src/org/geneanet/customcamera/CameraActivity.java b/src/android/customCamera/src/org/geneanet/customcamera/CameraActivity.java index 355599b..5cf9893 100644 --- a/src/android/customCamera/src/org/geneanet/customcamera/CameraActivity.java +++ b/src/android/customCamera/src/org/geneanet/customcamera/CameraActivity.java @@ -9,6 +9,7 @@ import android.content.res.Resources.NotFoundException; import android.graphics.Bitmap; import android.graphics.Bitmap.CompressFormat; import android.graphics.BitmapFactory; +import android.graphics.BitmapFactory.Options; import android.graphics.Color; import android.graphics.Matrix; import android.graphics.drawable.BitmapDrawable; @@ -46,6 +47,7 @@ import android.widget.TextView; import org.geneanet.customcamera.CameraPreview; import org.geneanet.customcamera.ManagerCamera; import org.geneanet.customcamera.TransferBigData; +import org.geneanet.customcamera.BitmapUtils; import org.xmlpull.v1.XmlPullParserException; import java.io.ByteArrayOutputStream; @@ -469,34 +471,32 @@ public class CameraActivity extends Activity { imgBackgroundBase64 = TransferBigData.getImgBackgroundBase64OtherOrientation(); } 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); + + // Get picture. + Options options = BitmapUtils.determineOriginalSizePicture(imgBackgroundBase64); + int widthResize = 0; + int heightResize = 0; + int widthBackground = options.outWidth; + int heightBackground= options.outHeight; 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); + widthResize = (int) displayWidthPx; + heightResize = (int) (ratioX * heightBackground); } else if (ratioX >= ratioY && ratioY < 1) { - paramsMiniature.width = (int) (ratioY * widthBackground); - paramsMiniature.height = (int) displayHeightPx; + widthResize = (int) (ratioY * widthBackground); + heightResize = (int) displayHeightPx; } + BitmapUtils.determineInSampleSize(options, displayWidthPx, displayHeightPx); + Bitmap imgBackgroundBitmap = BitmapUtils.decodeOptimalPictureFromByteArray(imgBackgroundBase64, options); + // set image at the view. ImageView background = (ImageView) findViewById(R.id.background); background.setImageBitmap(imgBackgroundBitmap); @@ -507,6 +507,7 @@ public class CameraActivity extends Activity { background.setAlpha((float)1); } + RelativeLayout.LayoutParams paramsMiniature = new RelativeLayout.LayoutParams(widthResize, heightResize); paramsMiniature.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE); @@ -734,12 +735,6 @@ public class CameraActivity extends Activity { // Temporarily storage to use for decoding opt.inTempStorage = new byte[16 * 1024]; - Camera.Parameters paramsCamera = customCamera.getParameters(); - Size size = paramsCamera.getPictureSize(); - - int height = size.height; - int width = size.width; - float res = (width * height) / 1024000; // Preview from camera photoTaken = BitmapFactory.decodeByteArray(data, 0, data.length, opt);