从文件中读取图像并使用java显示它

时间:2015-01-06 01:58:15

标签: java

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

public class JavaImageIOTest
{

  public JavaImageIOTest()
  {
    try
    {
      // the line that reads the image file
      BufferedImage image = ImageIO.read(new File("C:\ Users \ Public \ Pictures\    Sample Pictures\ Penguins.jpg"));
      // work with the image here ...
    } 
    catch (IOException e)
    {
        // log the exception
      // re-throw if desired
    }  
  }

  public static void main(String[] args)
 {
    new ImageIOTest();
  }

}

为此我获取路径规范错误.....所以你能告诉我读取图像并显示它的确切代码

1 个答案:

答案 0 :(得分:1)

您需要在文件规范中使用双斜杠

BufferedImage image = ImageIO.read(new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\Penguins.jpg"));