I have a image data from which I need to remove the following substring
data:image/jpeg;base64,
from the string
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD......
data:image/png;base64......
data:image/;base64
and then I am thinking of doing something like the following
imageData = imageData.replace("regex", "");
return Base64.decodeBase64(imageData.getBytes());
I want to know the regex first and also want to know whether calling
imageData.getBytes()
will work out or not...
.replace(regex,repl)
treats regex as "literal" (doesn't allow to
use "^
" to denote beginning of line, etc) - try .replaceFirst(regex,repl)
insteadAside from validation/handling - should end up with something like this:
String imageData = "data:image/jpeg;base64,SGVsbG8sIHdvcmxkIQ==";
imageData = imageData.replaceFirst("^data:image/[^;]*;base64,?","");
byte[] bytes = javax.xml.bind.DatatypeConverter.parseBase64Binary(imageData);
System.out.println(new String(bytes));
Output:
Hello, world!