javafx image.write()do do do

发布于 2025-01-26 19:35:08 字数 1797 浏览 2 评论 0原文

我正在尝试创建一个应用程序,使用户可以修改图片,然后保存它们。我在保存部分遇到麻烦。

这是旋转图片的方法:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

命比纸薄 2025-02-02 19:35:08

imageio您调用的方法在下面指定了您忽略的布尔状态代码:

public static boolean write(RenderedImage im,
                            String formatName,
                            File output) throws IOException

您尚未包含文件的详细信息,因此无法确定您是否已通过有效formatname您是从输出文件名的最后三个字符中得出的。请注意,格式名称并不总是3个字符,应该是小写 - 因此“ file.jpg”和“ file.jpeg”都可能无法保存。

如果您相信文件扩展名是正确的,并且由您使用的bufferedimage.type_int_argb格式所使用,请尝试更改以使用小写格式,然后检查结果:

String format = dir.getAbsolutePath().substring(dir.getAbsolutePath().length() - 3).toLowerCase();
boolean ok = ImageIO.write(img, format, dir);
if (!ok)
    throw new RuntimeException("Failed to write "+format+" to " + dir.getAbsolutePath());

如果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:

public static boolean write(RenderedImage im,
                            String formatName,
                            File output) throws IOException

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 that formatName 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:

String format = dir.getAbsolutePath().substring(dir.getAbsolutePath().length() - 3).toLowerCase();
boolean ok = ImageIO.write(img, format, dir);
if (!ok)
    throw new RuntimeException("Failed to write "+format+" to " + dir.getAbsolutePath());

If the BufferedImage format isn't compatible with the file extension, try using new BufferedImage(... , BufferedImage.TYPE_INT_RGB) or a different format type (eg PNG).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文