将javafx.scene.image.Image写入文件?

将javafx.scene.image.Image写入文件?,第1张

将javafx.scene.image.Image写入文件?

差不多3年后,我现在有知识去做并回答这个问题。是的,原始答案也是有效的,但它涉及到先将图像转换为BufferedImage,我理想上想完全避免摆动。虽然这确实会输出图像的原始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();}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5439009.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存