I've encoded a image into base64 using the org.apache.axis.encoding.Base64 class. My code:
public String get64EncodedPhotoForUser(User user) {
// Path to image
String path = "some path to the jpg file on the server";
// Read image from server
String base64Image = null;
try {
BufferedImage bufferedImage = ImageIO.read(new File(path));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "jpg", baos);
baos.flush();
byte[] imageInByte = baos.toByteArray();
baos.close();
base64Image = Base64.encode(imageInByte);
} catch (Exception e) {
System.out.println(e.getMessage());
}
return base64Image;
}
As you can see I first convert the image into a byte array as that is what the encode method expects.. Everything seems to work fine, I get my encoded string back and it seems to be a correctly encoded string. Worth mentioning here that some online decoders can correctly decode the image while others again can't, which I thought is odd..
When I try and display the image in my HTML:
<img ng-src="data:image/png;base64,/9j/4AAQSkZJRgABAgAAA..." id="avatar">
It doesn't work. I just get that 'missing image' icon you get when you're pointing your html to an image that doesn't exist.
I've copied and pasted the encoded string into a jsfiddle you can check out here
PS - I also take the base64 string and I pass it from the server to the client in a JSON object along with some other things. The Base64 string you're seeing in the jsfiddle is what I get back on the client-side. Could it be that the encoded string could perhaps have been corrupted or modified, when it was passed over the network?