Base64 image in JTextPane

Is it possible to display an encrypted base64 image in a JTextPane?

Here is 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);
+3
source share
3 answers

What you need to do is parse the Base64 encoded image into an array of bytes, and then load the image using ByteArrayIn, putStream using this byte array.

As an example, to decode Base64 you can use javax.xml.bind.DatatypeConverter # parseBase64Binary

+1
source

I found out!

HTMLEditorKit getViewFactory javax.swing.text.html.HTMLEditorKit ImageViewclass.java loadImage base64!

+1

, , "loadimage". [src= "foo" ] , . , "JTextPane.setEditorKit( MyExtendedHtmlEditorKit());".

. "ImageViewer.class create() [HtmlFactory.class( ) [HtmlEditorKit.class]] > void loadImage() [ImageViewer.class]" , . , , , . Java VM, . , , , !

+1

Source: https://habr.com/ru/post/1743311/


All Articles