Thumbnailator图像旋转会使图像混乱

时间:2016-12-21 08:07:12

标签: image rotation javax.imageio

我正在尝试使用Thumbnailator库旋转图像。我使用的代码如下所示。它可以旋转图像或成功翻转图像,但颜色质量完全破坏。输入和输出图像也会显示出来。

package com.abk;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.filters.Flip;
import net.coobird.thumbnailator.filters.Rotation;
import net.coobird.thumbnailator.util.exif.ExifUtils;
import net.coobird.thumbnailator.util.exif.Orientation;

public class ImageAutoRotate {
    public static void main(String[] args) {
        try {
            BufferedImage img = ImageIO.read(new File("314.jpg"));
            BufferedImage newImg = Rotation.RIGHT_90_DEGREES.apply(img);
            BufferedImage flipImg = Flip.HORIZONTAL.apply(img);

            File outputfile = new File("314_2.jpg");
            ImageIO.write(newImg, "jpg", outputfile);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

输入图像 enter image description here 输出图像 Output Image

2 个答案:

答案 0 :(得分:1)

通过将图像另存为PNG图像解决了该问题。问题实际上是因为图像配置文件在保存时被视为CMYK

link

显示了解决此问题的最终代码

答案 1 :(得分:0)

由于ImageIO.write类,存在同样的问题。以下是我使用的方法。

//Here imagebuffer is an array of bytes you can convert it from bufferedimage

InputStream fiStream = new ByteArrayInputStream(imageBuffer);

//Creating a file using byte array

FileUtils.writeByteArrayToFile(new File(path + "/webapps/northstar- 
primefaces-portlet/images/member/" + event.getFile().getFileName()), imageBuffer);

//Creating an output stream using the created file

OutputStream out = new FileOutputStream(path + "/webapps/northstar-primefaces-portlet/images/member/"  + event.getFile().getFileName());
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Thumbnails.of(fiStream).scale(1).toOutputStream(outputStream);

//Now the outstream has the rotated image with the correct colours

让我知道这是否有帮助。

参考: https://github.com/coobird/thumbnailator/issues/23