Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
634 views
in Technique[技术] by (71.8m points)

image - Data URI - how to create them in Java?

I have just been told to send the thumbnail of an image using data URI. I have been searching it but all I found was that its basically a textual representation of a file and can be directly used in HTML. I could not really find how to make a data URI in Java. I have an input stream of a file. Can someone please shed some light on it and point me to a way to generate this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

E.G. for an image:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
    ImageIO.write(image, "png", baos);
} catch (IOException e) {
    e.printStackTrace();
}
String imageString = "data:image/png;base64," +
    DatatypeConverter.printBase64Binary(baos.toByteArray());

Example

Run the code below. If FF is the default browser, you might see something like this:

Data URI image in FF

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;

import javax.xml.bind.DatatypeConverter;

public class DataUriConverter {
    
    static String getImageAsString(BufferedImage image) throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        // serialize the image
        ImageIO.write(image, "png", baos);
        // convert the written image to a byte[]
        byte[] bytes = baos.toByteArray();
        System.out.println("bytes.length " + bytes.length);
        // THIS IS IT! Change the bytes to Base 64 Binary
        String data = DatatypeConverter.printBase64Binary(bytes);
        // add the 'data URI prefix' before returning the image as string
        return "data:image/png;base64," + data;
    }

    static BufferedImage getImage() {
        int sz = 500;
        BufferedImage image = new BufferedImage(
                sz, sz, BufferedImage.TYPE_INT_ARGB);

        // paint the image..
        Graphics2D g = image.createGraphics();
        g.setRenderingHint(
                RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g.setColor(new Color(0,0,255,63));
        g.setStroke(new BasicStroke(1.5f));
        for (int ii = 0; ii < sz; ii += 5) {
            g.drawOval(ii, ii, sz - ii, sz - ii);
        }
        g.dispose();

        return image;
    }
    
    public static void main(String[] args) throws Exception {
        String imageString = getImageAsString(getImage());
        String htmlFrag = "<html><body><img src='%1s'></body></html>";
        String html = String.format(htmlFrag, imageString);

        // write the HTML
        File f = new File("image.html");
        FileWriter fw = new FileWriter(f);
        fw.write(html);
        fw.flush();
        fw.close();

        // display the HTML
        Desktop.getDesktop().open(f);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...