如何从一个Java小程序发送图像的JavaScript? [英] How to send an image from a Java Applet to JavaScript?

查看:149
本文介绍了如何从一个Java小程序发送图像的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对象矢量数据有点像马培德一些code:

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的建议,这里是code我习惯了图像转换为Base64编码字符串任何人谁可能会好奇:(IMG是一个BufferedImage,dataImg是一个String)

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的共享codeC 做Base64编码。也许这是小事,但它是新的我。

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

推荐答案

您基本上有2个选择:


  1. 恩code将其从字节[] 使用的 = http://commons.apache.org/$c$cc/apidocs/org/apache/commons/$c$cc/binary/Base64.html相对=nofollow> Base64的使二进制数据当被转移为字符数据会不会畸形。那么,让我们的JavaScript将其设置为一些POST形式的一些隐藏的输入值。在服务器端,你需要为Base64德code它来获得原始字节数组回来。

  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编程触发一个HTTP POST请求的小程序里面。在Java SE的API提供 的java .net.URLConnection 这个(<一个href=\"http://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests\">little教程这里)。更convenienced 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小程序发送图像的JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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