title: How to send an image from a Java Applet to JavaScript?
body: <p>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.</p>
  
  <p>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:</p>
  
  <pre><code>sigObj.setImagePenWidth(10);
  sigObj.setImageXSize(1000);
  sigObj.setImageYSize(350);
  image = sigObj.sigImage();
  </code></pre>
  
  <p>The image variable is a BufferedImage object. Also, here is the alert output if I just send the image variable back to my JavaScript:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>(Line breaks added for readability)</p>
  
  <p>Is it possible to send the image itself back? Any suggestions? </p>
  
  <p>I do not know much Java, so I apologize if I am asking a dumb question.</p>
  
  <p>Thank you.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>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)</p>
  
  <pre><code>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) {}
  </code></pre>
  
  <p>This uses the <a href="http://commons.apache.org/codec/" rel="nofollow noreferrer">Apache Commons Codec</a> to do the Base64 encoding. Maybe this is trivial, but it was new to me.</p>