当我从 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 并作为彩信发送,我不确定,但我认为 JPEG 是彩信中所有设备都支持的唯一格式。接收器会打开图像并将其转换回 png 而不会丢失数据。