在Java中将Base64 jpg转换为Base64Tiff

时间:2019-02-18 05:18:26

标签: java image jpeg tiff

我有一个Base64 jpeg字符串。我希望它转换为Tiff并使Tiff作为Base64字符串。我正在使用JAI库。

所有其他答案都保存了Tiff。我不想将tiff保存到磁盘中。我只想使用Base64字符串。

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

将图像转换为任何格式的Base64表示的简单方法可以是:

var source = d3.select("body")
    .append("svg")
    .attr("height", this.node.clientHeight + this.node.clientHeight/10)
    .attr("width", this.node.clientWidth + 65*2 + this.node.clientWidth/20)
    .attr('version', 1.1)
    .attr('xmlns', 'http://www.w3.org/2000/svg')
    .html(d3.select(".svgaxisy").node().innerHTML+"<g transform='translate(65,10)'>"+d3.select(this.node).node().innerHTML+"</g>"+
        "<g transform='translate("+(this.node.clientWidth + ((this.node.clientWidth/this.nested_data.length)/20) + 65)+",0)'>"+d3.select(".svgaxisy2").node().innerHTML+"</g>");

saveSvgAsPng.saveSvgAsPng(source.node(), "graph.png");

针对您的具体情况,使用:

private static String toBase64(BufferedImage image, String formatName) throws IOException {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream(1024 * 1024);

    if (!ImageIO.write(image, formatName, bytes)) {
        throw new IIOException("Error writing " + formatName);
    }

    return Base64.getEncoder().encodeToString(bytes.toByteArray());
}
相关问题