我无法使用Java JAI(Java高级成像)API将JPEG图像的分辨率从1024x800更改为512x400。
我玩过API并继续获取流或java.lang.OutOfMemory
例外。
任何有实际工作的人。
答案 0 :(得分:4)
这是一个使用JAI
public void resize(String filename, RenderedOp image, double wScale, double hScale)
{
// now resize the image
ParameterBlock pb = new ParameterBlock();
pb.addSource(image); // The source image
pb.add(wScale); // The xScale
pb.add(hScale); // The yScale
pb.add(0.0F); // The x translation
pb.add(0.0F); // The y translation
RenderingHints hints = new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
RenderedOp resizedImage = JAI.create("SubsampleAverage", pb, hints);
// lastly, write the newly-resized image to an
// output stream, in a specific encoding
try
{
FileOutputStream fos = new FileOutputStream(new File(filename));
JAI.create("encode", resizedImage, fos, getImageType(filename), null);
}
catch (FileNotFoundException e)
{
}
}
答案 1 :(得分:2)
这是一个工作示例,按“原样”提供,不做任何保证:)
BufferedImage scaleImage(BufferedImage sourceImage, int scaledWidth) {
float scale = scaledWidth / (float) sourceImage.getWidth();
int scaledHeight = (int) (sourceImage.getHeight() * scale);
Image scaledImage = sourceImage.getScaledInstance(
scaledWidth,
scaledHeight,
Image.SCALE_AREA_AVERAGING
);
BufferedImage bufferedImage = new BufferedImage(
scaledImage.getWidth(null),
scaledImage.getHeight(null),
BufferedImage.TYPE_INT_RGB
);
Graphics g = bufferedImage.createGraphics();
g.drawImage(scaledImage, 0, 0, null);
g.dispose();
return bufferedImage;
}