Android 按比例调整位图大小

Android 按比例调整位图大小,android,Android,我有一个位图。。。如果位图的高度大于maxHeight,或者宽度大于maxWidth,我想按比例调整图像大小,使其适合maxWidth X maxHeight。以下是我正在尝试的: BitmapDrawable bmp = new BitmapDrawable(getResources(), PHOTO_PATH); int width = bmp.getIntrinsicWidth(); int height = bmp.getIntrinsicHeight();

我有一个位图。。。如果位图的高度大于maxHeight,或者宽度大于maxWidth,我想按比例调整图像大小,使其适合maxWidth X maxHeight。以下是我正在尝试的:

    BitmapDrawable bmp = new BitmapDrawable(getResources(), PHOTO_PATH);

    int width = bmp.getIntrinsicWidth();
    int height = bmp.getIntrinsicHeight();

    float ratio = (float)width/(float)height;

    float scaleWidth = width;
    float scaleHeight = height;

    if((float)mMaxWidth/(float)mMaxHeight > ratio) {
        scaleWidth = (float)mMaxHeight * ratio;
    }
    else {
        scaleHeight = (float)mMaxWidth / ratio;
    }

    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);

    Bitmap out = Bitmap.createBitmap(bmp.getBitmap(), 
            0, 0, width, height, matrix, true);

    try {
        out.compress(Bitmap.CompressFormat.JPEG, 100, 
                new FileOutputStream(PHOTO_PATH));
    }
    catch(FileNotFoundException fnfe) {
        fnfe.printStackTrace();
    }
我得到以下例外情况:

java.lang.IllegalArgumentException:位图大小超过32位


我在这里做错了什么?

您的缩放宽度和缩放高度应该是缩放因子(所以不是很大的数字),但您的代码似乎传递了您要查找的实际宽度和高度。因此,您最终将大幅增加位图的大小

不过,我认为派生scaleWidth和scaleHeight的代码还有其他问题。首先,您的代码总是使用scaleWidth=width或scaleHeight=height,并且只更改其中一个,因此您也会扭曲图像的纵横比。如果您只想调整图像的大小,那么您应该只使用一个scaleFactor


另外,为什么if语句有效地检查maxRatio>ratio?你不应该检查宽度>最大宽度或高度>最大高度吗?

这是因为
缩放宽度或
缩放高度的值太大,
缩放宽度或
缩放高度是指放大或缩小的速率,而不是
宽度或
高度,速率太大导致
位图
大小超过32位

matrix.postScale(scaleWidth, scaleHeight);
我就是这样做的:

public Bitmap decodeAbtoBm(byte[] b){
    Bitmap bm; // prepare object to return

    // clear system and runtime of rubbish
    System.gc();
    Runtime.getRuntime().gc();  

    //Decode image size only
    BitmapFactory.Options oo = new BitmapFactory.Options();
    // only decodes size, not the whole image
    // See Android documentation for more info.
    oo.inJustDecodeBounds = true;
    BitmapFactory.decodeByteArray(b, 0, b.length ,oo);

    //The new size we want to scale to
    final int REQUIRED_SIZE=200;

    // Important function to resize proportionally.
    //Find the correct scale value. It should be the power of 2.
    int scale=1;
    while(oo.outWidth/scale/2>=REQUIRED_SIZE
            && oo.outHeight/scale/2>=REQUIRED_SIZE)
            scale*=2; // Actual scaler

    //Decode Options: byte array image with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize=scale; // set scaler
    o2.inPurgeable = true; // for effeciency
    o2.inInputShareable = true;

    // Do actual decoding, this takes up resources and could crash
    // your app if you do not do it properly
    bm = BitmapFactory.decodeByteArray(b, 0, b.length,o2);

    // Just to be safe, clear system and runtime of rubbish again!
    System.gc();
    Runtime.getRuntime().gc();

    return bm; // return Bitmap to the method that called it
}

你能在这里通过正确的代码吗?我也有同样的例外