-4

I have a problem with showing a picture from the database, the database saves the picture in blob, when i pick up the data the blob passes to Byte[], so after that i do that to show the image, why didnt work?

Select_1 xp = new Select_1();
byte[] img=xp.Select_1(username);
InputStream in = new ByteArrayInputStream(img);
BufferedImage image = ImageIO.read(in);
BufferedImage resizedImage=resize(image,204,204);
ImageIcon icon=new ImageIcon(resizedImage);
lblavatar.setIcon(icon);

Edit according to the comment:

Originally, the image was written using the following methods:

blob = (Blob) connect.createBlob(); 
ImageIcon ii = new ImageIcon(ficheiro); 
ObjectOutputStream oos; 
oos = new ObjectOutputStream(blob.setBinaryStream(1));
oos.writeObject(ii);  
oos.close(); 
psInsert.setBlob(4, blob);
Marco13
  • 53,703
  • 9
  • 80
  • 159
  • Possible duplicate: [How to convert a byte array to a BufferedImage in Java?](http://stackoverflow.com/questions/12705385/how-to-convert-a-byte-to-a-bufferedimage-in-java) – Ascalonian Feb 16 '15 at 19:35
  • 1
    You write "BufferedReader" in the title, but your code has no BufferedReader. You say "I have a problem" and you don't tell us which is (error message, exception, unexpected result). Your two first line of code are incomprehensible ( what the heck is `xp.Select_1` ??) YOu don't tell us if the first `image` was retrieved ok. These are just a few of the reasons your question lacks quality. – leonbloy Feb 16 '15 at 19:39
  • Any error message, exception? What kind of image format does the database blob hold – dragon66 Feb 16 '15 at 19:39
  • Sorry is BufferedImage. The problem is that do not retrieve any error...and the image was retrieved( i think) The way i pull the blob from to database to byte[] `byte[] img= resultSet.getBytes("avatar");` Is that wrong? – Joni Correia Feb 16 '15 at 19:51
  • How was the original image converted to a blob? – MadProgrammer Feb 16 '15 at 20:28
  • `blob = (Blob) connect.createBlob(); ImageIcon ii = new ImageIcon(ficheiro); ObjectOutputStream oos; oos = new ObjectOutputStream(blob.setBinaryStream(1)); oos.writeObject(ii); oos.close(); psInsert.setBlob(4, blob);` @MadProgrammer – Joni Correia Feb 16 '15 at 20:38
  • You've stored the `ImageIcon` object in the blob not the physical image data. Object serialisation is not meant for long term persistance of objects – MadProgrammer Feb 16 '15 at 20:59

1 Answers1

0

You are not serialzing a BufferedImage, but an ImageIcon.

In order to create an image from the blob data, you have to do "the opposite" of what you have been doing to create the blob. In this case, you'll have to do something along the lines of

byte[] img=xp.Select_1(username);
InputStream in = new ByteArrayInputStream(img);
ObjectInputStream ois = new ObjectInputStream(in);
ImageIcon imageIcon = (ImageIcon)ois.readObject();

Now, you have an ImageIcon, from which you can obtain the Image. For many cases, this image can be used directly. If you really need a BufferedImage, then you can do

BufferedImage bi = new BufferedImage(204,204,BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.createGraphics();
g.drawImage(imageIcon.getImage(), 0, 0, 204, 204, null);
g.dispose();

Then the buffered image will contain your image (already scaled to the desired target size of 204,204 in this case).

In any case, you should consider to not store the image as a serialized ImageIcon. Instead, you should write your image into a byte array as a PNG or JPG file, and store the resulting byte array as the blob in the database, as, for example, shown in Java: BufferedImage to byte array and back

Community
  • 1
  • 1
Marco13
  • 53,703
  • 9
  • 80
  • 159
  • It would be better to write the original image via `ImageIO.write` – MadProgrammer Feb 16 '15 at 21:00
  • @MadProgrammer That's what I said in the last paragraph. I even used a **bold** font there! :-o – Marco13 Feb 16 '15 at 21:29
  • @MadProgrammer It's difficult. If someone asks "How can I shoot me in the foot?", saying "You should not do this" could be considered as "Not an answer". In this case, it's not even clear whether the asker can (still) influence the format of the data that was stored in the blob. I hope the **bold** font and the link (and these comments) will lead him (and everybody else) in the right direction (if they have the option) – Marco13 Feb 16 '15 at 23:28