繁体   English   中英

在不更改分辨率的情况下缩小大JPEG的大小

[英]Reduce size of a large JPEG without changing resolution

我的相机有JPEG,大小为3264x1952。 该文件约为1.7兆字节。

我想知道是否可以在更改分辨率的情况下将该图像压缩为较小的文件大小。 即我希望我的合成图像也为3264x1952。

如果不使用inSampleSize选项缩小图像,我什至无法将JPEG作为位图打开。

有人知道我的选择是什么吗? 有什么方法可以减少色位深度/增加压缩率/降低画质?

编辑:我专门在寻找Android的解决方案。 主要问题是我不能在未遇到OOM错误的情况下打开完整的res JPEG,所以我不知道如何进行。

理想情况下,我想这样做:

bitmap.compress(Bitmap.CompressFormat.JPEG, 50, out);

但是当我这样做时,应用程序将崩溃:

Bitmap bitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

可以使用BitmapFactory.decodeFile方法,它有作为第二放慢参数一个BitmapFactory.Options对象,并使用inSampleSize缩放图像。 例如, inSampleSize = 2将产生width/2height/2,的输出图像。 只要记住您的位图总是需要width x heigth x 4字节的内存

我认为您想使用BitmapFactory.decodeStream ,从JPEG文件中获取流。 您可以在返回位图之前修改密度。

您需要按比例缩小图像。

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

使用适当的Bitmap.decode方法并按比例缩小图像。

Bitmap.decode

范例:

使用必需的参数调用该方法。

public static Bitmap decodeFile(File f,int WIDTH,int HIGHT){
 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_WIDTH=WIDTH;
     final int REQUIRED_HIGHT=HIGHT;
     //Find the correct scale value. It should be the power of 2.
     int scale=1;
     while(o.outWidth/scale/2>=REQUIRED_WIDTH && o.outHeight/scale/2>=REQUIRED_HIGHT)
         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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM