簡體   English   中英

從另一個目錄讀取圖像

[英]Read image from another directory

我需要從另一個目錄打開圖像,但是當我嘗試輸入整個路徑名(如/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"))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM