How to convert ImageIcon to read/write it on db

C

carmelo

Hi everybody,
I'm working on a swing app which have to read/write from a MySql
database, using JPA. The image is displayed on a JLabel. The problem
is that for using data binding I need to create a converter to convert
the image "to" and "from" the database...

I wrote this converter, but I get runtime casting exceptions:


public class ImageConverter extends Converter
{
//byte to image
public ImageIcon convertForward(Object arg)
{
Image image = new ImageIcon( ( byte[] ) arg ).getImage()
.getScaledInstance( 50, 50,
Image.SCALE_SMOOTH );

return new ImageIcon( image );
}

//image to byte
public byte[] convertReverse(Object arg)
{
ByteArrayOutputStream bStream = new ByteArrayOutputStream();
ObjectOutputStream oStream = null;
try {
oStream = new ObjectOutputStream(bStream);
oStream.writeObject(arg);
} catch (IOException ex) {
Logger.getLogger(ConvertitoreImage.class.getName()).log
(Level.SEVERE, null, ex);
}
byte[] byteVal = bStream. toByteArray();

return new ImageIcon(byteVal);
}

}


I hope you can help me. Thank you very much for your help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,821
Messages
2,569,748
Members
45,726
Latest member
RaleighAll

Latest Threads

Top