简体   繁体   English

如何从位图数据创建BufferedImage

[英]How to create BufferedImage from bitmap data

After going through many similar looking questions I had no way but put my own question here. 在经历了许多相似的问题之后,我只能将自己的问题放在这里。

I need to display an image on swing application. 我需要在Swing应用程序上显示图像。 The source of image is bitmap data which is retrieved from MS SQL server. 图像源是从MS SQL Server检索的位图数据。 I have tried the following ways 我尝试了以下方法

TRY 1 - I have tried creating an ImageIcon from the bytes retrieved. 尝试1-我尝试ImageIcon检索到的字节创建ImageIcon No results. 没结果。

TRY 2 - Saved the bytes in a .png file and tried loading Using ImageIO . 尝试2-将字节保存在.png文件中,并尝试使用ImageIO加载。 This works fine on my local machine but fails on test server. 这在我的本地计算机上工作正常,但在测试服务器上失败。 Both are windows machines. 两者都是Windows机器。

TRY3 - On step 2 I tried saving in different formats than .png. TRY3-在第2步中,我尝试使用不同于.png的格式进行保存。 It does not work at all. 它根本不起作用。

Please let me know what am I missing? 请让我知道我在想什么?

NOTE : I have tried including jai jars into the Referenced Libraries also. 注意:我也尝试将jai jars包含到“参考库”中。

You should have stored a hint what format the data has in the database. 您应该已经存储了数据库中数据的格式提示。 If not, you can only hope that ImageIO can handle it. 如果没有,您只能希望ImageIO 可以处理它。

There is no need to write the data to files (which is a pitfall in itself, where would you write them? Think of restricted process privileges and disk quotas). 无需将数据写入文件(这本身就是一个陷阱,您将在哪里写入它们?请考虑受限制的进程特权和磁盘配额)。 Just create an InputStream that accesses the data directly (eg java.io.ByteArrayInputStream), that way you can have ImageIO load directly using the stream based methods. 只需创建一个InputStream即可直接访问数据(例如java.io.ByteArrayInputStream),这样就可以使用基于流的方法直接加载ImageIO。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM