Thumbnailator 图像旋转弄乱了图像
Thumbnailator Image Rotation messes the image
我正在尝试使用 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();
}
}
}
输入图片
输出图像
通过将图像另存为 PNG 图像解决了该问题。这个问题实际上是因为图像配置文件在保存时被视为 CMYK
解决此问题的最终代码显示在此 link
由于 ImageIO.write
class 出现了同样的问题。以下是我使用的方法。
//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
如果有帮助请告诉我。
我正在尝试使用 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();
}
}
}
输入图片
通过将图像另存为 PNG 图像解决了该问题。这个问题实际上是因为图像配置文件在保存时被视为 CMYK
解决此问题的最终代码显示在此 link
由于 ImageIO.write
class 出现了同样的问题。以下是我使用的方法。
//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
如果有帮助请告诉我。