I'm trying to create an application that lets users modify pictures and then save them. I'm having trouble with the saving part.
This is the method that rotates the picture:
public void process(ImageView imageView) {
if(imageView.getImage() != null){
BufferedImage img;
img = Home.img;
double rads = Math.toRadians(90);
double sin = Math.abs(Math.sin(rads)), cos = Math.abs(Math.cos(rads));
int w = img.getWidth();
int h = img.getHeight();
int newWidth = (int) Math.floor(w * cos + h * sin);
int newHeight = (int) Math.floor(h * cos + w * sin);
BufferedImage rotated = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = rotated.createGraphics();
AffineTransform at = new AffineTransform();
at.translate((newWidth - w) / 2, (newHeight - h) / 2);
int x = w / 2;
int y = h / 2;
at.rotate(rads, x, y);
g2d.setTransform(at);
g2d.drawImage(img, 0, 0,null);
g2d.dispose();
imageView.setImage(convertToFxImage(rotated));
Home.img = rotated;
}
}
It sets the image in the Home
controller class's imageView and also sets a static field to the modified image. Then I try to save it inside the Home
class:
savAs.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
File dir = fileChooser.showSaveDialog(opButton.getScene().getWindow());
if (dir != null) {
try {
ImageIO.write(img, dir.getAbsolutePath().substring(dir.getAbsolutePath().length() - 3), dir);
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
This for some reason doesn't work. No IOException
is thrown, but it doesn't create any file. When I try to save without modifying the image it works. Any idea why?