android - 在Android中将位图的大小缩小到指定大小

标签 android bitmap bitmapimage image-compression

我想将位图的大小精确地减小到 200kb。我从 sdcard 中获取图像,将其压缩并以不同的名称再次保存到 sdcard 中的不同目录中。压缩效果很好(3 mb 的图像被压缩到大约 100 kb)。我为此编写了以下代码行:

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;
        }

最佳答案

我找到了一个非常适合我的答案:

/**
 * 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);
}

调用方法:

Bitmap converetdImage = getResizedBitmap(photo, 500);

photo 是您的位图

关于android - 在Android中将位图的大小缩小到指定大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16954109/

相关文章:

android - LocationServices.FusedLocationApi.getLastLocation 在 onCreate() 方法中不起作用

java - 集成 Facebook 登录时 key 哈希无效

wpf - 将 WPF 控件转换为 BitmapSource

android - 使用按钮 onClick 获取图像

项目中的 Android 单元测试

java - j2objc 转换为 Objective C 时出错

android - 如何在方向更改期间将图像保留在 Android Activity 中?

c# - 第二个路径片段不能是驱动器或 UNC 名称

java - 如何将 R、G 和 B channel 分别拆分为双阵列并使用 Android 位图进行处理

c++ - C/C++ 旋转 BMP 图像