1

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

4

4 回答 4

3

你可以保存图像

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);
于 2012-06-05T12:33:23.137 回答
1

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

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

尝试此链接以获取更多信息。在 Java 中读取/加载图像

而这个用于保存图像。写入/保存图像

try {
    // retrieve image
    BufferedImage bi = getMyImage();
    File outputfile = new File("saved.png");
    ImageIO.write(bi, "png", outputfile);
} catch (IOException e) {
    ...
}
于 2012-06-05T12:28:51.667 回答
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);
        }
于 2020-09-08T18:17:28.450 回答
-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// 
于 2017-06-22T19:21:21.100 回答