【问题标题】:What's wrong with ImageIO.write. It doesn't write the image back or show it?ImageIO.write 有什么问题。它不写回图像或显示它?
【发布时间】:2014-10-01 01:29:49
【问题描述】:

这个程序的目的是获取灰度图像的底片。当我运行时,什么都没有出现,结果图像也没有出现。调试后,除 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;
            }
        }

【问题讨论】:

  • ImageIO.write() 返回一个 boolean 指示图像是否已写入文件。弄清楚为什么在某些情况下没有写入图像并不是很有用,但至少它是一个起点。在你的情况下它会返回什么?如果false,请检查明显的内容,例如;路径是否正确?你有写权限吗?等等……
  • 它会创建一个空文件吗?我遇到了一个问题,它创建了一个我告诉它的文件,但该文件没有一个字节的数据。

标签: java image-processing javax.imageio


【解决方案1】:

你写错了,替换这一行

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

这个

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

会好的

【讨论】:

  • 我复制/粘贴您的代码,一切正常,同时我更改文件路径...确保可以访问“D:\Eman Hamed\Academics\Fall 14\Computer Vision\flower.jpg”和“D:\Eman Hamed\Academics\Fall 14\Computer Vision\flower2.jpg”
猜你喜欢
  • 2015-03-08
  • 2023-02-07
  • 2022-08-16
  • 1970-01-01
  • 1970-01-01
  • 2013-11-25
  • 2012-01-28
  • 1970-01-01
  • 2016-10-22
相关资源
最近更新 更多