javafx image.write()do do do
我正在尝试创建一个应用程序,使用户可以修改图片,然后保存它们。我在保存部分遇到麻烦。
这是旋转图片的方法:
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;
}
}
它在home
Controller类的ImageView中设置图像,并将静态字段设置为修改后的图像。然后,我尝试将其保存在home
类中:
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();
}
}
}
});
由于某种原因,这是行不通的。没有ioexception
被抛出,但不会创建任何文件。当我尝试保存而无需修改图像时,它起作用。知道为什么吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
imageio
您调用的方法在下面指定了您忽略的布尔状态代码:您尚未包含文件的详细信息,因此无法确定您是否已通过有效
formatname
您是从输出文件名的最后三个字符中得出的。请注意,格式名称
并不总是3个字符,应该是小写 - 因此“ file.jpg”和“ file.jpeg”都可能无法保存。如果您相信文件扩展名是正确的,并且由您使用的
bufferedimage.type_int_argb
格式所使用,请尝试更改以使用小写格式,然后检查结果:如果
bufferedimage
格式与文件扩展名不兼容,请尝试使用new BufferedImage(...,BufferedImage.type_int_rgb)
或其他格式类型(例如PNG)。The method in
ImageIO
you call is specified below and it returns a boolean status code that you ignore:You haven't included details of the File so there is no way of telling if you have passed in a valid
formatName
which you derive from the last three characters of the output filename. Note thatformatName
isn't always 3 characters and should be lowercase - so both "FILE.JPG" and "FILE.JPEG" may fail to save.If you believe the file extension is correct and is supported by the
BufferedImage.TYPE_INT_ARGB
format you've used, try change to use lowercase format, then check the result:If the
BufferedImage
format isn't compatible with the file extension, try usingnew BufferedImage(... , BufferedImage.TYPE_INT_RGB)
or a different format type (eg PNG).