我们有一些旧代码(Java 1.2)大量使用JAI_IO进行图像处理。我们现在是Java 1.6,因此我们不再需要它。首先,我还应该使用BufferedImage吗?还是有更好的课程?我基本上需要做的是:
我知道答案是“哈哈哈”,但是有一个类会将元文件转换为位图吗?
谢谢 - 戴夫
答案 0 :(得分:0)
我使用ImageIO
和javax.imageio
API来阅读图片并调整DPI / PPI(请参阅下面的示例),可能还有像imgscalr这样的包thumbnailator根据您的需求/偏好调整大小。
代码如下:
// input is typically a File or InputStream
// Wrap in ImageInputStream
ImageInputStream stream = ImageIO.createImageInputStream(input);
// Obtain reader
ImageReader reader = ImageIO.getImageReaders(stream).next(); // In real code, test for presence
reader.setInput(stream);
String formatName = reader.getFormatName(); // Get the format name
ImageReadParam param = reader.getDefaultReadParam();
// ... modify params as you see fit, or just go with defaults
IIOImage image = reader.readAll(0, param);
RenderedImage red = image.getRenderedImage();
BufferedImage buf = (BufferedImage) red; // In real code, test if it's safe, or convert
// .. pas buf along to imgscalr or thumnailator
image.setRenderedImage(buf);
IIOMetadata meta = image.getMetadata();
// ... modify DPI in meta data
ImageWriter writer = reader.getImageWriter();
writer.write(null, image, null); // Or modify write params if you need (last parameter)