Program to convert base64 to Image and vice versa
Code snippet:
This method decodes the base64 string into the bytes and converts it into an image.
* Note: this method expects input to be an encoded Image in base64 format. It can't work with other other files encoded in base64 format
ByteArrayInputStream bis = new ByteArrayInputStream(Base64.getDecoder().decode(base64data));
BufferedImage img = ImageIO.read(bis);
bis.close();
This method encodes the PNG image to base64 format
ByteArrayOutputStream output = new ByteArrayOutputStream();
ImageIO.write(image, "PNG", output);
String encodedString = Base64.getEncoder().encodeToString(output.toByteArray());
output.close();
This method decodes the base64 string into the bytes and converts it into an image.
* Note: this method expects input to be an encoded Image in base64 format. It can't work with other other files encoded in base64 format
ByteArrayInputStream bis = new ByteArrayInputStream(Base64.getDecoder().decode(base64data));
BufferedImage img = ImageIO.read(bis);
bis.close();
This method encodes the PNG image to base64 format
ByteArrayOutputStream output = new ByteArrayOutputStream();
ImageIO.write(image, "PNG", output);
String encodedString = Base64.getEncoder().encodeToString(output.toByteArray());
output.close();
Comments
Post a Comment