gpt4 book ai didi

java - 从另一个目录读取图像

转载 作者:行者123 更新时间:2023-12-01 23:00:14 24 4
gpt4 key购买 nike

我需要从另一个目录打开图像,但当我尝试输入整个路径名(如 /Users/Documents/image.dcm)时,以下代码不起作用。

我正在尝试打开 dicom 图像。我这样做是为了为这段代码制作 GUI,但我真的被困住了。我尝试了很多事情,但似乎没有什么真正有效。任何建议将不胜感激。

/**
* This class displays the first frame of a file from any format
* supported by ImageIO.
* In the case of DICOM files, stored values are displayed as
* is, without using Value of Interest or Presentation LUTs.
*/

class Read1 {
public static void main(String[] s) {
try {
//System.out.println(s[0]);
if (s.length != 1) {
//System.err.println("Please supply an input file");
System.exit(1);
}

//URL url = Main.class.getResource(s[0]);
ImageIO.scanForPlugins();
final BufferedImage bi = ImageIO.read(new File(s[0]));
if (bi == null) {
System.err.println("read error");
System.exit(1);
}

JFrame jf = new JFrame();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final Rectangle bounds = new Rectangle(0, 0, bi.getWidth(), bi.getHeight());
JPanel panel = new JPanel() {
public void paintComponent(Graphics g) {
Rectangle r = g.getClipBounds();
((Graphics2D) g).fill(r);
if (bounds.intersects(r))
g.drawImage(bi, 0, 0, null);
}
};
jf.getContentPane().add(panel);
panel.setPreferredSize(new Dimension(bi.getWidth(), bi.getHeight()));
jf.pack();
jf.setVisible(true);

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

最佳答案

你可以尝试任何一个

// Read from same package 
ImageIO.read(getClass().getResourceAsStream("c.png"));

// Read from absolute path
ImageIO.read(new File("E:/SOFTWARE/TrainPIS/res/drawable/c.png"));

// Read from images folder parallel to src in your project
ImageIO.read(new File("images/c.jpg"));

// Read from src/images folder
ImageIO.read(getClass().getResource("/images/c.png"))

// Read from src/images folder
ImageIO.read(getClass().getResourceAsStream("/images/c.png"))

关于java - 从另一个目录读取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23478984/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com