views:

31

answers:

1

Hello,

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.

+1  A: 

You have basically 2 options:

  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.

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

BalusC
Thank you for the suggestion, encoding to a Base64 String solved my problems. I was then able to decode it in PHP with base64_decode to get my image back.
rjc
You're welcome.
BalusC