当我从 PNG 转换为 JPEG,然后将 JPEG 转换为 PNG 时,我遇到了图片大小问题。
public void onClick(View v) {
String imageFileName = "/sdcard/Penguins2.png";
File imageFile = new File(imageFileName);
if (imageFile.exists()) {
// Load the image from file
myBitmap = BitmapFactory.decodeFile(imageFileName);
// Display the image in the image viewer
myImageView = (ImageView) findViewById(R.id.my_image_view);
if (myImageView != null) {
myImageView.setImageBitmap(myBitmap);
}
}
}
转换:
private void processImage() {
try {
String outputPath = "/sdcard/Penguins2.jpg";
int quality = 100;
FileOutputStream fileOutStr = new FileOutputStream(outputPath);
BufferedOutputStream bufOutStr = new BufferedOutputStream(
fileOutStr);
myBitmap.compress(CompressFormat.JPEG, quality, bufOutStr);
bufOutStr.flush();
bufOutStr.close();
} catch (FileNotFoundException exception) {
Log.e("debug_log", exception.toString());
} catch (IOException exception) {
Log.e("debug_log", exception.toString());
}
myImageView.setImageBitmap(myBitmap);
处理完这个操作后,我只需更改这些行:
String imageFileName = "/sdcard/Penguins2.png";
到
String imageFileName = "/sdcard/Penguins2.jpg";
和
String outputPath = "/sdcard/Penguins2.jpg";
(...)
myBitmap.compress(CompressFormat.JPEG, quality, bufOutStr);
到
String outputPath = "/sdcard/Penguins2.png";
(...)
myBitmap.compress(CompressFormat.PNG, quality, bufOutStr);
图像大小从 585847 更改为 531409(在 DDMS 中)
我想做这样的事情,因为我想使用 PNG,它对某些图像处理是无损的。 然后将图像转换为 jpeg 并作为 MMS 发送,我不确定,但我认为 JPEG 是 MMS 中所有设备都支持的唯一格式。接收器将打开图像并将其转换回 png 而不会丢失数据。
最佳答案
除了@Sherif elKhatib 的回答,如果您查看文档:http://developer.android.com/reference/android/graphics/Bitmap.html#compress%28android.graphics.Bitmap.CompressFormat,%20int,%20java.io.OutputStream%29
您可以看到 PNG 图像没有使用质量参数:
quality: Hint to the compressor, 0-100. 0 meaning compress for small size, 100 meaning compress for max quality. Some formats, like PNG which is lossless, will ignore the quality setting
关于android - 位图压缩 PNG -> JPEG,在 Android 中反之亦然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14604031/