数据URI - 如何用Java创建它们?

时间:2013-02-21 07:28:01

标签: java image data-uri

我刚被告知使用数据URI发送图像的缩略图。我一直在搜索它,但我发现它基本上是文件的文本表示,可以直接在HTML中使用。我无法真正找到如何在Java中创建数据URI。我有一个文件的输入流。有人可以对此有所了解并指出我的方法来产生这个吗?

2 个答案:

答案 0 :(得分:16)

E.G。对于图像:

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());

实施例

运行以下代码。如果FF是默认浏览器,您可能会看到如下内容:

Data URI image in FF

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.xml.bind.DatatypeConverter;
import javax.imageio.ImageIO;
import java.io.ByteArrayOutputStream;
import java.io.*;

public class DataUriConverter {

    public static void main(String[] args) throws Exception {
        int sz = 200;
        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(Color.BLUE);
        for (int ii = 0; ii < sz; ii += 5) {
            g.drawOval(ii, ii, sz - ii, sz - ii);
        }
        g.dispose();

        // convert the image
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(image, "png", baos);
        System.out.println("baos.toByteArray() " + baos.toByteArray());
        System.out.println("baos.toByteArray().length " + baos.toByteArray().length);
        String data = DatatypeConverter.printBase64Binary(baos.toByteArray());
        String imageString = "data:image/png;base64," + data;
        String html =
                "<html><body><img src='" + imageString + "'></body></html>";

        // 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);
    }
}

答案 1 :(得分:1)

这是我的例子。

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

import javax.xml.bind.DatatypeConverter;

public class ToDataURI {

    public static void main(String[] args) throws IOException {

        // source file
        File file = new File("movie.mp4");

        // check content type of the file
        String contentType = Files.probeContentType(file.toPath());

        // read data as byte[]
        byte[] data = Files.readAllBytes(file.toPath());

        // convert byte[] to base64(java7)
        String base64str = DatatypeConverter.printBase64Binary(data);

        // convert byte[] to base64(java8)
        // String base64str = Base64.getEncoder().encodeToString(data);

        // cretate "data URI"
        StringBuilder sb = new StringBuilder();
        sb.append("data:");
        sb.append(contentType);
        sb.append(";base64,");
        sb.append(base64str);

        System.out.println(sb.toString());

    }
}

处理流程

  1. 检查文件contentType
  2. 将文件数据读入byte []
  3. 将byte []数据转换为base64
  4. 创建&#34;数据URI&#34;格式
  5. 你可以像

    一样
    data:video/mp4;base64,AAAAIGZ0eXBpc29tAAACAGlzb21p....