【问题标题】:How to Decode base64 image encoded by window.btoa using Java如何使用 Java 解码由 window.btoa 编码的 base64 图像
【发布时间】:2016-08-20 03:33:40
【问题描述】:

我有一个 svg 图像,我想导出为 png。

在客户端使用 javascript,我将其转换为 base64

var s = new XMLSerializer().serializeToString(document.getElementById("svg"))
var encodedData = window.btoa(s);

在服务器端我想对其进行解码并创建一个 .png 文件

BASE64Decoder decoder = new BASE64Decoder();
byte[] imageByte = decoder.decodeBuffer(string);

但这给了我一个无法打开的文件。

或者有没有其他方法可以将 svg 导出为 png。我不能使用 toDataUrl,因为我的 svg 包含来自外部源的图像

【问题讨论】:

    标签: javascript java svg


    【解决方案1】:

    Java 8 支持 Base 64 编码和解码

    你可以这样做

    byte [] barr = Base64.getDecoder().decode(encoded); 
    

    但是,为什么要将它作为 base 64 编码字符串传递?为什么不将图像作为 multipart/form-data 发送?

    【讨论】:

    • 抱歉,我不知道如何将 svg 作为 multipart/form-data 传递。应该转成png再发送?
    • 文件格式无关紧要。这个答案包含一些关于如何在 java stackoverflow.com/questions/19510656/… 中进行文件上传的说明
    • 感谢您的回复。我正在使用 d3.js 绘制一个 svg,我想将它保存为 png 图像。由于我在 svg 中使用外部图像源,因此在尝试使用 toDataUrl 时出现 CORS 错误。您提供的参考对我没有帮助:(
    【解决方案2】:

    对于mime类型解码,使用这个,

    byte[] decodedBuffer = Base64.getMimeDecoder().decode( encodedBuffer );
    

    资源链接:https://examples.javacodegeeks.com/core-java/util/base64/java-8-base64-encoding-example/

    【讨论】:

    • @Jerry 并在您的问题中提供完整代码
    • 我试过是解码。但是当我创建一个文件时,它仍然是不受支持的文件 \n byte[] image = Base64.getMimeDecoder().decode( encodedBuffer ); String dir = System.getProperty("java.io.tmpdir"); String fileName = input.getShortName(); File file = new File(dir + File.separatorChar + fileName); FileOutputStream fop = new FileOutputStream(file); if (!file.exists()) { file.createNewFile(); } fop.write(image); fop.flush(); fop.close();
    • 对不起,这是我的错误。只有解码是正确的。获得 svg 文件后,我只是将扩展名更改为 png 并下载。现在我正在使用 batik 库进行 svg 到 png 的转换。它工作正常。 :)
    • @Jerry Nice。最后你得到了平息。感谢分享。
    【解决方案3】:

    解码后的字节数组将是 svg 格式。使用一些库将其转换为 png(我正在使用 apache batik transcoder

        BASE64Decoder decoder = new BASE64Decoder();
        byte[] image = decoder.decodeBuffer(string);
        String fileLocation  = "C:\temp";
        String fileName = "New-" + System.currentTimeMillis();
        File file = new File(fileLocation +File.separator+ fileName + ".svg");
        FileOutputStream fop = new FileOutputStream(file);
        if (!file.exists()) {
          file.createNewFile();
        }
        fop.write(image);
        fop.flush();
        fop.close();
        PNGTranscoder transcoder = new PNGTranscoder();
        TranscoderInput tinput = new TranscoderInput(new FileInputStream(fileLocation + File.separator + fileName +".svg"));
        OutputStream ostream = new FileOutputStream(fileLocation + File.separator + fileName +".png");
        TranscoderOutput toutput = new TranscoderOutput(ostream);
        try {
              transcoder.transcode(tinput, toutput);
        } catch (TranscoderException e) { 
              System.out.println("error*");
               e.printStackTrace();
        }
        ostream.close();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-31
      • 2015-10-25
      • 1970-01-01
      • 2016-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多