66117

how to save the the image in folder on disk using java

Question:

I want to save the image on disk such as c:/images which is captured by webcam using java ..and again I want to display that image on JForm as a label... is this possible using java and netbeans I'm new in java

Answer1:

you can save image

private static void save(BufferedImage image, 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()); } }

and read image from disk and show into label as

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

Answer2:

You can use BufferedImage to load an image from your hard disk :

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

Try this link for further information. <a href="http://docs.oracle.com/javase/tutorial/2d/images/loadimage.html" rel="nofollow">Reading/Loading Images in Java</a>

And this one for saving the image. <a href="http://docs.oracle.com/javase/tutorial/2d/images/saveimage.html" rel="nofollow">Writing/Saving an Image</a>

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

Answer3:

//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//

Recommend

  • PHP use header() for a specific page
  • android downloading multiple files with InputStream FileOutputStream
  • java.nio.file.FileSystemNotFoundException when getting file from resources folder
  • Help refreshing list with notifyDataSetChanged()
  • Android: save file to downloads that can be viewed later
  • Turning String textfile into object array
  • Spring bean validation messages resolution
  • Put database into android project
  • 3.0.0.M1: SSL - Invalid keystore format
  • PDO error when wrong host name
  • How to copy styled text in JTextPane
  • DIV instruction jumping to random location?
  • Creating Java object from class name with constructor, which contains parameters [duplicate]
  • how to avoid repetitive constructor in children
  • OOP Javascript - Is “get property” method necessary?
  • Transactional Create with Validation in ServiceStack Redis Client
  • How to match http request and response using Jersey ContainerRequestFilter and ContainerResponseFilt
  • Is there a way to do normal logging with EureakLog?
  • How to test if a URL from an Eclipse bundle is a directory?
  • How to set ini file attributes during an Inno install
  • Setting up SourceTree to merge unity3d scenes with UnityYAMLMerge
  • Reading JSON from a file using C++ REST SDK (Casablanca)
  • Why value captured by reference in lambda is broken? [duplicate]
  • Email format validation in mvc3 view
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • PHP - How to update data to MySQL when click a radio button
  • How to add date and time under each post in guestbook in google app engine
  • javascript inside java/jsp code
  • SQL merge duplicate rows and join values that are different
  • KeystoneJS: Relationships in Admin UI not updating
  • AngularJs get employee from factory
  • Load html files in TinyMce
  • Hits per day in Google Big Query
  • coudnt use logback because of log4j
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • LevelDB C iterator
  • Linking SubReports Without LinkChild/LinkMaster
  • Bitwise OR returns boolean when one of operands is nil
  • Can't mass-assign protected attributes when import data from csv file
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass