mbnailator或imgscalr调整jpeg图像大小时的粉

mbnailator或imgscalr调整jpeg图像大小时的粉

本文介绍了使用java thumbnailator或imgscalr调整jpeg图像大小时的粉红色/偏红色调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用两个库(thumbnitor和imgscalr)转换图像(下面的url)。我的代码适用于大多数图像,除了转换后的一些图像具有粉红色/红色色调。



我想了解原因并欢迎任何推荐。



注意 - 此图片的图片类型是5即 BufferedImage.TYPE_3BYTE_BGR 我正在使用 Java 7





使用Thumbnailator



  Thumbnails.of(fromDir.listFiles( ))
.size(thumbnailWidth,thumbnailHeight)
.toFiles(Rename.SUFFIX_HYPHEN_THUMBNAIL);



使用imgscalr



  BufferedImage bufferedImage = ImageIO.read(file); 
final BufferedImage jpgImage;

LOG.debug(image type is = [{}],bufferedImage.getType());

BufferedImage scaledImg = Scalr.resize(bufferedImage,Method.ULTRA_QUALITY,thumbnailWidth,thumbnailHeight,Scalr.OP_ANTIALIAS);


文件thumbnailFile = new文件(fromDirPath +/+ getFileName(file.getName())+ THUMBNAIL_KEYWORD +。png);

ImageIO.write(scaledImg,getFileExtension(file.getName()),thumbnailFile);

bufferedImage.flush();
scaledImg.flush();


解决方案

我得到了很多这个问题(imgscalr的作者) - 问题是几乎总是您正在读/写不同的文件格式,并且ALPHA通道导致您的一个颜色通道(R / G / B)从生成的文件中被剔除。



例如,如果您读入的文件是ARGB(4通道)并将其写成JPG(3通道) - 除非您有意操纵图像类型你自己直接渲染旧图像,你会得到一个带有ARG频道的文件...或者更具体地说,只有红色和绿色 - 没有蓝色。



PNG支持alpha通道而JPG不支持,所以请注意这一点。



解决此问题的方法是有目的地创建正确类型的适当BufferedImage( RGB,ARGB等)并使用 destImage.getGraphics()调用将一个图像渲染到另一个图像,然后再将其写入磁盘并重新编码。 / p>

Sun和Oracle从未使ImageIO库足够智能,以便在写入不同的文件类型时检测不支持的通道,因此这种行为始终发生:(



希望有所帮助!


I am trying to convert an image (url below) using two libraries (thumbnailator and imgscalr. My code works on most of the images except a few which after conversion have a pink/reddish tint.

I am trying to understand the cause and would welcome any recommendation.

Note - Image type of this image is 5 i.e BufferedImage.TYPE_3BYTE_BGR and i am using Java 7

Using Thumbnailator

  Thumbnails.of(fromDir.listFiles())
                    .size(thumbnailWidth, thumbnailHeight)
                    .toFiles(Rename.SUFFIX_HYPHEN_THUMBNAIL);

Using imgscalr

    BufferedImage bufferedImage = ImageIO.read(file);
    final BufferedImage jpgImage;

    LOG.debug("image type is =[{}] ", bufferedImage.getType());

     BufferedImage scaledImg = Scalr.resize(bufferedImage, Method.ULTRA_QUALITY, thumbnailWidth, thumbnailHeight, Scalr.OP_ANTIALIAS);


    File thumbnailFile = new File(fromDirPath + "/" + getFileName(file.getName()) +THUMBNAIL_KEYWORD  + ".png");

    ImageIO.write(scaledImg, getFileExtension(file.getName()), thumbnailFile);

    bufferedImage.flush();
    scaledImg.flush();
解决方案

I get this question a lot (author of imgscalr) -- the problem is almost always that you are reading/writing out different file formats and the ALPHA channel is causing one of your color channels (R/G/B) to be culled from the resulting file.

For example, if you read in a file that was ARGB (4 channel) and wrote it out as a JPG (3 channel) - unless you purposefully manipulate the image types yourself and render the old image to the new one directly, you will get a file with a "ARG" channels... or more specifically, just Red and Green - no Blue.

PNG supports an alpha channel and JPG does not, so be aware of that.

The way to fix this is to purposefully create appropriate BufferedImage's of the right type (RGB, ARGB, etc.) and using the destImage.getGraphics() call to render one image to the other before writing it out to disk and re-encoding it.

Sun and Oracle have NEVER made the ImageIO libraries smart enough to detect the unsupported channels when writing to differing file types, so this behavior happens all the time :(

Hope that helps!

这篇关于使用java thumbnailator或imgscalr调整jpeg图像大小时的粉红色/偏红色调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 19:29