如何使用java将图像保存在磁盘上的文件夹中

时间:2012-06-05 12:21:52

标签: java image netbeans-7 javax.imageio

我想将图像保存在磁盘上,例如c:/ images,这是由网络摄像头使用java捕获的...再次我希望在JForm上将该图像显示为标签... 这可能使用java和netbeans 我是java的新手

4 个答案:

答案 0 :(得分:2)

你可以保存图片

private static void save(String fileName, String ext) {

   File file = new File(fileName + "." + ext);
   BufferedImage image = toBufferedImage(file);
try {
   ImageIO.write(image, ext, file);  // ignore returned boolean
} catch(IOException e) {
 System.out.println("Write error for " + file.getPath() +
                               ": " + e.getMessage());
  }
 }

从磁盘读取图像并显示为标签

File file = new File("image.gif");
    image = ImageIO.read(file);
JFrame frame = new JFrame();
JLabel label = new JLabel(new ImageIcon(image));
frame.getContentPane().add(label, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);

答案 1 :(得分:1)

您可以使用BufferedImage从硬盘加载图像:

BufferedImage img = null;
try {
    img = ImageIO.read(new File("strawberry.jpg"));
} catch (IOException e) {
}

请尝试此链接以获取更多信息。 Reading/Loading Images in Java

这个用于保存图像。 Writing/Saving an Image

try {
    // retrieve image
    BufferedImage bi = getMyImage();
    File outputfile = new File("saved.png");
    ImageIO.write(bi, "png", outputfile);
} catch (IOException e) {
    ...
}

答案 2 :(得分:0)

纯Java,不需要第三方库:

  byte[] image = /*your image*/
  String filePath = /*destination file path*/

  File file = new File(filePath); 
  
        try (FileOutputStream fosFor = new FileOutputStream(file)) {
            fosFor.write(image);
        }

答案 3 :(得分:-1)

    //Start Photo Upload with  No// 
    if (simpleLoanDto.getPic() != null && simpleLoanDto.getAdharNo() != null) {
        String ServerDirPath = globalVeriables.getAPath() + "\\";
        File ServerDir = new File(ServerDirPath);
        if (!ServerDir.exists()) {
            ServerDir.mkdirs();
        }
        // Giving File operation permission for LINUX//
        IOperation.setFileFolderPermission(ServerDirPath);
        MultipartFile originalPic = simpleLoanDto.getPic();
        byte[] ImageInByte = originalPic.getBytes();
        FileOutputStream fosFor = new FileOutputStream(
                new File(ServerDirPath + "\\" + simpleLoanDto.getAdharNo() + "_"+simpleLoanDto.getApplicantName()+"_.jpg"));
        fosFor.write(ImageInByte);
        fosFor.close();
    }
    //End  Photo Upload with  No//