从另一个目录中读取图像 [英] Read image from another directory
本文介绍了从另一个目录中读取图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从另一个目录打开一个图像,但是当我尝试输入整个路径名时,以下代码不起作用,如 /Users/Documents/image.dcm
。
I need to open a image from another directory but the following code doesn't work when I try to put in the whole path name like /Users/Documents/image.dcm
.
我正在尝试打开 dicom
图片。我这样做,所以我可以为这个代码制作GUI,但我真的卡住了。我尝试过很多东西,但似乎没什么用。任何建议将不胜感激。
I am trying to open a dicom
image. I am doing this so I can make GUI for this code but I am really stuck. I have tried many things but nothing really seems to work. Any suggestions will be appreciated.
/**
* 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"))
这篇关于从另一个目录中读取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文