【问题标题】:how to save the the image in folder on disk using java如何使用java将图像保存在磁盘上的文件夹中
【发布时间】:2012-06-05 12:21:52
【问题描述】:

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

【问题讨论】:

  • 听起来你想从磁盘加载图像。顺便说一句,您在这里使用的是哪个版本的 netbeans-7?
  • JForm DYM JFrame? J2SE 中没有JForm

标签: java image netbeans-7 javax.imageio


【解决方案1】:

你可以保存图片

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);

【讨论】:

    【解决方案2】:

    您可以使用 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) {
        ...
    }
    

    【讨论】:

      【解决方案3】:

      纯 Java,不需要第三方库:

        byte[] image = /*your image*/
        String filePath = /*destination file path*/
      
        File file = new File(filePath); 
        
              try (FileOutputStream fosFor = new FileOutputStream(file)) {
                  fosFor.write(image);
              }
      

      【讨论】:

        【解决方案4】:
            //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// 
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-10-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多