我有一个java应用程序允许用户从他的相机拍摄照片并使用网络服务将其发送给我,但我的问题是在发送图像时。发送进度需要很长时间,因为图像很大,所以我想压缩图像。我尝试过:
1-使用此代码:
Bitmap img = BitmapFactory.decodeFile("C:\\test.jpg");
ByteArrayOutputStream streem = new ByteArrayOutputStream();
img.compress(Bitmap.CompressFormat.JPEG, 75, streem);
byte[] b = streem.toByteArray();
但是这段代码对我来说没有用,因为它使图像非常糟糕并且对图像大小影响不大。
2- 搜索了很多有关调整大小的方法,但所有结果都使用 BufferedImage。我不能使用这种类型(Class),因为它占用大量内存:
private static BufferedImage resizeImage(BufferedImage originalImage, int type)
{
BufferedImage resizedImage = new BufferedImage(new_w, new_h, type);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, new_w, new_h, null);
g.dispose();
return resizedImage;
}
我想使用位图,任何人都可以在我的应用程序中帮助我???
最佳答案
使用ImageIO
将 BufferedImage
写入您需要的格式
您可以向 ImageIO
提供输出流,这样您就应该能够写入到几乎任何地方。
查看Writing/Saving an Image了解更多详情
关于java - 在java中调整图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13540767/