java中的imgdir,使用java thumbnailator或imgscalr调整jpeg图像大小时的粉红色/偏红色调...

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

Tt6WQ.jpg

HtMGz.jpg

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!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值