如何将图像从 Java Applet 发送到 JavaScript? [英] How to send an image from a Java Applet to JavaScript?

查看:29
本文介绍了如何将图像从 Java Applet 发送到 JavaScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个生成图像的 Java Applet.最后,我想将图像数据插入到数据库中,因此我想将图像数据临时存储在包含小程序的页面上的表单字段中.我希望在不将图像文件存储在客户端计算机上的情况下执行此操作.

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.

这一切都来自签名簿.下面是一些代码,用于从存储在 sigObj 对象中的矢量数据生成位图图像:

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

图像变量是一个 BufferedImage 对象.此外,如果我只是将图像变量发送回我的 JavaScript,这里是警报输出:

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?

我对Java不太了解,所以如果我问了一个愚蠢的问题,我很抱歉.

I do not know much Java, so I apologize if I am asking a dumb question.

谢谢.

根据 BalusC 的建议,这里是我用来将图像转换为 Base64 字符串的代码,供任何可能好奇的人使用:(img 是 BufferedImage,dataImg 是字符串)

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) {}

这使用 Apache Commons Codec 进行 Base64 编码.也许这是微不足道的,但对我来说是新的.

This uses the Apache Commons Codec to do the Base64 encoding. Maybe this is trivial, but it was new to me.

推荐答案

您基本上有 2 个选择:

You have basically 2 options:

  1. 使用 Base64 这样二进制数据作为字符数据传输时不会出现格式错误.然后,让 JavaScript 将其设置为某个 POST 表单的某个隐藏输入值.在服务器端,您需要对它进行 Base64 解码以取回原始字节数组.

  1. Encode it from byte[] to String 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.

使用 Java 在 Applet 内以编程方式触发 HTTP POST 请求.Java SE API 提供了 java.net.URLConnection 为此(这里的小教程).一个更方便的 API 是 Httpclient.在服务器端,您只需要将其作为常规 POST 请求参数进行处理.

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.

这篇关于如何将图像从 Java Applet 发送到 JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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