thumbnailator
thumbnailator copied to clipboard
color loss(turn red) when resizing some specific images
I use the following code to resize the jpg file: Thumbnails.of(new File("src/main/resources/images").listFiles()) .size(640, 480) .outputFormat("jpg") .toFiles(Rename.PREFIX_DOT_THUMBNAIL);
Images which have problem: original image resized image
I did the following to remove Alpha channel on PNG files:
// REMOVE Alpha channel. Source: http://stackoverflow.com/a/4388542
Image img = Toolkit.getDefaultToolkit().createImage(fileBytes);
PixelGrabber pg = new PixelGrabber(img, 0, 0, -1, -1, true);
pg.grabPixels();
int w = pg.getWidth();
int h = pg.getHeight();
DataBuffer buffer = new DataBufferInt((int[]) pg.getPixels(), pg.getWidth() * pg.getHeight());
WritableRaster raster = Raster.createPackedRaster(buffer, w, h, w, RGB_MASKS, null);
BufferedImage bi = new BufferedImage(RGB_OPAQUE, raster, false, null);
// Resize
BufferedImage bufferedImage1 = Thumbnails
.of(bi)
.width(thumbnailWidth)
.height(thumbnailHeight)
.keepAspectRatio(true)
.outputQuality(0.8)
.outputFormat(thumbnailType)
.asBufferedImage();
@zdjray did you find any solution?
`File in = new File("c:\img\in.jpg"); // 原图片 FileInputStream inStream = new FileInputStream(in); ImageWrapper imageWrapper = ImageReadHelper.read(inStream);//alibaba simpleimage lib Thumbnails.of(imageWrapper.getAsBufferedImage()).size(160, 160) .toFile("c:\img\out.jpg");
`you can use this code