Mark Lorens Mark Lorens - 18 days ago 4x
Java Question

How to save and read image as text, embedded in the code

I know this is a strange question but I really need this to work.
I have a very small

image, I need to import ("type it") it into a java source file without reading from the file system. (At least not when the program runs, I can read the image on the system with other dummy applications to parse is in binary or I don't know what else).

In other words, I literally need lines of code that represent the image, which I will only put there once (I don't need to import on the fly, I just type it and leave it there forever).

The image needs to be stored as an
and, since you can type out byte arrays, I don't see why it wouldn't be possible to create a little jar that reads the image, prints the byte array to a .txt file, that I can then copy and put into the source file. I just don't know how to do these steps exactly.

I know this is not something one would or should do, it's for a specific project and I need it done this way, the reasons go beyond me.

I cannot use any external lib or dependency.


FYI, similar questions to this one that may help provide a more indepth explanation/answer are here and here.

My answer to solve your question about,

I literally need lines of code that represent the image, which I will only put there once (I don't need to import on the fly, I just type it and leave it there forever).

is to use Base64 and read the image code in that per-determined format.

For example, the following code makes a simple JFrame to display a base64 string as an image icon:

import java.awt.BorderLayout;
import java.awt.image.BufferedImage;
import java.util.Base64;
import java.util.Scanner;

import javax.imageio.ImageIO;
import javax.swing.*;

public class App extends JFrame {
    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                App app = new App();
    public App() {
        JLabel photo = new JLabel();
        add(photo, BorderLayout.CENTER);
        setSize(200, 100);

        // Ex. stackoverflow.ico (then converted to png for this example)
        String imgBase64 = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAJ+SURBVFhH7ZXbS1RRFMb7Y5qHiu4PEfUQRS9B9FBEBBVFT70WvQQhmJpdLUkMS0mNikDKlKxIDR+iCz3YmJgWCZmlKWPNPvd9vvZeZ8+ZS3NmzlzoYn2wHmatYe/f2etbey/Ab9Z/gPkFwCcGoTVtB/8yojL5VTYA1/gGdnIV4hULoV3cBFefU5XcKusJ2G96BECEIPT2A4LKUZVglQYgNuBTo+qHJ7P3PAHIMB+dUtlglQRgdh1HvHIx7Jc3VEbI5dCvH1QQEdjRblXIrqIB3NhHsDNr/a81Oo4AlubVhB+0+s1erWop+OdhymdTSSfgfp+C3rLbh9AatoB/fU81Pv0OrGYl5VndBrhslvKZKt2E3KFeJ8zHalbAHrpPJWfksZ/XW/eSZzJVMIA92CkW7qVep8oZfuiPodzU7KkScDas/nqVE6aUuQwVBODN+mpaTLuwEdbTFsolxGc+QGvc6m+oN++COzcJ/eYhsOpl6WZVKghAjpxcNLGBDFa9HGZ3hd972AaMO0eT9dNr4IwNgM+Oe/UMFeUB/inqbSJGMAkTocvHGe2n9tivbiN+YgnYufV0CkEqyYQum4H15BLY2XUpIKI9YgStZ61wxl/kfRdCAzhv+4QB74rxGvvZzcJs9ut70K7sSAOxBhrVH4IVGkC/tie5uLhc9Ks7qffyqPnkEEFIyRfR6DhMl5RrximXS+EB2vbTjKd+YVpULoJ2eRuMzmOwnrfTRIRRYR4Q5pJut6Nd4vKpFZfLPrBabywzQ7YsjAIBmttuUYSRG5sQF9EDmH119BCR87UY1fKtUxaAXCobQLZjzhUJzR+AYvX3A5QrgvTnAvwq/esAwA/q0/BiaJrlDgAAAABJRU5ErkJggg==";
        String data = imgBase64.substring(imgBase64.indexOf(",") + 1);
        ByteArrayInputStream stream = new ByteArrayInputStream(Base64.getDecoder().decode(data.getBytes()));
        BufferedImage image = null;
        try {
            image =;
        } catch (IOException e) {
        ImageIcon imgIcon = new ImageIcon(image.getScaledInstance(32, 32, 0));

This code doesn't use any external libraries and accomplishes most of the task you have requested - so to fully complete the answer, here's how I'd go about reading that input from the console (aka a user types into the console). Note: the following portion of code would be added after the line with the ByteArrayInputStream stream object declaration.

Scanner sc = new Scanner(;
System.out.println("Type .png input (base64): ");
StringBuilder sb = new StringBuilder();
while (sc.hasNext())
String data = sb.toString();

This would essentially replace the two lines of the 'String imgBase64' and the 'String data' from the earlier example snippet.

Hope this answers your question, please provide feedback for further clarification or mark as an answer ;)


Edit: For going from and image to string, image -> string: Note: I am using a File for this example, you may also choose to look into the URL or Stream options that could be used alternatively in the method. Also note: the try-with-resources is java 1.7+ (could revert easily to try-catch if needed for earlier versions)

Lets say you loaded up an ImageIcon object from somewhere, URL/stream/file, you name it. In my example, I'll use a local file:

ImageIcon icon = new ImageIcon("C:\\Users\\Nick\\Desktop\\favicon.png");

Now take this image icon and get the base64 string you ask? Here's one way how to do it / how I did it (using the already loaded ImageIcon icon object):

try (ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageOutputStream ios = ImageIO.createImageOutputStream(os)) {
    BufferedImage img = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.BITMASK);
    Graphics g = img.createGraphics();
    icon.paintIcon(null, g, 0, 0);
    ImageIO.write(img, "png", ios);
    byte[] bytes = os.toByteArray();
    String data = Base64.getEncoder().encodeToString(bytes);
} catch (IOException e) {

One thing I'd like to point out is the 'BufferedImage.BITMASK' flag being used. This could easily get mixed up or switched and cause inconsistencies with the decoded/encoded string value(s).

More about the ImageIcon class here:

Used this tool for converting stackoverflow icon to base64