How to Resize a Bitmap in Android?

后端 未结 16 1981
有刺的猬
有刺的猬 2020-11-22 03:13

I have a bitmap taken of a Base64 String from my remote database, (encodedImage is the string representing the image with Base64):

profileImage          


        
相关标签:
16条回答
  • 2020-11-22 03:34
      public Bitmap scaleBitmap(Bitmap mBitmap) {
            int ScaleSize = 250;//max Height or width to Scale
            int width = mBitmap.getWidth();
            int height = mBitmap.getHeight();
            float excessSizeRatio = width > height ? width / ScaleSize : height / ScaleSize;
             Bitmap bitmap = Bitmap.createBitmap(
                    mBitmap, 0, 0,(int) (width/excessSizeRatio),(int) (height/excessSizeRatio));
            //mBitmap.recycle(); if you are not using mBitmap Obj
            return bitmap;
        }
    
    0 讨论(0)
  • 2020-11-22 03:38

    Someone asked how to keep aspect ratio in this situation:

    Calculate the factor you are using for scaling and use it for both dimensions. Let´s say you want an image to be 20% of the screen in height

    int scaleToUse = 20; // this will be our percentage
    Bitmap bmp = BitmapFactory.decodeResource(
        context.getResources(), R.drawable.mypng);
    int sizeY = screenResolution.y * scaleToUse / 100;
    int sizeX = bmp.getWidth() * sizeY / bmp.getHeight();
    Bitmap scaled = Bitmap.createScaledBitmap(bmp, sizeX, sizeY, false);
    

    for getting the screen resolution you have this solution: Get screen dimensions in pixels

    0 讨论(0)
  • 2020-11-22 03:39
    import android.graphics.Matrix
    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);
        bm.recycle();
        return resizedBitmap;
    }
    

    EDIT: as suggested by by @aveschini, I have added bm.recycle(); for memory leaks. Please note that in case if you are using the previous object for some other purposes, then handle accordingly.

    0 讨论(0)
  • 2020-11-22 03:39
    public static Bitmap resizeBitmapByScale(
                Bitmap bitmap, float scale, boolean recycle) {
            int width = Math.round(bitmap.getWidth() * scale);
            int height = Math.round(bitmap.getHeight() * scale);
            if (width == bitmap.getWidth()
                    && height == bitmap.getHeight()) return bitmap;
            Bitmap target = Bitmap.createBitmap(width, height, getConfig(bitmap));
            Canvas canvas = new Canvas(target);
            canvas.scale(scale, scale);
            Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
            canvas.drawBitmap(bitmap, 0, 0, paint);
            if (recycle) bitmap.recycle();
            return target;
        }
        private static Bitmap.Config getConfig(Bitmap bitmap) {
            Bitmap.Config config = bitmap.getConfig();
            if (config == null) {
                config = Bitmap.Config.ARGB_8888;
            }
            return config;
        }
    
    0 讨论(0)
  • 2020-11-22 03:39

    Keeping the aspect ratio,

      public Bitmap resizeBitmap(Bitmap source, int width,int height) {
        if(source.getHeight() == height && source.getWidth() == width) return source;
        int maxLength=Math.min(width,height);
        try {
            source=source.copy(source.getConfig(),true);
            if (source.getHeight() <= source.getWidth()) {
                if (source.getHeight() <= maxLength) { // if image already smaller than the required height
                    return source;
                }
    
                double aspectRatio = (double) source.getWidth() / (double) source.getHeight();
                int targetWidth = (int) (maxLength * aspectRatio);
    
                return Bitmap.createScaledBitmap(source, targetWidth, maxLength, false);
            } else {
    
                if (source.getWidth() <= maxLength) { // if image already smaller than the required height
                    return source;
                }
    
                double aspectRatio = ((double) source.getHeight()) / ((double) source.getWidth());
                int targetHeight = (int) (maxLength * aspectRatio);
    
                return Bitmap.createScaledBitmap(source, maxLength, targetHeight, false);
    
            }
        }
        catch (Exception e)
        {
            return source;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 03:41

    Scale based on aspect ratio:

    float aspectRatio = yourSelectedImage.getWidth() / 
        (float) yourSelectedImage.getHeight();
    int width = 480;
    int height = Math.round(width / aspectRatio);
    
    yourSelectedImage = Bitmap.createScaledBitmap(
        yourSelectedImage, width, height, false);
    

    To use height as base intead of width change to:

    int height = 480;
    int width = Math.round(height * aspectRatio);
    
    0 讨论(0)
提交回复
热议问题