Subversion Repository Public Repository

Nextrek

Diff Revisions 679 vs 690 for /Android/SmartCharging/SmartCharging/app/src/main/java/it/fedeloper/smartcharging/Manager/Utils.java

Diff revisions: vs.
  @@ -30,6 +30,42 @@
30 30 * Created by federico on 11/08/2015.
31 31 */
32 32 public class Utils {
33 +
34 + public static Bitmap getResizedBitmapFromStream(InputStream imageStream){
35 + BitmapFactory.Options options = new BitmapFactory.Options();
36 + options.inJustDecodeBounds = true;
37 + BitmapFactory.decodeStream(imageStream, null, options);
38 +
39 + options.inJustDecodeBounds = false;
40 +
41 + options.inSampleSize = calculateInSampleSize(options, 1000, 2000);
42 + return BitmapFactory.decodeStream(imageStream, null, options);
43 + }
44 +
45 + public static int calculateInSampleSize(
46 + BitmapFactory.Options options, int reqWidth, int reqHeight) {
47 + // Raw height and width of image
48 + final int height = options.outHeight;
49 + final int width = options.outWidth;
50 + int inSampleSize = 1;
51 +
52 + if (height > reqHeight || width > reqWidth) {
53 +
54 + final int halfHeight = height / 2;
55 + final int halfWidth = width / 2;
56 +
57 + // Calculate the largest inSampleSize value that is a power of 2 and keeps both
58 + // height and width larger than the requested height and width.
59 + while ((halfHeight / inSampleSize) > reqHeight
60 + && (halfWidth / inSampleSize) > reqWidth) {
61 + inSampleSize *= 2;
62 + }
63 + }
64 +
65 + return inSampleSize;
66 + }
67 +
68 +
33 69 public static int dpToPx(int dp, Context c) {
34 70 DisplayMetrics displayMetrics = c.getResources().getDisplayMetrics();
35 71 int px = Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));