change bitmap resolution in Android app

后端 未结 3 1899
日久生厌
日久生厌 2021-01-14 08:16

I\'m writing an application that uses the phone\'s camera to take a picture, and then use it in my app. The thing is, the app runs out of memory, and it is probably because

相关标签:
3条回答
  • 2021-01-14 08:59

    from jeet.chanchawat' s answer: https://stackoverflow.com/a/10703256/3027225

      public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
            int width = bm.getWidth();
            int height = bm.getHeight();
            float scaleWidth = ((float) newWidth) / width;
            float scaleHeight = ((float) newHeight) / height;
            // CREATE A MATRIX FOR THE MANIPULATION
            Matrix matrix = new Matrix();
            // RESIZE THE BIT MAP
            matrix.postScale(scaleWidth, scaleHeight);
    
            // "RECREATE" THE NEW BITMAP
            Bitmap resizedBitmap = Bitmap.createBitmap(
                bm, 0, 0, width, height, matrix, false);
            return resizedBitmap;
        }
    
    0 讨论(0)
  • 2021-01-14 09:01

    this can be done using Options.inSampleSize, when creating the bitmap

    0 讨论(0)
  • 2021-01-14 09:17

    You can Set Its Width and Height

    Bitmap bm = ShrinkBitmap(imagefile, 150, 150);
    

    Function to Call

    Bitmap ShrinkBitmap(String file, int width, int height){
    
     BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
        bmpFactoryOptions.inJustDecodeBounds = true;
        Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
    
        int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)height);
        int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)width);
    
        if (heightRatio > 1 || widthRatio > 1)
        {
         if (heightRatio > widthRatio)
         {
          bmpFactoryOptions.inSampleSize = heightRatio;
         } else {
          bmpFactoryOptions.inSampleSize = widthRatio; 
         }
        }
    
        bmpFactoryOptions.inJustDecodeBounds = false;
        bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
     return bitmap;
    }
    

    }

    This are two more links which might Help You. Link 1 & Link 2

    0 讨论(0)
提交回复
热议问题