ImageIO.write出了什么问题。它不会将图像写回或显示出来吗?

时间:2014-10-01 01:29:49

标签: java image-processing javax.imageio

此程序的目的是获得灰度图像的负片。当我跑步时,什么都没有出现,结果图像没有出现。调试后,除ImageIO.write函数外,一切正常。知道为什么吗?

import java.awt.BufferCapabilities;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;


public class emancv {

    public  static void main(String[] args){
    String filepath = "D:\\Eman Hamed\\Academics\\Fall 14\\Computer Vision\\flower.jpg";
        try {
                BufferedImage bi = ImageIO.read(new File(filepath));
                BufferedImage output = negative(bi);
                ImageIO.write(output, "jpg", new File("D:\\Eman Hamed\\Academics\\Fall 14\\Computer Vision\\flower2.jpg"));

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

    public static BufferedImage negative(BufferedImage bi){
        WritableRaster myImage = bi.copyData(null);
        WritableRaster myImage2 = bi.copyData(null);
        for(int i =0; i< myImage2.getHeight(); i++)
            for(int j =0; j<myImage2.getWidth(); j++)
            {
                int value = 255 - myImage.getSample(j, i, 0);
                myImage2.setSample(j, i, 0, value);
            }
         BufferedImage res= new BufferedImage(bi.getWidth(),bi.getHeight(),BufferedImage.TYPE_BYTE_GRAY);
        res.setData(myImage2);
        return res;
            }
        }

1 个答案:

答案 0 :(得分:0)

你犯了一个错误,替换了这一行

BufferedImage res= newBufferedImage(bi.getWidth(),bi.getHeight(),BufferedImage.TYPE_BYTE_GRAY);

这一个

BufferedImage res= new BufferedImage(bi.getWidth(),bi.getHeight(),BufferedImage.TYPE_BYTE_GRAY);

并且没关系