Tôi đang cố chuyển đổi hình ảnh bằng cách lật hình ảnh theo chiều ngang và thay đổi kích thước hình ảnh đó. Vấn đề là khi việc chuyển đổi được thực hiện, màu sắc của hình ảnh đều lạ, nó đã nhận được tông màu đỏ. Có thể sửa lỗi này bằng cách nào đó, tôi nghĩ rằng tôi đọc ở đâu đó rằng nó có thể là một số lỗi trong thư viện AWT nhưng tôi không chắc chắn?Kết quả chuyển đổi hình ảnh thành hình ảnh màu đỏ?
Đây là mã:
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class LocalImageSizeFlip {
public static void main(String[] args) {
BufferedImage img = null;
try {
img = ImageIO.read(new File("C:\\picture.jpg"));
AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
tx.translate(0, -img.getHeight(null));
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
img = op.filter(img, null);
img = resize(img, 100, 75);
File newFile = new File("newPicture.jpg");
ImageIO.write(img, "JPEG", newFile);
} catch (IOException e) {
e.printStackTrace();
}
}
private static BufferedImage resize(BufferedImage image, int width, int height) {
BufferedImage resizedImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(image, 0, 0, width, height, null);
g.dispose();
return resizedImage;
}
}
Xem thêm [BufferedImage thay đổi color] (http://stackoverflow.com/questions/6122964/bufferedimage-colour-change) – trashgod