在Android上,如何以30%的质量将图像文件保存为JPEG格式?
在标准Java语言中,我将使用ImageIO将图像读取为BufferedImage,然后使用IIOImage实例http://www.universalwebservices.net/web-programming-resources/java/adjust-jpeg-image-compression-quality-when-saving-images-in-java将其保存为JPEG文件。然而,看起来安卓缺少javax.imageio包。
发布于 2011-01-03 02:10:07
通过调用compress并设置第二个参数,可以将位图存储为JPEG格式:
Bitmap bm2 = createBitmap();
OutputStream stream = new FileOutputStream("/sdcard/test.jpg");
/* Write bitmap to file using JPEG and 80% quality hint for JPEG. */
bm2.compress(CompressFormat.JPEG, 80, stream);
发布于 2011-01-03 04:31:56
InputStream in = new FileInputStream(file);
try {
Bitmap bitmap = BitmapFactory.decodeStream(in);
File tmpFile = //...;
try {
OutputStream out = new FileOutputStream(tmpFile);
try {
if (bitmap.compress(CompressFormat.JPEG, 30, out)) {
{ File tmp = file; file = tmpFile; tmpFile = tmp; }
tmpFile.delete();
} else {
throw new Exception("Failed to save the image as a JPEG");
}
} finally {
out.close();
}
} catch (Throwable t) {
tmpFile.delete();
throw t;
}
} finally {
in.close();
}发布于 2016-07-19 04:57:56
@Phyrum Tea是好的,只是别忘了关闭一切
InputStream in = new FileInputStream(context.getFilesDir() + "image.jpg");
Bitmap bm2 = BitmapFactory.decodeStream(in);
OutputStream stream = new FileOutputStream(String.valueOf(
context.getFilesDir() + pathImage + "/" + idPicture + ".jpg"));
bm2.compress(Bitmap.CompressFormat.JPEG, 50, stream);
stream.close();
in.close();https://stackoverflow.com/questions/4579647
复制相似问题