0

Ok so I've been trying do read an image and resize it to a specified dimension and save it into a package directory, everything goes well but the image still keeps the same dimensions as the external file, and don't understand why this is my method.

    public boolean guardarImagen() {
    boolean imgcreated = false;
    MUbicaciones ub = (MUbicaciones) this.getObject();
    File imagenFile = ub.getImagen();

    //create img  dimensions (supposed to anyways)
    BufferedImage bufim = new BufferedImage(300, 300,BufferedImage.TYPE_INT_RGB);
    // asignar la imagen al bufim.
    try {
        bufim = ImageIO.read(imagenFile);
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    // obtener las extensiones de los archivos.
    String ff = imagenFile.getName();
    String formatName = ff.replaceAll("^[\\w]+(\\s)?[^.]", "");
    String fileExtension = ff.replaceAll("^[\\w]+.[^A-Za-z]", "");
    // nuevo archivo que contiene la ubicacion donde se va a guardar la
    // imagen.
    newImageFile = new File(this.getDirImagen() + this.getNombreArchivo()
            + formatName);      
    try {
        // escribe la copia de la imagen en nuevo directorio.
        ImageIO.write(bufim, fileExtension, newImageFile);
        imgcreated = true;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return imgcreated;
}// guardar imagen
shep
  • 513
  • 2
  • 5
  • 17
  • 1
    Where are you actually resizing the image?? – MadProgrammer Nov 06 '14 at 05:47
  • 1
    You might consider having a look at [this example](http://stackoverflow.com/questions/11959758/java-maintaining-aspect-ratio-of-jpanel-background-image/11959928#11959928) and [this example](http://stackoverflow.com/questions/14115950/quality-of-image-after-resize-very-low-java/14116752#14116752) – MadProgrammer Nov 06 '14 at 05:49
  • @MadProgrammer, hello, so I thought the `BufferedImage` took the new `width,height` so looking at examples would this do the trick: `bufim.getScaledInstance(400, 400, Image.SCALE_SMOOTH);` ? – shep Nov 06 '14 at 05:57
  • 1
    1- You actually create two `BufferedImage`s, one via `new BufferedImage` and one by `ImageIO`. 2- Yes, `getScaledInstance` can work, but you'd need to paint the result to another `BufferedImage` of the same size (`getScaledInstance` doesn't affect the original, it simply creates a new instance at the desired size) and `ImageIO` won't write `Image` types (it will want a `BufferedImage`... – MadProgrammer Nov 06 '14 at 06:01

0 Answers0