【问题标题】:How to resize bitmap to maximum available size?如何将位图大小调整为最大可用大小?
【发布时间】:2013-02-05 14:55:56
【问题描述】:

我有非常大的位图图像。我的来源

BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f), null, o);

            // The new size we want to scale to
            final int REQUIRED_WIDTH = 1000;
            final int REQUIRED_HIGHT = 500;
            // Find the correct scale value. It should be the power of 2.
            int scale = 1;
            while (o.outWidth / scale / 2 >= REQUIRED_WIDTH
                    && o.outHeight / scale / 2 >= REQUIRED_HIGHT)
                scale *= 2;

            // Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);

我想正确调整图像大小,我需要将图像调整为最大可用尺寸

例如

我下载的图片大小为 4000x4000 像素,我的手机支持 2000x1500 像素大小 我需要安德斯滕我的手机支持多大尺寸? 然后我将图像大小调整为 2000x1500(例如)

【问题讨论】:

标签: android bitmap image-resizing


【解决方案1】:

在这里您可以将位图大小调整为最大 avaliabe 大小:

public void onClick() //for example 
{
/*
Getting screen diemesions
*/
WindowManager w = getWindowManager();
        Display d = w.getDefaultDisplay();
        int width = d.getWidth();
        int height = d.getHeight(); 
// "bigging" bitmap
Bitmap nowa = getResizedBitmap(yourbitmap, width, height);
}

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {

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;

}

希望能帮到你

【讨论】:

  • metrics.widthPixels - 这是显示尺寸...显示尺寸比我想要的要小很多
  • @MaxUsanin 对不起,我改变了这个。看看吧。
  • 这个答案仍然基于最大屏幕尺寸,而不是要显示的最大位图尺寸。需要找到的是最大位图大小,在很多手机上是 2048x2048,但这需要以编程方式找到。
【解决方案2】:

不是那么完美,但这是我的解决方案,

fun setScaleImageWithConstraint(bitmap: Bitmap): Bitmap {

        val maxWidth = 500 //max Height Constraint
        val maxHeight = 500 //max Width Constraint

        var width = bitmap.width
        var height = bitmap.height

        if (width > maxWidth || height > maxHeight) {
            //If Image is still Large than allowed W/H Half the Size
            val quarterWidth = ((width / 2).toFloat() + (width / 3).toFloat()).toInt()
            val quarterHeight = ((height / 2).toFloat() + (height / 3).toFloat()).toInt()
            val scaledBitmap = Bitmap.createScaledBitmap(bitmap, quarterWidth, quarterHeight, false)
            //Recursive Call to Resize and return Resized One
            return setScaleImageWithConstraint(scaledBitmap)
        } else {
            //This will be executed when Image is not violating the Constraints
            return bitmap
        }
    }

递归减小位图 1 四分之一大小,直到达到允许的高度和高度。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-23
    • 2013-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多