用于android示例的ImageResize

时间:2016-10-08 13:28:39

标签: android resize image-resizing

假设我的图像大小为20mb,我需要将其缩小到最大目标大小15Mb。我如何实现这一目标?

我找不到除 createScaledBitmap 之外的任何api,它只取得图片,它的高度和宽度,但没有目标尺寸。

我怎样才能做到这一点?我应该盲目地继续尝试不同的宽度和高度,直到它缩小到< =目标尺寸?

请帮忙。

3 个答案:

答案 0 :(得分:0)

Graphics2D 正在提供图像重新调整大小功能,如下所示:

BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
g.dispose();

根据一些文章,如果您想增加重新调整大小的图像质量,可以按如下方式添加 RenderingHints

BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
g.dispose();
g.setComposite(AlphaComposite.Src);
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

完整示例http://www.mkyong.com/java/how-to-resize-an-image-in-java/

答案 1 :(得分:0)

您是否检查了此link

据我所知,您使用Bitmap

缩放图像大小或文件大小

答案 2 :(得分:0)

使用此代码调整图像大小

// Draw Bitmap on Canvas
public void setBitmap(String imageUrl){

    Bitmap myBitmap = BitmapFactory.decodeFile(imageUrl);

    Bitmap scaleBitmap = null;
    int bWidth = myBitmap.getWidth();
    int bHeight = myBitmap.getHeight();
    int diff = 0;

    Log.d(TAG, "bitmapWidth = " + bitmapWidth + " bitmapHeight = " + bitmapHeight);

    Log.d(TAG, "bWidth = " + bWidth + " bHeight = " + bHeight);

    if(bWidth >= bHeight){

        if(bWidth > bitmapWidth ){

            // landscape
            float ratio = (float) bWidth / bitmapWidth;
            diff = bWidth - bitmapWidth;
            bHeight = (int)(bHeight / ratio);
            scaleBitmap = Bitmap.createScaledBitmap(myBitmap, bWidth - diff, bHeight, false);
        }
        else{

            scaleBitmap = myBitmap;
        }
    }
    else{

        if(bHeight > bitmapHeight){

            float ratio = (float) bHeight / bitmapHeight;
            diff = bHeight - bitmapHeight;
            bWidth = (int)(bWidth / ratio);
            scaleBitmap = Bitmap.createScaledBitmap(myBitmap, bWidth, bHeight - diff, false);
        }
        else{

            scaleBitmap = myBitmap;
        }
    }



    canvasBitmap = scaleBitmap;
    invalidate();

}

请抬起箭头,以便其他人也可以获得调整图像大小的链接。