图像转换会产生红色图像吗? [英] Image transformation results in a red image?

查看:130
本文介绍了图像转换会产生红色图像吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过水平翻转并调整图像大小来变换图像。问题是,当转换完成时,图片的颜色都很奇怪,它已经变成了红色调。是否有可能以某种方式解决这个问题,我想我在某处看到它可能是AWT库中的一些错误,但我不确定?

I am trying to transform an image by flipping it horizontally and resizing it. The problem is that when the transformation is done the picture's colors are all weird, it has gotten this reddish tone. Is it possible to fix this somehow, I think I read somewhere that it might be some bug in the AWT library but I am not sure?

以下是代码:

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;
    }   
}


推荐答案

拥有图像形成色调通常意味着使用错误的色彩空间渲染图像,Adobe RGB与sRGB是常年的最爱。尝试在代码中将TYPE_INT_ARGB更改为TYPE_INT_RGB。

Having an image develop a tint usually means the image is being rendered using the wrong colorspace, Adobe RGB vs. sRGB being a perennial favorite. Try changing TYPE_INT_ARGB to TYPE_INT_RGB in your code.

这篇关于图像转换会产生红色图像吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆