0

I am trying to resize the picture with file chooser. It seems everything is file, but I can't open it after adding in folder.

public void metodAddpath(String fullPath)  {

try {
                       File sourceFile = new File(fullPath);               
                     BufferedImage bufferedimage = ImageIO.read(sourceFile);
                     ByteArrayOutputStream os = new ByteArrayOutputStream();
                     ImageIO.write(bufferedimage, "jpg", os);
                      InputStream is = new ByteArrayInputStream(os.toByteArray());
        FileOutputStream fileOutputStream = new FileOutputStream(
                        sourceFile);

        int bufferSize;
        byte[] bufffer = new byte[512];

        while ((bufferSize = is.read(bufffer)) > 0) {
            fileOutputStream.write(bufffer, 0, bufferSize);
        }
        is.close();
        fileOutputStream.close();             

                    //scaleImage(bufferedimage, 220, 220);  

} catch(Exception e) {
    e.printStackTrace();
}

} After I push the button to save the image in folder.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

    Database base = new  Database();
   metodAddpath(jTextField1.getText());

    base.addPictureResource(jTextField1.getText());
}

But when I am trying to add it in folder, there is a mistake.

Dennis
  • 101
  • 2
  • 10
  • I'm just scratching my head over all the "buffers" and file I/O. Why not just use `ImageIO.write` to write the image directly to the file? – MadProgrammer Nov 29 '18 at 23:01
  • *"It says thumbnail is not available. What is that mean?"* - You tell us, because the message makes no sense based on the available context – MadProgrammer Nov 29 '18 at 23:02
  • I am a beginner in java. May you give me any example with ImageIO.write? I know it's hard to lead beginners, but I am trying my best to understand. – Dennis Nov 29 '18 at 23:04
  • You open and read from fullPath, then try to create a new FileOutputStream with the same name. But that (existing) file is still open. – FredK Nov 29 '18 at 23:06
  • @Dennis, for easy to use purpose you can use this image resizer library -> https://github.com/hkk595/Resizer – Jay Dangar Nov 30 '18 at 04:57

2 Answers2

1

I'm just going to come out and say it, none of this...

try {
    File sourceFile = new File(fullPath);               
    BufferedImage bufferedimage = ImageIO.read(sourceFile);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    ImageIO.write(bufferedimage, "jpg", os);
    InputStream is = new ByteArrayInputStream(os.toByteArray());
    FileOutputStream fileOutputStream = new FileOutputStream(
    sourceFile);

    int bufferSize;
    byte[] bufffer = new byte[512];

    while ((bufferSize = is.read(bufffer)) > 0) {
        fileOutputStream.write(bufffer, 0, bufferSize);
    }
    is.close();
    fileOutputStream.close();             

    //scaleImage(bufferedimage, 220, 220);  

} catch(Exception e) {
    e.printStackTrace();
}

makes sense.

You're reading the image, writing it to a ByteArrayOutputStream, piping that through a InputStream which you're then using to write the contents to another file via a FileOutputStream ... why?!

Something like...

File sourceFile = new File(fullPath);               
try {
    BufferedImage bufferedimage = ImageIO.read(sourceFile);

    //scaleImage(bufferedimage, 220, 220);  

    // Beware, this is overwriting the existing file
    try (FileOutputStream fileOutputStream = new FileOutputStream(sourceFile)) {
        ImageIO.write(bufferedimage, "jpg", fileOutputStream);
    }
} catch(Exception e) {
    e.printStackTrace();
}

would do the same job, is easier to read and probably more efficient...

I doubt this will answer you question, but it might reduce some of the confusion

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • But I am trying to resize the image. How can I do it? – Dennis Nov 29 '18 at 23:09
  • Scaling an image is not an unusual job and has not only been asked before, is well documented. However, you might consider having a look at [Java: maintaining aspect ratio of JPanel background image](https://stackoverflow.com/questions/11959758/java-maintaining-aspect-ratio-of-jpanel-background-image/11959928#11959928) and [Quality of Image after resize very low — Java](https://stackoverflow.com/questions/14115950/quality-of-image-after-resize-very-low-java/14116752#14116752) for better implementations – MadProgrammer Nov 29 '18 at 23:50
  • Thanks man, I will have a look at that example. I am appreciate your help. – Dennis Nov 29 '18 at 23:57
0

Finally, I found the way how to scale the image before saving in the folder. First I would like to add a listener for the button and get the image with file chooser.

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
   JFileChooser file = new JFileChooser();
            file.setCurrentDirectory(new File(System.getProperty("user.home")));

            FileNameExtensionFilter filter = new FileNameExtensionFilter("*.Images", "jpeg", "jpg", "png"); 
            file.addChoosableFileFilter(filter);
            int result = file.showSaveDialog(null);
            if(result ==JFileChooser.APPROVE_OPTION) {
                File selectedFile = file.getSelectedFile();
                //GET ABSOLUTE PATH OF PICTURES
                jTextField1.setText(selectedFile.getAbsolutePath());
                //addPicture.setText(selectedFile.getName()); 
                //GET NAME OF PICTURES
                //getPicName = selectedFile.getName();


            } else if(result == JFileChooser.CANCEL_OPTION) {
                System.out.println("File not found!");

            }
}

After I am adding a listener for another button that is responsible for adding a picture to the folder. Here is my code:

  private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        try{
   addPicture(jTextField1.getText());
          }catch(Exception e) {
    e.printStackTrace();
}

    } 

And finally, let's add two functions:

    public void addPicture(String fullPath) throws IOException {
     File sourceFile = new File(fullPath);               
try {
    BufferedImage bufferedimage = ImageIO.read(sourceFile);
// add method scaleImage(bufferedimage, 220, 220) in ImageIO.write(scaleImage(bufferedimage, 220, 220), "jpg", fileOutputStream)  
    try (FileOutputStream fileOutputStream = new FileOutputStream("/my files/NetBeans IDE 8.2/NewDataBase/src/newdatabase/images/" + sourceFile.getName())) {
        ImageIO.write(scaleImage(bufferedimage, 220, 220), "jpg", fileOutputStream);
    }

} catch(Exception e) {
    e.printStackTrace();
}

Add don't forget about the important method

public BufferedImage scaleImage(BufferedImage img, int width, int height) {
int imgWidth = img.getWidth();
int imgHeight = img.getHeight();
if (imgWidth*height < imgHeight*width) {
    width = imgWidth*height/imgHeight;
} else {
    height = imgHeight*width/imgWidth;
}

BufferedImage newImage = new BufferedImage(width, height,
        BufferedImage.TYPE_INT_RGB);
Graphics2D g = newImage.createGraphics();
try {
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
            RenderingHints.VALUE_INTERPOLATION_BICUBIC);

    g.clearRect(0, 0, width, height);
    g.drawImage(img, 0, 0, width, height, null);
} 

finally {
    g.dispose();
}
return newImage;
}  




}

Thanks averyone for help. I would like to say thanks to MadProgrammer. You're a genius, man.

Dennis
  • 101
  • 2
  • 10