import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
-import java.io.DataInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Scanner;
import java.util.regex.Pattern;
-import java.util.zip.ZipInputStream;
import javax.imageio.ImageIO;
* in case of IO error
*/
static public String fromImage(BufferedImage image) throws IOException {
+ return fromImage(image, null);
+ }
+
+ /**
+ * Convert the given {@link Image} object into a Base64 representation of
+ * the same {@link Image}. object.
+ *
+ * @param image
+ * the {@link Image} object to convert
+ * @param format
+ * the image format to use to serialise it (default is PNG)
+ *
+ * @return the Base64 representation
+ *
+ * @throws IOException
+ * in case of IO error
+ */
+ static public String fromImage(BufferedImage image, String format)
+ throws IOException {
+ if (format == null) {
+ format = "png";
+ }
+
String imageString = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
- ImageIO.write(image, "jpeg", out);
+ ImageIO.write(image, format, out);
byte[] imageBytes = out.toByteArray();
imageString = new String(Base64.encodeBytes(imageBytes));