如何在Android中调整位图大小?

Nul*_*ion 314 base64 android bitmap

我从远程数据库中获取了一个Base64字符串的位图,(encodedImage是用Base64表示图像的字符串):

profileImage = (ImageView)findViewById(R.id.profileImage);

byte[] imageAsBytes=null;
try {
    imageAsBytes = Base64.decode(encodedImage.getBytes());
} catch (IOException e) {e.printStackTrace();}

profileImage.setImageBitmap(
    BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
);
Run Code Online (Sandbox Code Playgroud)

profileImage是我的ImageView

好的,但是我必须在显示我ImageView的布局之前调整此图像的大小.我必须将其调整为120x120.

有人能告诉我调整大小的代码吗?

我找到的示例无法应用于获得位图的base64字符串.

use*_*209 516

更改:

profileImage.setImageBitmap(
    BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
Run Code Online (Sandbox Code Playgroud)

至:

Bitmap b = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
profileImage.setImageBitmap(Bitmap.createScaledBitmap(b, 120, 120, false));
Run Code Online (Sandbox Code Playgroud)

  • 如果我们想保存宽高比怎么办? (24认同)
  • 添加b.recycle()以获得更好的内存性能. (7认同)
  • createScaledBitmap在我的Galaxy Tab2上抛出一个Out of Memory Exception,这对我来说很奇怪,因为有很多内存而且没有其他特定应用正在运行.Matrix解决方案虽然有效. (5认同)
  • 那为什么dpi缩放呢?我认为缩放的位图应该基于设备屏幕的高度和宽度? (3认同)
  • 使用Bitmap.createScaledBitmap()将图像缩小到原始大小的一半以上,可能会产生锯齿现象。您可以看一下我写的[post](https://medium.com/@petrakeas/alias-free-resize-with-renderscript-5bf15a86ce3),我在其中提出了一些替代方案并比较了质量和性能。 (2认同)

jee*_*wat 271

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;
}
Run Code Online (Sandbox Code Playgroud)

编辑:正如@a​​veschini所建议的那样,我添加bm.recycle();了内存泄漏.请注意,如果您将上一个对象用于其他目的,请相应处理.

  • 这与调用Bitmap.createScaledBitmap()相同.请参阅https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/graphics/java/android/graphics/Bitmap.java#645 (10认同)
  • 我尝试了bitmap.createscaledbitmap和这种矩阵方法.我发现使用矩阵方法可以更清晰地显示图像.我不知道这是常见的还是因为我使用的是模拟器而不是手机.对于像我这样经历同样麻烦的人的暗示. (6认同)
  • 在这里你也必须添加 bm.recycle() 以获得更好的内存性能 (2认同)
  • 感谢您的解决方案,但是最好对参数重新排序;公共位图getResizedBitmap(位图bm,int newWidth,int newHeight)。我花了很多时间弄清楚它。; P (2认同)
  • 请注意,Matrix 的正确导入是 android.graphics.Matrix。 (2认同)

Zen*_*nce 113

如果您已有位图,则可以使用以下代码调整大小:

Bitmap originalBitmap = <original initialization>;
Bitmap resizedBitmap = Bitmap.createScaledBitmap(
    originalBitmap, newWidth, newHeight, false);
Run Code Online (Sandbox Code Playgroud)


sag*_*its 35

基于宽高比的比例:

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

yourSelectedImage = Bitmap.createScaledBitmap(
    yourSelectedImage, width, height, false);
Run Code Online (Sandbox Code Playgroud)

使用height作为宽度的基本intead更改为:

int height = 480;
int width = Math.round(height * aspectRatio);
Run Code Online (Sandbox Code Playgroud)


Kev*_*vin 22

使用目标最大大小和宽度缩放位图,同时保持纵横比:

int maxHeight = 2000;
int maxWidth = 2000;    
float scale = Math.min(((float)maxHeight / bitmap.getWidth()), ((float)maxWidth / bitmap.getHeight()));

Matrix matrix = new Matrix();
matrix.postScale(scale, scale);

bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
Run Code Online (Sandbox Code Playgroud)


Rav*_*ana 7

试试这个代码:

BitmapDrawable drawable = (BitmapDrawable) imgview.getDrawable();
Bitmap bmp = drawable.getBitmap();
Bitmap b = Bitmap.createScaledBitmap(bmp, 120, 120, false);
Run Code Online (Sandbox Code Playgroud)

我希望它有用.


Tao*_*hok 7

在这种情况下有人问如何保持纵横比:

计算用于缩放的因子,并将其用于两个维度.假设您希望图像高度为屏幕的20%

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);
Run Code Online (Sandbox Code Playgroud)

要获得屏幕分辨率,您可以使用此解决方案: 以像素为单位获取屏幕尺寸


Asa*_*hry 5

尽管接受的答案是正确的,但它不会Bitmap通过保持相同的Aspect Ratio来调整大小。如果您正在寻找一种Bitmap通过保持相同纵横比来调整大小的方法,您可以使用以下实用函数。此链接提供了该功能的使用详细信息和说明。

public static Bitmap resizeBitmap(Bitmap source, int maxLength) {
       try {
           if (source.getHeight() >= source.getWidth()) {
               int targetHeight = maxLength;
               if (source.getHeight() <= targetHeight) { // if image already smaller than the required height
                   return source;
               }

               double aspectRatio = (double) source.getWidth() / (double) source.getHeight();
               int targetWidth = (int) (targetHeight * aspectRatio);

               Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);
               if (result != source) {
               }
               return result;
           } else {
               int targetWidth = maxLength;

               if (source.getWidth() <= targetWidth) { // if image already smaller than the required height
                   return source;
               }

               double aspectRatio = ((double) source.getHeight()) / ((double) source.getWidth());
               int targetHeight = (int) (targetWidth * aspectRatio);

               Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);
               if (result != source) {
               }
               return result;

           }
       }
       catch (Exception e)
       {
           return source;
       }
   }
Run Code Online (Sandbox Code Playgroud)