Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
640 views
in Technique[技术] by (71.8m points)

javascript - Pass image from java applet to html

I am using a signed java applet to load an image from the filesystem of the user.
Then I want to display this image in the website where the applet is running.

I know how to communicate between applet and JavaScript, but I only used strings and numbers as parameter.

How do I handle image objects and how do I display them on the website?

If required I can convert the format in the applet to match the JavaScript.

Edit:
I passed the Image object from java to javascript with a call from JSObject. Chrome ignores the call and Firefox crash..

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You might encode the image as Base 64, pass it to JS as a String, and use the data:image/gif; form URL to display it in the web page. You'll need to 'roll your own' base 64 encoder or find an API, since the J2SE has no inbuilt method for the conversion.1

It might look something like this in the HTML.

<img src="data:image/gif;base64,R0lGODlhEAAOALMAAOazToeHh0tLS/7LZv/0jvb29t/f3//Ub/
/ge8WSLf/rhf/3kdbW1mxsbP//mf///yH5BAAAAAAALAAAAAAQAA4AAARe8L1Ekyky67QZ1hLnjM5UUde0ECwLJoExKcpp
V0aCcGCmTIHEIUEqjgaORCMxIC6e0CcguWw6aFjsVMkkIr7g77ZKPJjPZqIyd7sJAgVGoEGv2xsBxqNgYPj/gAwXEQA7" 
width="16" height="14" alt="embedded folder icon">

E.G. taken from Inline Images with Data URLs.

  1. On later JREs (once JAXB was introduced) look to use DatatypeConverter.printBase64Binary(byte[]) something like this:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
    ImageIO.write(image, "png", baos);
} catch (IOException e) {
    showError(e);
    e.printStackTrace();
}
String imageString = "data:image/png;base64," +
    DatatypeConverter.printBase64Binary(baos.toByteArray());

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...