旋转图像会导致损坏

发布于 2024-11-05 17:15:48 字数 760 浏览 4 评论 0原文

我正在尝试将图像左右旋转 90 度。

但由于某种原因,此过程的输出会导致腐败。

这是我的代码:
(它很酷,但不需要太多想象力就可以假装它的java)

void rotate(File file){
    def image = ImageIO.read(file);
    double theta  = Math.PI / 2;
    def w = image.width / 2;
    def h = image.height / 2;

    def transform = new AffineTransform();
    transform.rotate(theta, h, w);
    def op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
    image = op.filter(image, null);

    def name = file.getName();
    def type = name.substring(name.lastIndexOf(".") + 1, name.length());
    ImageIO.write(image,type,file);
}

原始: 在此处输入图像描述

旋转:在此处输入图像描述

I am trying to rotate and image left and right by 90 degrees.

For some reason though, the output of this process results in corruption.

Here is my code:
(its groovy but it doesnt take much imagination to pretend its java)

void rotate(File file){
    def image = ImageIO.read(file);
    double theta  = Math.PI / 2;
    def w = image.width / 2;
    def h = image.height / 2;

    def transform = new AffineTransform();
    transform.rotate(theta, h, w);
    def op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
    image = op.filter(image, null);

    def name = file.getName();
    def type = name.substring(name.lastIndexOf(".") + 1, name.length());
    ImageIO.write(image,type,file);
}

original:
enter image description here

rotated:enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

℉服软 2024-11-12 17:15:48

如果您所说的损坏指的是颜色变化,请取出过滤器。如果我正确理解语法的话,这会给你一个负面的印象。

每当我使用变换时,我都会关闭过滤器并手动完成它们。这确实需要花费很多时间,但它们总是变得更有用。只是一个建议。

If by corruption you are referring to the color change, take out the filter. That's giving you a negative image if I'm understanding the syntax properly.

Whenever I use transforms I leave filters off and do them by hand. It does take a lot of time, but they always turn out being more useful. Just a suggestion.

水中月 2024-11-12 17:15:48

filter() 方法需要 srcdst BufferedImage,必须不同。

Image image = null;
try {
    image = ImageIO.read(new File("gZtC3.jpg"));
} catch (IOException ex) {
    ex.printStackTrace(System.err);
}
double theta = Math.PI / 2;
int w = image.getWidth(null);
int h = image.getHeight(null);
AffineTransform at = AffineTransform.getRotateInstance(theta, w / 2, h / 2);
BufferedImage src = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = src.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BICUBIC);
BufferedImage dst = op.filter(src, null);
this.add(new JLabel(new ImageIcon(dst), JLabel.CENTER));

The filter() method requires a src and dst BufferedImage, which must be different.

Image image = null;
try {
    image = ImageIO.read(new File("gZtC3.jpg"));
} catch (IOException ex) {
    ex.printStackTrace(System.err);
}
double theta = Math.PI / 2;
int w = image.getWidth(null);
int h = image.getHeight(null);
AffineTransform at = AffineTransform.getRotateInstance(theta, w / 2, h / 2);
BufferedImage src = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = src.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BICUBIC);
BufferedImage dst = op.filter(src, null);
this.add(new JLabel(new ImageIcon(dst), JLabel.CENTER));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文