我在将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";
to
String imageFileName = "/sdcard/Penguins2.jpg";
并且
String outputPath = "/sdcard/Penguins2.jpg";
(...)
myBitmap.compress(CompressFormat.JPEG, quality, bufOutStr);
to
String outputPath = "/sdcard/Penguins2.png";
(...)
myBitmap.compress(CompressFormat.PNG, quality, bufOutStr);
图像大小从585847更改为531409(在DDMS中)
我想这样做是因为我想使用PNG进行某些图像处理,PNG是无损的。然后将图像转换为JPEG并作为MMS发送,我不确定,但我认为JPEG是所有设备都支持的MMS格式。接收者将打开图像并将其转换回PNG而不会丢失数据。