将图像从 java applet 传递到 html [英] Pass image from java applet to html

查看:22
本文介绍了将图像从 java applet 传递到 html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用签名的 Java 小程序从用户的文件系统加载图像.
然后我想在运行小程序的网站上显示这个图像.

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.

我知道如何在小程序和 JavaScript 之间进行通信,但我只使用字符串和数字作为参数.

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?

如果需要,我可以转换小程序中的格式以匹配 JavaScript.

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


我通过来自 JSObject 的调用将 Image 对象从 java 传递到 javascript.Chrome 忽略调用,Firefox 崩溃..


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

推荐答案

您可以将图像编码为 Base 64,将其作为字符串传递给 JS,并使用 data:image/gif;表单 URL 以在网页中显示它.您需要推出自己的"base 64 编码器或找到 API,因为 J2SE 没有用于转换的内置方法.1

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

它在 HTML 中可能看起来像这样.

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.取自带有数据 URL 的内嵌图像.

  1. 在后来的 JRE 上(一旦引入了 JAXB)希望使用 DatatypeConverter.printBase64Binary(byte[]) 像这样:

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());

这篇关于将图像从 java applet 传递到 html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆