简体   繁体   English

在不改变原始纵横比的情况下将位图调整为 512x512

[英]Resize Bitmap to 512x512 Without changing original aspect ratio

I have a created bitmaps.我有一个创建的位图。 Sizes are not specific.尺寸不具体。 Sometimes 120x60 , 129x800 , 851x784.有时 120x60 、 129x800 、 851x784。 Its not have a specific value... I want to make these bitmaps resizing to 512x512 always but without changing original images aspect ratio.它没有特定的值......我想让这些位图始终调整为 512x512,但不改变原始图像的纵横比。 And without cropping.而且没有裁剪。 New image must have canvas 512x512 and original image must be center without any cropping.新图像必须有 512x512 的画布,原始图像必须居中,没有任何裁剪。

I was resizing my bitmaps with this function but it makes images really bad because image fitting X and Y .我正在使用此功能调整位图大小,但它使图像非常糟糕,因为图像拟合 X 和 Y 。 I don't want image to fit x and y on same time fits one of it and keeps its aspect ratio.我不希望图像同时适合 x 和 y 适合其中之一并保持其纵横比。

 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;
    }

What I have;我拥有的;

在此处输入图片说明

What I want;我想要的是;

在此处输入图片说明

Ok, so you're really close. 好的,您真的很亲近。 I can't test this right now, but basically what needs to be changed is 我目前无法测试,但是基本上需要更改的是

1) You need to apply the same scale to both X and Y, so you need to pick the smaller one (try the bigger one if that doesn't work). 1)您需要对X和Y应用相同的比例尺,因此需要选择较小的比例尺(如果不可行,请尝试较大的比例尺)。

matrix.postScale(Math.min(scaleWidth, scaleHeight), Math.min(scaleWidth, scaleHeight));

2) The result will be a bitmap where at least one side is 512px large, the other one will be smaller. 2)结果将是一个位图,其中至少一侧为512px大,另一侧较小。 So you need to add the padding to fit that side to 512px (equally left and right/top and bottom for centering). 因此,您需要添加填充以使该边适合512像素(即居中的左右,上/下)。 In order to do so, you need to create an new bitmap of the desired size: 为此,您需要创建所需大小的新位图:

Bitmap outputimage = Bitmap.createBitmap(512,512, Bitmap.Config.ARGB_8888);

3) and lastly depending on what side of the resizedBitmap is 512px you need to draw resizedBitmap to the correct position in outputImage 3),最后这取决于侧的resizedBitmap是你512x512像素需要绘制resizedBitmap在正确的位置outputImage

Canvas can = new Canvas(outputimage);
can.drawBitmap(resizedBitmap, (512 - resizedBitmap.getWidth()) / 2, (512 - resizedBitmap.getHeight()) / 2, null);

Note here, that 512 - resizedBitmap.getWidth() results in 0 and therefor no padding at the side with correct size. 请注意,这里512 - resizedBitmap.getWidth()结果为0 ,因此在具有正确大小的一侧没有填充。

4) Now return outputImage 4)现在返回outputImage

Here's a simplification in Kotlin that does both the scale and the translation with the matrix, skipping the intermediate bitmap.这是 Kotlin 中的一个简化,它使用矩阵进行缩放和平移,跳过中间位图。

Note that it also sets the background color to white for new pixels, which I needed for my image pipeline.请注意,它还将新像素的背景颜色设置为白色,这是我的图像管道所需要的。 Feel free to remove that if you don't need it.如果您不需要它,请随意删除它。

fun resizedBitmapWithPadding(bitmap: Bitmap, newWidth: Int, newHeight: Int) : Bitmap {
    val scale = min(newWidth.toFloat() / bitmap.width, newHeight.toFloat() / bitmap.height)
    val scaledWidth = scale * bitmap.width
    val scaledHeight = scale * bitmap.height

    val matrix = Matrix()
    matrix.postScale(scale, scale)
    matrix.postTranslate(
        (newWidth - scaledWidth) / 2f,
        (newHeight - scaledHeight) / 2f
    )

    val outputBitmap = Bitmap.createBitmap(newWidth, newHeight, bitmap.config)
    outputBitmap.eraseColor(Color.WHITE)
    
    Canvas(outputBitmap).drawBitmap(
        bitmap,
        matrix,
        null
    )

    return outputBitmap
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM