本文介绍了JAI更改JPEG分辨率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Java JAI(Java高级映像)API很难将JPEG图像的分辨率从例如1024x800更改为512x400.

I am having difficulty using the Java JAI (Java Advance Imaging) API to change the resolution of a JPEG image from lets say 1024x800 to 512x400.

我一直在使用该API,并不断收到消息流或java.lang.OutOfMemory异常.

I have played around with the API and keep getting stream or java.lang.OutOfMemory exceptions.

有一个可行的例子的人.

Anyone with a working example.

推荐答案

这是一个可行的示例,按原样"提供,不提供任何担保:)

Here's a working example, supplied on an "as is" basis with no warranty :)

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;
}

这篇关于JAI更改JPEG分辨率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 17:17