The HtmlUnit's HtmlImage#getImageReader()
returns javax.imageio.ImageReader
which is part of standard Java 2D API. You can get an BufferedImage
out of it which you in turn can write to an OutputStream
of any flavor using ImageIO#write()
.
The Apache Commons Codec has a Base64OutputStream
which you can just decorate your OutputStream
with.
HtmlImage img = (HtmlImage) p.getByXPath("//img").get(3);
ImageReader imageReader = img.getImageReader();
BufferedImage bufferedImage = imageReader.read(0);
String formatName = imageReader.getFormatName();
ByteArrayOutputStream byteaOutput = new ByteArrayOutputStream();
Base64OutputStream base64Output = new base64OutputStream(byteaOutput);
ImageIO.write(bufferedImage, formatName, base64output);
String base64 = new String(byteaOutput.toByteArray());
Or if you want to write it to file directly:
// ...
FileOutputStream fileOutput = new FileOutputStream("/base64.txt");
Base64OutputStream base64Output = new base64OutputStream(fileOutput);
ImageIO.write(bufferedImage, formatName, base64output);