base64 image into JTextPane
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);
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
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!
Finally I also found out where I have to override, "loadimage" method. I should add conditional flow of [src="foo"] to load image, would be just image's link or image data itself. Now, all I'd write "JTextPane.setEditorKit(new MyExtendedHtmlEditorKit());".
However, my problem is here. the method exists "ImageViewer.class create()[HtmlFactory.class(static inner class)[HtmlEditorKit.class]] > void loadImage()[ImageViewer.class]" That's deep nested ,those class have many object invisible from outside class and many static definition. So, this means I must build a whole tree from roots for just pick a leaf on the top of branch. I can't edit Java VM runtime directly, compile and distribute it. I think this is IMPOSSIBLE, at least It's for sure for me!