当我从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,而不会丢失数据。
发布于 2013-01-30 20:37:39
除了@Sherif elKhatib回答之外,如果您查看文档:http://developer.android.com/reference/android/graphics/Bitmap.html#compress%28android.graphics.Bitmap.CompressFormat,%20int,%20java.io.OutputStream%29
您可以看到PNG图像没有使用质量参数:
质量:对压缩机的提示,0-100。0表示压缩为小尺寸,100表示压缩为最大质量。某些格式,如无损的PNG格式,将忽略质量设置
发布于 2013-01-30 20:33:05
这是不可行的!一旦你转换成JPG,你就失去了PNG的“无损状态”。
无论如何,每个人都支持png。
+在您的情况下,您希望接收器将其更改回PNG以检索无损图像。这意味着接收器也支持PNG。在发送之前将其更改为JPG,然后在接收时将其更改回PNG,这有什么意义。只是一些额外的计算?
https://stackoverflow.com/questions/14604031
复制相似问题