0

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. The source of image is bitmap data which is retrieved from MS SQL server. I have tried the following ways

TRY 1 - I have tried creating an ImageIcon from the bytes retrieved. No results.

TRY 2 - Saved the bytes in a .png file and tried loading Using ImageIO. This works fine on my local machine but fails on test server. Both are windows machines.

TRY3 - On step 2 I tried saving in different formats than .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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Harihar Das
  • 484
  • 3
  • 11
  • I solved the problem with help from Durandal. I had to create the image directly using JAI since I do not have privilege to install JAI on the target system. 'PlanarImage planar = JAI.create("stream", SeekableStream.wrapInputStream(inputStream, true));' is the way to go. Then use 'planar.getAsBuffredImage()' for display. – Harihar Das Apr 23 '13 at 07:05

1 Answers1

0

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.

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 (e.g. java.io.ByteArrayInputStream), that way you can have ImageIO load directly using the stream based methods.

Durandal
  • 19,919
  • 4
  • 36
  • 70
  • Thanks for your response. The image is saved as _image_ type in the database. If you look at the data it is a large hex integer. I am reading the data as bytes. Is there any other way I should try? I tried the way you had mentioned. Still no results. May be I am wrong while reading the data from database(!?) – Harihar Das Apr 18 '13 at 08:34
  • The type column "IMAGE" in MS-SQLServer has nothing to do with graphical images. Image is a blob type for binaries, it can contain any kind of binary data. If ImageIO can't handle all images, you can only look manually at the ones that fail and check out the fisrt four bytes - those contain usually some magic number that allows you to guess the format of the file (http://www.astro.keele.ac.uk/oldusers/rno/Computing/File_magic.html has a short list of common magic numbers). I suspect that some of your files are not images at all, or its an uncommon format not supported by ImageIO. – Durandal Apr 19 '13 at 13:38
  • I checked in the link you provided and it's little endian intel TIFF format. And yes this image column will always have image data. All image data starts with '49492a00'. I see that tiff format is not supported by default by ImageIP API. As an additional info in most cases I have the image data in the form of files as well, but I am not able to read those images too, even after including JAI into my library. – Harihar Das Apr 22 '13 at 14:50
  • I had no trouble reading litte endian tif with JAI. Although you may have to open the image directly with JAI: PlanarImage planar = JAI.create("stream", SeekableStream.wrapInputStream(inputStream, true)); – Durandal Apr 22 '13 at 15:29
  • Are you sure you *installed* JAI? JAI installs into the local JDK, I believe the SPI integration does not work if you simply add it to the classpath. See: http://download.java.net/media/jai/builds/release/1_1_3/INSTALL.html#Java. I had to access tif images directly through JAI because I didn't *install* JAI. – Durandal Apr 22 '13 at 15:36
  • Thanks a ton. Using JAI directly solved my problem. And yes you are right, I simply loaded JAI jars into my classpath without installing it and unfortunately I do have restricted access to install it. Thanks much. – Harihar Das Apr 23 '13 at 07:01