如何从 javafx imageView 获取 byte[]?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24038524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 09:50:50  来源:igfitidea点击:

How to get byte[] from javafx imageView?

javaimagejavafxbyteprocessing

提问by Tomas Bisciak

How do i get byte[] from javafx image/imageview class? I want to store my image as a Blob into my database.This is the method that i use for it

我如何从 javafx 图像/图像视图类获取字节 []?我想将我的图像作为 Blob 存储到我的数据库中。这是我使用的方法

 public PreparedStatement prepareQuery(HSQLDBConnector connector) {
        try {
            Blob logoBlob = connector.connection.createBlob();
            logoBlob.setBytes(0,logo.getImage());//stuck  here
            for (int i = 0, a = 1; i < data.length; i++, a++) {

                connector.prepStatCreateProfile.setString(a, data[i]);

            }
            //store LOB

            connector.prepStatCreateProfile.setBlob(11, logoBlob);

        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return connector.prepStatCreateProfile;
    }

Is there a way to convert from my current object (imageview),image) into byte[]?, or shoud i start to think about using other class for my image/ alternatively point to the location with reference and work with paths/urls?

有没有办法从我当前的对象(图像视图),图像)转换为字节[]?,或者我应该开始考虑为我的图像使用其他类/或者指向带有参考的位置并使用路径/url?

采纳答案by Lorenzo Boccaccia

try this one:

试试这个:

BufferedImage bImage = SwingFXUtils.fromFXImage(logo.getImage(), null);
ByteArrayOutputStream s = new ByteArrayOutputStream();
ImageIO.write(bImage, "png", s);
byte[] res  = s.toByteArray();
s.close(); //especially if you are using a different output stream.

should work depending on the logo class

应该根据徽标类工作

you need to specify a format while writing and reading, and as far as I remember bmp is not supported so you will end up with a png byte array on the database

您需要在写入和读取时指定格式,据我所知不支持 bmp,因此您最终会在数据库中得到一个 png 字节数组

回答by Lorenzo Boccaccia

pure java fx solution trace( == you will have to fill in missing points :)

纯 Java 外汇解决方案跟踪(== 你将不得不填写缺失的点:)

Image i = logo.getImage();
PixelReader pr = i.getPixelReader();
PixelFormat f = pr.getPixelFormat();

WriteablePixelFromat wf = f.getIntArgbInstance(); //???

int[] buffer = new int[size as desumed from the format f, should be  i.width*i.height*4];

pr.getPixels(int 0, int 0, int i.width, i.height, wf, buffer, 0, 0);

回答by jewelsea

Lorenzo's answer is correct, this answer just examines efficiency and portability aspects.

Lorenzo 的答案是正确的,这个答案只是考察了效率和便携性方面。

Depending on the image type and storage requirements, it may be efficient to convert the image to a compressed format for storage, for example:

根据图像类型和存储要求,将图像转换为压缩格式进行存储可能是有效的,例如:

ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
ImageIO.write(SwingFXUtils.fromFXImage(fxImage, null), "png", byteOutput);
Blob logoBlob = connector.connection.createBlob();
logoBlob.setBytes(0, byteOutput.toByteArray());

Another advantage of doing a conversion to a common format like png before persisting the image is that other programs which deal with the database would be able to read the image without trying to convert it from a JavaFX specific byte array storage format.

在持久化图像之前转换为通用格式(如 png)的另一个优点是,处理数据库的其他程序将能够读取图像,而无需尝试将其从 JavaFX 特定的字节数组存储格式进行转换。