如何在Java中使用非标准采样因子缩放JPEG图像?
我正在使用JavaAWT缩放JPEG图像,以创建缩略图。当图像具有正常采样因子(2x2,1x1,1x1)时,代码工作正常 但是,具有此采样因子(1x1、1x1、1x1)的图像在缩放时会产生问题。虽然特征可以识别,但颜色会被破坏 屏幕和缩略图: 我使用的代码大致相当于:如何在Java中使用非标准采样因子缩放JPEG图像?,java,image-processing,jpeg,awt,image-scaling,Java,Image Processing,Jpeg,Awt,Image Scaling,我正在使用JavaAWT缩放JPEG图像,以创建缩略图。当图像具有正常采样因子(2x2,1x1,1x1)时,代码工作正常 但是,具有此采样因子(1x1、1x1、1x1)的图像在缩放时会产生问题。虽然特征可以识别,但颜色会被破坏 屏幕和缩略图: 我使用的代码大致相当于: static BufferedImage awtScaleImage(BufferedImage image, int maxSize, int hint)
static BufferedImage awtScaleImage(BufferedImage image,
int maxSize, int hint) {
// We use AWT Image scaling because it has far superior quality
// compared to JAI scaling. It also performs better (speed)!
System.out.println("AWT Scaling image to: " + maxSize);
int w = image.getWidth();
int h = image.getHeight();
float scaleFactor = 1.0f;
if (w > h)
scaleFactor = ((float) maxSize / (float) w);
else
scaleFactor = ((float) maxSize / (float) h);
w = (int)(w * scaleFactor);
h = (int)(h * scaleFactor);
// since this code can run both headless and in a graphics context
// we will just create a standard rgb image here and take the
// performance hit in a non-compatible image format if any
Image i = image.getScaledInstance(w, h, hint);
image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.drawImage(i, null, null);
g.dispose();
i.flush();
return image;
}
(代码由提供)
有更好的方法吗
这里是一个采样因子为[1x1,1x1,1x1]的示例。我认为问题不在于缩放,而是在构建BuffereImage时使用了不兼容的颜色模型(“图像类型”)
用Java创建像样的缩略图异常困难。这里有一个。我已经阅读了这个讨论,并且正在使用其中一些技术来加快扩展速度。但似乎与我的问题无关。但是,在图像加载阶段,不兼容的颜色模型是一个可能的嫌疑。我使用
javax.imageio.imageio.read
将图像加载到内存中。也许它不支持异常采样因子。我在使用ImageIO
将半透明图像编码为JPEG时见过这种效果,但我认为这不适用于您的示例,因为您的输出图像是不透明的(TYPE\u INT\u RGB
),此代码示例是否已完成,或者是否对图像应用了其他后处理?可能会无意中创建半透明图像(例如,AffineTransformOp
withTYPE_BILINEAR
将添加一个alpha通道,以防产生的图像边缘不位于精确的像素边界上。)