我的相机的JPEG是3264x1952。这个文件大约是1.7兆字节。
我想知道无论如何要将此图像压缩为较小的文件大小而不更改分辨率。即我希望我的结果图像也是3264x1952。
我甚至无法在不使用inSampleSize选项缩小图像的情况下将此JPEG打开为位图。
任何人都知道我的选择是什么?有没有办法减少颜色位深度/增加压缩/降低质量?
编辑: 我专门为Android寻找解决方案。主要问题是我无法打开完整的JPEG JPEG而没有遇到OOM错误,所以我不知道如何继续。
理想情况下,我想这样做:
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, out);
但是当我这样做时应用程序会崩溃:
Bitmap bitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
答案 0 :(得分:1)
您可以使用BitmapFactory.decodeFile
方法作为BitmapFactory.Options
对象的第二参数,并使用inSampleSize
来缩放图像。例如,inSampleSize = 2
将生成width/2
和height/2,
的输出图像。请记住,您的位图始终需要width x heigth x 4
个字节的内存
答案 1 :(得分:0)
我认为您想使用BitmapFactory.decodeStream,从JPEG文件中获取流。您可以在返回位图之前修改密度。
答案 2 :(得分:0)
您需要缩小图像。
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
使用适当的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;
}