Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否将javafx.scene.image.image写入文件?_Java_Image_File Io_Javafx - Fatal编程技术网

是否将javafx.scene.image.image写入文件?

是否将javafx.scene.image.image写入文件?,java,image,file-io,javafx,Java,Image,File Io,Javafx,如何将javafx.scene.image.image图像写入文件。我知道你可以在BuffereImage上使用ImageIO,但是有没有办法在javafx映像上使用它呢?先用javafx.embed.swing.SwingFXUtils将它转换成BuffereImage: Image image = ... ; // javafx.scene.image.Image String format = ... ; File file = ... ; ImageIO.write(SwingFXUti

如何将javafx.scene.image.image图像写入文件。我知道你可以在BuffereImage上使用ImageIO,但是有没有办法在javafx映像上使用它呢?

先用
javafx.embed.swing.SwingFXUtils将它转换成
BuffereImage

Image image = ... ; // javafx.scene.image.Image
String format = ... ;
File file = ... ;
ImageIO.write(SwingFXUtils.fromFXImage(image, null), format, file);

差不多三年后,我现在有了知识去做和回答这个问题。是的,最初的答案也是有效的,但它首先需要将图像转换为BuffereImage,理想情况下,我希望完全避免swing。虽然这确实输出了原始RGBA版本的图像,这足以满足我的需要。实际上,我可以使用原始BGRA,因为我正在编写软件来打开结果,但由于gimp无法打开,我想我会将其转换为RGBA

Image img = new Image("file:test.png");
int width = (int) img.getWidth();
int height = (int) img.getHeight();
PixelReader reader = img.getPixelReader();
byte[] buffer = new byte[width * height * 4];
WritablePixelFormat<ByteBuffer> format = PixelFormat.getByteBgraInstance();
reader.getPixels(0, 0, width, height, format, buffer, 0, width * 4);
try {
    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("test.data"));
    for(int count = 0; count < buffer.length; count += 4) {
        out.write(buffer[count + 2]);
        out.write(buffer[count + 1]);
        out.write(buffer[count]);
        out.write(buffer[count + 3]);
    }
    out.flush();
    out.close();
} catch(IOException e) {
    e.printStackTrace();
}
Image img=新图像(“文件:test.png”);
int width=(int)img.getWidth();
int height=(int)img.getHeight();
PixelReader=img.getPixelReader();
字节[]缓冲区=新字节[宽度*高度*4];
WritablePixelFormat=PixelFormat.getByteBgraInstance();
getPixels(0,0,宽度,高度,格式,缓冲区,0,宽度*4);
试一试{
BufferedOutputStream out=新的BufferedOutputStream(新文件输出流(“test.data”));
对于(int count=0;count
@Cypher每个颜色值为0-255(1字节),每个像素有4个颜色值:蓝色、绿色、红色和alpha(透明)这对每种图像格式都有效吗?如果图像没有jpg那样的透明度怎么办?@JFValdes我没有用JPEG尝试过,但是我猜测,由于像素格式是使用
PixelFormat.getByteBgraInstance()
获取的,因此它将始终具有alpha通道。我的假设是,在JPEG的情况下,所有alpha值都将是0xFF。我可以在有时间的时候测试它,并在假设您还没有测试的情况下返回给您。@JFValdes是的,它可以与jpeg一起正常工作。正如我所怀疑的,alpha值最终都是0xFF。