如何将编码图像转换为.PNG?

时间:2014-01-16 05:00:50

标签: java javascript image base64

如何将base64编码的图像转换为.PNG(使用Java或JavaScript)?

url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABL0lEQVR42q2TLW/DMBCG+1OKworCzMzMAsMCQw0zaGgaahwaXKmsoxuaVD66X3G995Jzk/Rj0rSTXsW+j8cXf+x2/22X7wu1Y0cmVlQEK8IYPsReFiOpjI7CZyFK50gDKx4DtUObQU+LXV9zcifFMHz7o5/ViZBzB0FrsvLoGeDp9HUiNUDgWwq5q98BsUo1+aERLQEK0Zhq1QWIDQNUW4BCNN7Ne7UCuHDT+DHSI3v/eZM4ADidDNjzpOxM1iMAutJ4kzztlwDQDqHM2gIw15jlbmveg1UH2BDbV3xEk5YAjNXvUiWbbbdHiSM5RDsHbQbga6IVwQ8AVkfu3a2U69tP7aEQKqPhYkM2wV9LDDkvbyPoLk3JLV8sCGP4EHtavH1MRbT5MRVz4a+P6S92BSaqyCgd7GKfAAAAAElFTkSuQmCC")

2 个答案:

答案 0 :(得分:1)

在pom.xml中

<dependencies>
    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.9</version>
    </dependency>
</dependencies>

然后

public static void main(String[] args) throws IOException {
    String ecodedData = "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABL0lEQVR42q2TLW/DMBCG+1OKworCzMzMAsMCQw0zaGgaahwaXKmsoxuaVD66X3G995Jzk/Rj0rSTXsW+j8cXf+x2/22X7wu1Y0cmVlQEK8IYPsReFiOpjI7CZyFK50gDKx4DtUObQU+LXV9zcifFMHz7o5/ViZBzB0FrsvLoGeDp9HUiNUDgWwq5q98BsUo1+aERLQEK0Zhq1QWIDQNUW4BCNN7Ne7UCuHDT+DHSI3v/eZM4ADidDNjzpOxM1iMAutJ4kzztlwDQDqHM2gIw15jlbmveg1UH2BDbV3xEk5YAjNXvUiWbbbdHiSM5RDsHbQbga6IVwQ8AVkfu3a2U69tP7aEQKqPhYkM2wV9LDDkvbyPoLk3JLV8sCGP4EHtavH1MRbT5MRVz4a+P6S92BSaqyCgd7GKfAAAAAElFTkSuQmCC";
    Base64 decoder = new Base64();   
    byte[] imgBytes = decoder.decode(ecodedData);  
    FileOutputStream osf;
    osf = new FileOutputStream(new File("yourImage.png"));
    osf.write(imgBytes);  
    osf.flush();        
}

此链接使用http://www.coderanch.com/t/482256/java/java/Converting-Base-encoded-String-Image,但它没有最终的工作代码。

答案 1 :(得分:0)

对于Java,您可以使用DatatypeConverter对其进行解码并保存它解码的任何内容,如果这就是您想要的,那么应该为您提供PNG文件。