妙传Java小程序的图像为HTML [英] Pass image from java applet to html

查看:144
本文介绍了妙传Java小程序的图像为HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是签署Java小程序加载从用户的文件系统的图像。结果
然后我想在小应用程序运行的网站显示此图像。

我知道如何小程序和JavaScript之间的沟通,但我只用字符串和数字作为参数。

我如何处理图像对象?如何在网页上显示它们?

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

编辑:结果
我通过Image对象从Java到JavaScript的与JSObject的电话。 Chrome浏览器会忽略通话和Firefox崩溃。


解决方案

您可以连接code中的形象为基础64,它传递给JS为字符串,并使用数据:图像/ GIF; 形式的URL在网页中显示它。你需要'滚你自己的'基地64 EN codeR或找到一个API,因为J2SE对转换没有内置的方法。 1

这可能看起来像这样在HTML

 < IMG SRC =数据:图像/ GIF; BASE64,R0lGODlhEAAOALMAAOazToeHh0tLS / 7LZv / 0jvb29t / F3 // UB /
/ ge8WSLf / RHF / 3kdbW1mxsbP // /// MF yH5BAAAAAAALAAAAAAQAA4AAARe8L1Ekyky67QZ1hLnjM5UUde0ECwLJoExKcpp
V0aCcGCmTIHEIUEqjgaORCMxIC6e0CcguWw6aFjsVMkkIr7g77ZKPJjPZqIyd7sJAgVGoEGv2xsBxqNgYPj / gAwXEQA7
WIDTH =16高度=14ALT =嵌入式文件夹图标>

例如。从数据的URL 内嵌图像拍摄。


  1. 在以后的JRE(JAXB一次被引入)外观使用<一个href=\"http://docs.oracle.com/javase/7/docs/api/javax/xml/bind/DatatypeConverter.html#printBase64Binary%28byte%5b%5d%29\"相对=nofollow> DatatypeConverter.printBase64Binary(字节[]) 是这样的:

ByteArrayOutputStream BAOS =新ByteArrayOutputStream();
尝试{
    ImageIO.write(形象,PNG,BAOS);
}赶上(IOException异常五){
    showError(E);
    e.printStackTrace();
}
字符串和imagestring =数据:图像/ PNG; BASE64,+
    DatatypeConverter.printBase64Binary(baos.toByteArray());

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..

解决方案

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

这篇关于妙传Java小程序的图像为HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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