在设置为android中的imageview之前,如何将位图从10mb图像压缩到300kb

时间:2014-12-11 04:40:52

标签: android bitmap

我有一堆代码:

Bitmap bitmap;
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),bitmapOptions); 

最后位图有来自相机的图像(我从高分辨率相机拍摄图像)所以尺寸可能是10mb。


我想做什么::

我正在尝试在imageview中设置位图,如下所示

portfolioPicImgId.setImageBitmap(bitmap);
  • 在此之前,我想将图像压缩成300kb,我该如何实现 这个!
  • 我已经看过其他stackoverflow答案,但是如何准确指定 300KB

3 个答案:

答案 0 :(得分:1)

很难获得300kb。它取决于质量,每个像素包含4(32位)或2(16位)字节,这意味着图像应包含75000像素(在32位图像中)。下一步获取图像比例以简化图像1:1 - sqrt(75000)~274像素的比例,我们得到274x274像素将是~300kb ram。

您需要处理此数据,并在选项中指定宽度和高度。

答案 1 :(得分:0)

你试过这个吗? :

//decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f){
    try {
        //Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f),null,o);

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

        //Find the correct scale value. It should be the power of 2.
        int scale=1;
        while(o.outWidth/scale/2>=REQUIRED_SIZE && o.outHeight/scale/2>=REQUIRED_SIZE)
            scale*=2;

        //Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize=scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {}
    return null;
}

答案 2 :(得分:0)

只需通过以下功能中的图片路径

    public Bitmap get_Reduce_bitmap_Picture(String imagePath) {

    int ample_size = 16;
     // change ample_size to 32 or any power of 2 to increase or decrease bitmap size of image


    Bitmap bitmap = null;
    BitmapFactory.Options bitoption = new BitmapFactory.Options();
    bitoption.inSampleSize = ample_size;

    Bitmap bitmapPhoto = BitmapFactory.decodeFile(imagePath, bitoption);

    ExifInterface exif = null;
    try {
        exif = new ExifInterface(imagePath);
    } catch (IOException e) {
        // Auto-generated catch block
        e.printStackTrace();
    }
    int orientation = exif
            .getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
    Matrix matrix = new Matrix();

    if ((orientation == 3)) {
        matrix.postRotate(180);
        bitmap = Bitmap.createBitmap(bitmapPhoto, 0, 0,
                bitmapPhoto.getWidth(), bitmapPhoto.getHeight(), matrix,
                true);

    } else if (orientation == 6) {
        matrix.postRotate(90);
        bitmap = Bitmap.createBitmap(bitmapPhoto, 0, 0,
                bitmapPhoto.getWidth(), bitmapPhoto.getHeight(), matrix,
                true);

    } else if (orientation == 8) {
        matrix.postRotate(270);
        bitmap = Bitmap.createBitmap(bitmapPhoto, 0, 0,
                bitmapPhoto.getWidth(), bitmapPhoto.getHeight(), matrix,
                true);

    } else {
        matrix.postRotate(0);
        bitmap = Bitmap.createBitmap(bitmapPhoto, 0, 0,
                bitmapPhoto.getWidth(), bitmapPhoto.getHeight(), matrix,
                true);

    }

    return bitmap;

}