A Base64 string is a way of encoding binary data using 64 printable characters, which are the 26 uppercase letters of the English alphabet, the 26 lowercase letters of the English alphabet, the 10 numerical digits, and the “+” and “/” symbols. This makes a total of 64 distinct characters, hence the name “Base64”.
Base64 encoding is commonly used when there is a need to encode binary data, especially when that data needs to be stored and transferred over media that is designed to handle text.
The primary use case of this encoding is to allow binary data to be represented in a way that looks and acts as plain text. For example, embedded images in HTML (often as data URIs), and storing complex data in XML or JSON.
In Java, you can use the java.util.Base64
classes to convert an image file to a base64 String. You can use it to convert a JPG or a PNG image file, or basically any binary image files. Here is a simple example:
package org.kodejava.util;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;
public class ImageToBase64 {
public static void main(String[] args) throws Exception {
String imagePath = "/Users/wayan/tmp/photo-placeholder.png";
byte[] fileContent = Files.readAllBytes(Paths.get(imagePath));
String encodedString = Base64.getEncoder().encodeToString(fileContent);
if (encodedString.length() > 65535) {
System.out.println("Encoded string is too large");
} else {
System.out.println(encodedString);
}
}
}
Here are the first 200 characters of the generated base64 string:
iVBORw0KGgoAAAANSUhEUgAAAGQAAAB2CAYAAAA+/DbEAAAAAXNSR0IArs4c6QAAAIRlWElmTU0AKgAAAAgABQESAAMAAAABAAEAAAEaAAUAAAABAAAASgEbAAUAAAABAAAAUgEoAAMAAAABAAIAAIdpAAQAAAABAAAAWgAAAAAAAABIAAAAAQAAAEgAAAABAAOgAQA...
The size of a base64-encoded string could be significantly larger than the original file. It’s not always the best way to handle large files or in cases where you’re sensitive to data usage. To check the size of the generated string using the length()
method of the String
class.
As you can see in the code snippet above, the application will print “Encoded string is too large” if the base64 string of the image is larger than 65535 characters. Otherwise, it will print the base64 string
To use a Base64 encoded string with an HTML <img>
tag, you can use the src
attribute and specify the data as follows:
<img src="data:image/png;base64,iVBORw0..." alt="Your image description">
In this line:
data
: is the Data URI scheme specifier.image/png
is the media type. This can beimage/jpeg
,image/gif
, or other image types.base64
indicates that the data is base64 encoded.iVBORw0...
is where your base64 data begins. ReplaceiVBORw0...
with your base64 string.
You should replace image/png
with the actual type of your image and replace the iVBORw0...
part with your full Base64 string.
This approach allows you to inline small images directly into your HTML, reducing the number of HTTP requests. However, you should note that if images are large, this can increase the size of your HTML document and slow down load times. It might be more appropriate to use external image files for larger images.
Note that Base64 is not an encryption or hashing method, and should not be used for password or security purposes. It is a binary-to-text encoding schemes that represent binary data in an ASCII string format. It’s designed to be easily transmitted and stored while ensuring that the data remains intact without modification during transport.
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024