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.