代码之家  ›  专栏  ›  技术社区  ›  synic

按比例调整位图大小

  •  5
  • synic  · 技术社区  · 14 年前

    我有个位图。。。如果位图的高度大于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: bitmap size exceeds 32bits

    3 回复  |  直到 14 年前
        1
  •  8
  •   oli    14 年前

    您的scaleWidth和scaleHeight应该是比例因子(所以不是很大的数字),但是您的代码似乎传入了您要查找的实际宽度和高度。所以你最终会大幅增加位图的大小。

    不过,我认为派生scaleWidth和scaleHeight的代码还有其他问题。首先,你的代码总是 scaleHeight=高度 缩放因子 .

    最大比率>比率 ? 你不是应该检查一下吗 ?

        2
  •  1
  •   Hussain Istinra    13 年前

    这是因为 scaleWidth scaleHeight 太大了, 标度宽度 刻度高度 是平均放大或缩小率,但不是 width height ,比率过大导致 bitmap 大小超过32位

    matrix.postScale(scaleWidth, scaleHeight);
    
        3
  •  1
  •   user595349 user595349    11 年前

    我就是这样做的:

    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
    }