如何将图像从 Java Applet 发送到 JavaScript?
我有一个正在生成图像的 Java Applet。最终,我想将图像数据插入数据库,因此我想将图像数据临时存储在包含小程序的页面上的表单字段中。我希望做到这一点而不在客户端计算机上存储图像文件。
这一切都来自签名板。下面是一些应该从 sigObj 对象中存储的矢量数据生成位图图像的代码:
sigObj.setImagePenWidth(10);
sigObj.setImageXSize(1000);
sigObj.setImageYSize(350);
image = sigObj.sigImage();
image 变量是 BufferedImage 对象。另外,如果我只是将图像变量发送回 JavaScript,这里是警报输出:(
BufferedImage@fe748f: type = 5
ColorModel: #
pixelBits = 24
numComponents = 3
color space = java.awt.color.ICC_ColorSpace@641e9a
transparency = 1
has alpha = false
isAlphaPre = false
ByteInterleavedRaster: width = 1000 height = 350 #
numDataElements 3
dataOff[0] = 2
为了可读性而添加换行符)
是否可以将图像本身发送回?有什么建议吗?
我对 Java 不太了解,所以如果我问了一个愚蠢的问题,我很抱歉。
谢谢。
编辑:
根据 BalusC 的建议,以下是我用于将图像转换为 Base64 字符串的代码,供任何可能好奇的人使用:(img 是 BufferedImage,dataImg 是字符串)
import org.apache.commons.codec.binary.Base64;
...
try{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(img, "BMP", baos);
byte[] bytes = baos.toByteArray();
dataImg = new Base64().encodeBase64String(bytes);
} catch(Exception e) {}
这使用Apache Commons Codec 进行 Base64 编码。也许这很微不足道,但对我来说是新的。
I have a Java Applet that is generating an image. Ultimately, I would like to insert the image data into a database, so I want to temporarily store the image data in a form field on the page containing the applet. I am hoping to do this without storing an image file on the client machine.
This all comes from a signature pad. Here is some code that is supposed to generate a bit maped image from vector data stored in the sigObj object:
sigObj.setImagePenWidth(10);
sigObj.setImageXSize(1000);
sigObj.setImageYSize(350);
image = sigObj.sigImage();
The image variable is a BufferedImage object. Also, here is the alert output if I just send the image variable back to my JavaScript:
BufferedImage@fe748f: type = 5
ColorModel: #
pixelBits = 24
numComponents = 3
color space = java.awt.color.ICC_ColorSpace@641e9a
transparency = 1
has alpha = false
isAlphaPre = false
ByteInterleavedRaster: width = 1000 height = 350 #
numDataElements 3
dataOff[0] = 2
(Line breaks added for readability)
Is it possible to send the image itself back? Any suggestions?
I do not know much Java, so I apologize if I am asking a dumb question.
Thank you.
Edit:
As per the suggestion from BalusC, here is the code I used to convert the image into a Base64 string for anybody who might be curious: (img is a BufferedImage, dataImg is a String)
import org.apache.commons.codec.binary.Base64;
...
try{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(img, "BMP", baos);
byte[] bytes = baos.toByteArray();
dataImg = new Base64().encodeBase64String(bytes);
} catch(Exception e) {}
This uses the Apache Commons Codec to do the Base64 encoding. Maybe this is trivial, but it was new to me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您基本上有 2 个选项:
使用 Base64 以便二进制数据在作为字符数据传输时不会出现格式错误。然后,让 JavaScript 将其设置为某些 POST 表单的隐藏输入值。在服务器端,您需要对其进行 Base64 解码以获取原始字节数组。
使用 Java 在 Applet 内以编程方式触发 HTTP POST 请求。 Java SE API 提供
java.net .URLConnection
为此(这里有一个小教程)。更方便的 API 是 Httpclient。在服务器端,您只需将其作为常规 POST 请求参数进行处理。目前尚不清楚您使用的是哪种服务器端编程语言,因此我无法给出该方面的更详细答案。
You have basically 2 options:
Encode it from
byte[]
toString
using Base64 so that the binary data won't be malformed when being transferred as character data. Then, let JavaScript set it as some hidden input value of some POST form. In the server side, you need to Base64-decode it to get the original byte array back.Use Java to fire a HTTP POST request programmatically inside the Applet. The Java SE API offers the
java.net.URLConnection
for this (little tutorial here). A more convenienced API is the Httpclient. In the server side you just need to handle it as a regular POST request parameter.It's unclear what server side programming language you're using, so I can't give a more detailed answer for that side.