+ /**
+ * Convert the given {@link Image} object into a Base64 representation of
+ * the same {@link Image}. object.
+ *
+ * @param image
+ * the {@link Image} object to convert
+ *
+ * @return the Base64 representation
+ */
+ static public String fromImage(BufferedImage image) {
+ String imageString = null;
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+
+ try {
+ ImageIO.write(image, "png", out);
+ byte[] imageBytes = out.toByteArray();
+
+ imageString = DatatypeConverter.printBase64Binary(imageBytes);
+
+ out.close();
+ } catch (IOException e) {
+ }
+
+ return imageString;
+ }
+
+ /**
+ * Convert the given Base64 representation of an image into an {@link Image}
+ * object.
+ *
+ * @param b64data
+ * the {@link Image} in Base64 format
+ *
+ * @return the {@link Image} object
+ *
+ * @throws IOException
+ * in case of IO error
+ */
+ static public BufferedImage toImage(String b64data) throws IOException {
+ BufferedImage image = ImageIO.read(new ByteArrayInputStream(
+ DatatypeConverter.parseBase64Binary(b64data)));
+ image.toString();
+ return image;
+ }
+