views:

183

answers:

2

Is it possible to display a base64 encrypted image into a JTextPane ?

Here my code

JTextPane jTextPane = new JTextPane();

javax.swing.text.html.HTMLEditorKit eKit = new javax.swing.text.html.HTMLEditorKit();
jTextPane.setEditorKit(eKit);

jTextPane.setContentType("text/html");

// my base64 image, used then in the img tag in the html...
String img64="data:image/gif;base64,R0lGODdhMAAwAPAAAAAAAP///ywAAAAAMAAwAAAC8IyPqcvt3wCcDkiLc7C0qwyGHhSWpjQu5yqmCYsapyuvUUlvONmOZtfzgFzByTB10QgxOR0TqBQejhRNzOfkVJ+5YiUqrXF5Y5lKh/DeuNcP5yLWGsEbtLiOSpa/TPg7JpJHxyendzWTBfX0cxOnKPjgBzi4diinWGdkF8kjdfnycQZXZeYGejmJlZeGl9i2icVqaNVailT6F5iJ90m6mvuTS4OK05M0vDk0Q4XUtwvKOzrcd3iq9uisF81M1OIcR7lEewwcLp7tuNNkM3uNna3F2JQFo97Vriy/Xl4/f1cf5VWzXyym7PHhhx4dbgYKAAA7";

jTextPane.setText(html);
A: 

What you have to do is parse the Base64 encoded image into a byte array, then load an image using a ByteArrayIn,putStream using that byte array.

As an example, for decoding Base64, you can use javax.xml.bind.DatatypeConverter#parseBase64Binary

Riduidel
// ok thank you i used this: public static byte[] decode(String str){ byte[] bt = null; try { sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder(); bt = decoder.decodeBuffer( str ); } catch (IOException e) { e.printStackTrace(); } return bt; }// and then byte [] parseImg = f.decode(texte); ImageIcon image = new ImageIcon(parseImg, "asd"); jTextPane.insertIcon(image);// but it doesn't dislay the image..could you help me ?
Nicolas
Well, without more infos, it will be hard to help you. Anyway, since there is no exception showing, I guess the Base64 decoder worked fine. As a consequence, issue come from the image icon creation. According to ImageIcon constructor documentation, iamge should be " containing a supported image format, such as GIF, JPEG, or (as of 1.3) PNG" Is it your case ?
Riduidel
thank you for your infos, i completed my question see below...
Nicolas
A: 

I found out!

solution is to extend the HTMLEditorKit by overriding the
getViewFactory in class javax.swing.text.html.HTMLEditorKit then rewrite in the ImageViewclass.java the loadImage method to support images base64 encoded!

Nicolas