【发布时间】:2014-07-28 15:12:22
【问题描述】:
我正在编写代码将 png 文件转换回 bmp 并将其保存在 sdcard 上。这是我当前的代码。
FileInputStream in;
BufferedInputStream buf;
try {
in = new FileInputStream("File_Path_to_Read.png");
buf = new BufferedInputStream(in);
byte[] bMapArray= new byte[buf.available()];
buf.read(bMapArray);
Bitmap bMap = BitmapFactory.decodeByteArray(bMapArray, 0, bMapArray.length);
//Code segment to save on file
int numBytesByRow = bMap.getRowBytes() * bMap.getHeight();
ByteBuffer byteBuffer = ByteBuffer.allocate(numBytesByRow);
bMap.copyPixelsToBuffer(byteBuffer);
byte[] bytes = byteBuffer.array();
FileOutputStream fileOuputStream = new FileOutputStream("File_Path_To_Save.bmp");
fileOuputStream.write(bytes);
fileOuputStream.close();
if (in != null) {
in.close();
}
if (buf != null) {
buf.close();
}
} catch (Exception e) {
}
我在将 bMap 保存到 SD 卡时遇到问题。我发现的所有示例都使用 bMap.compress()。使用这种方法我无法保存为 bmp。有人可以举例说明如何将位图保存在 SD 卡上吗?
编辑: 我现在可以将文件另存为 .bmp 到 sdcard。但是它不会达到原始大小。关于将 PNG 转换为 BMP 的任何建议?
【问题讨论】:
-
bitmap .compress(Bitmap.CompressFormat.JPEG, 80, out),无论何时使用此方法,保存的图像质量总是会发生变化,因此您必须通过更改方法的2参数自己尝试以获得最佳效果。 -
我知道使用压缩方法时图像质量会发生变化。但是,我正在寻找一种将 JPEG 转换回 BMP 的方法。使用我编辑的代码,原始 BMP 大约为 80 Mb,我得到 14 KB。显然解压不起作用...可以在 Android 上转换回 BMP(从 JPEG)吗?
标签: android bitmap png compression