33

I want to reduce the size of a bitmap to 200kb exactly. I get an image from the sdcard, compress it and save it to the sdcard again with a different name into a different directory. Compression works fine (3 mb like image is compressed to around 100 kb). I wrote the following lines of codes for this:

String imagefile ="/sdcard/DCIM/100ANDRO/DSC_0530.jpg";
Bitmap bm = ShrinkBitmap(imagefile, 300, 300);

//this method compresses the image and saves into a location in sdcard
    Bitmap ShrinkBitmap(String file, int width, int height){

         BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
            bmpFactoryOptions.inJustDecodeBounds = true;
            Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);

            int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)height);
            int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)width);

            if (heightRatio > 1 || widthRatio > 1)
            {
             if (heightRatio > widthRatio)
             {
              bmpFactoryOptions.inSampleSize = heightRatio;
             } else {
              bmpFactoryOptions.inSampleSize = widthRatio; 
             }
            }

            bmpFactoryOptions.inJustDecodeBounds = false;
            bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);

            ByteArrayOutputStream stream = new ByteArrayOutputStream();   
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);   
            byte[] imageInByte = stream.toByteArray(); 
            //this gives the size of the compressed image in kb
            long lengthbmp = imageInByte.length / 1024; 

            try {
                bitmap.compress(CompressFormat.JPEG, 100, new FileOutputStream("/sdcard/mediaAppPhotos/compressed_new.jpg"));
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


         return bitmap;
        }
4

3 回答 3

69

I found an answer that works perfectly for me:

/**
 * reduces the size of the image
 * @param image
 * @param maxSize
 * @return
 */
public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
    int width = image.getWidth();
    int height = image.getHeight();

    float bitmapRatio = (float)width / (float) height;
    if (bitmapRatio > 1) {
        width = maxSize;
        height = (int) (width / bitmapRatio);
    } else {
        height = maxSize;
        width = (int) (height * bitmapRatio);
    }
    return Bitmap.createScaledBitmap(image, width, height, true);
}

calling the method:

Bitmap converetdImage = getResizedBitmap(photo, 500);

Where photo is your bitmap

于 2014-08-05T10:27:15.050 回答
10

这是限制图像质量而不改变width和的解决方案height。这种方法的主要思想是在循环内压缩位图,同时输出大小大于maxSizeBytesCount. 归功于这个问题的答案。此代码块仅显示降低图像质量的逻辑:

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        int currSize;
        int currQuality = 100;

        do {
            bitmap.compress(Bitmap.CompressFormat.JPEG, currQuality, stream);
            currSize = stream.toByteArray().length;
            // limit quality by 5 percent every time
            currQuality -= 5;

        } while (currSize >= maxSizeBytes);

这是完整的方法:

public class ImageUtils {

    public byte[] compressBitmap(
            String file, 
            int width, 
            int height,
            int maxSizeBytes
    ) {
        BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
        bmpFactoryOptions.inJustDecodeBounds = true;
        Bitmap bitmap;

        int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)height);
        int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)width);

        if (heightRatio > 1 || widthRatio > 1)
        {
            if (heightRatio > widthRatio)
            {
                bmpFactoryOptions.inSampleSize = heightRatio;
            } else {
                bmpFactoryOptions.inSampleSize = widthRatio;
            }
        }

        bmpFactoryOptions.inJustDecodeBounds = false;
        bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        int currSize;
        int currQuality = 100;

        do {
            bitmap.compress(Bitmap.CompressFormat.JPEG, currQuality, stream);
            currSize = stream.toByteArray().length;
            // limit quality by 5 percent every time
            currQuality -= 5;

        } while (currSize >= maxSizeBytes);

        return stream.toByteArray();
    }
}
于 2019-06-02T07:05:41.923 回答
0

@TharakaNirmana 的方法工作正常,maxSize 以千字节为单位

例子:

fun getResizedBitmap(image: Bitmap, maxSize: Int): Bitmap? {
    var width = image.width
    var height = image.height
    val bitmapRatio = width.toFloat() / height.toFloat()
    if (bitmapRatio > 1) {
        width = maxSize
        height = (width / bitmapRatio).toInt()
    } else {
        height = maxSize
        width = (height * bitmapRatio).toInt()
    }
    return Bitmap.createScaledBitmap(image, width, height, true)
}

用法:

bitmap.let {
    val data = it.value

    if (data != null) {

        var image = viewModel.getResizedBitmap(data, 170)
        Log.d ("bitmap size", image!!.byteCount.toString()) ...

回答

2021-12-18 12:38:11.218 25502-25502/com.jdsalasc.sophosSolutions D/位图大小:67200 2021-12-18 12:39:11.673 25745-25745/com.jdsalasc.sophosSolutions D/位图大小:67200 2021-12-18 12:39:14.511 25745-25745/com.jdsalasc.sophosSolutions D/位图大小:90000 2021-12-18 12:39:20.423 25745-25745/com.jdsalasc.sophosSolutions D/位图大小:67200 2021-12-18 12:39:40.112 25907-25907/com.jdsalasc.sophosSolutions D/位图大小:120000 2021-12-18 12:39:43.322 25907-25907/com.jdsalasc.sophosSolutions D/位图大小:120000 2021-12-18 12:39:47.314 25907-25907/com.jdsalasc.sophosSolutions D/位图大小:160000 2021-12-18 12:40:17.231 26069-26069/com.jdsalasc.sophosSolutions D/位图大小:115600 2021-12-18 12:40:19.583 26069-26069/com.jdsalasc.sophosSolutions D/位图大小:86360

你怎么看,字节大小永远不会超过 150 kb

谢谢 Zoe 和 TharakaNirmana c:

于 2021-12-18T17:50:55.660 回答