Convert Data-URL to BufferedImage
I think, a simple regex replace would be better and more conform to the RFC2397:
java.util.Base64.getDecoder().decode(b64DataString.replaceFirst("data:.+,", ""))
The RFC states that the data:
and the ,
are the required prefixes for a data url, therefore it is wise to match for them.
The only one problem with RFC2397 string is its specification with everything before data but data:
and ,
optional:
data:[<mediatype>][;base64],<data>
So pure Java 8 solution accounting this would be:
final int dataStartIndex = dataUrl.indexOf(",") + 1;
final String data = dataUrl.substring(dataStartIndex);
byte[] decoded = java.util.Base64.getDecoder().decode(data);
Of course dataStartIndex should be checked.
As the comments already said the image data is Base64 encoded. To retrieve the binary data you have to strip the type/encoding headers, then decode the Base64 content to binary data.
String encodingPrefix = "base64,";
int contentStartIndex = dataUrl.indexOf(encodingPrefix) + encodingPrefix.length();
byte[] imageData = Base64.decodeBase64(dataUrl.substring(contentStartIndex));
I use org.apache.commons.codec.binary.Base64
from apaches common-codec, other Base64 decoders should work as well.