在我的应用程序中,我使用ACTION_IMAGE_CAPTURE
意图来拍摄照片。当相机返回时,文件被选中,如果旋转为纵向一个位图旋转并保存到磁盘上用下面的代码:已保存的JPEG是旋转后从磁盘加载的JPEG的两倍大
BitmapFactory.Options options = new Options();
options.inPreferredConfig = Bitmap.Config.RGB_565;
Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath(), options);
if (bmp != null) {
Matrix m = new Matrix();
m.postRotate(90);
Bitmap rotated = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), m,
true);
rotated = rotated.copy(Bitmap.Config.RGB_565, false); // added based on comment
f.delete();
FileOutputStream fos = new FileOutputStream(f);
rotated.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.close();
}
这工作,因为它应该,但文件大小的两倍非旋转图片。我试图将BitmapFactory.Options
上的密度设置为0,将比例设置为假,但都没有达到预期的效果。我希望我转换的图像与我从磁盘加载的图像大小相同。我的代码中是否有防止这种情况发生的东西?
相当不错的描述如何计算大小;)实际上已经错过了。 – Warpzit 2012-07-09 13:55:10
谢谢你的回复。我添加了一行代码,应该将旋转后的位图转换为适当的像素格式,但问题依然存在。任何其他想法?我认为在设置BitmapFactory.Options – redwoolf 2012-07-09 14:17:19
上的RGB_565时考虑了像素格式。我不确定Android如何处理像这样的位图格式。您更改的行可能不会影响位图如何输出(重新压缩)到新文件。 – MusiGenesis 2012-07-09 14:21:50