0

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...

Saurabh Kumar
  • 15,763
  • 47
  • 125
  • 205

1 Answers1

7
  1. .replace(regex,repl) treats regex as "literal" (doesn't allow to use "^" to denote beginning of line, etc) - try .replaceFirst(regex,repl) instead
  2. as discussed here - try other Base64-decoders (which might better suit Your needs, the one below may have issues above 64KB of string length)

Aside 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!
Community
  • 1
  • 1
Vlad
  • 1,112
  • 6
  • 14