2014-01-16 48 views
0

如何將base64編碼的圖像轉換爲.PNG(使用Java或JavaScript)?如何將編碼圖像轉換爲.PNG?

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

請注意,JavaScript不應該*需要*將基礎64編碼圖像轉換爲任何東西,它可以簡單地將數據設置爲'圖像'元素的src'並且圖像應該出現在網頁中。而對於Java,只需將字符串轉換爲輸入流並將該字節逐字節保存到文件。 –

回答

0

對於Java,您可以使用DatatypeConverter器解碼並保存無論它解碼,應該給你一個PNG文件,如果這就是你想要的。

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此鏈接,但它沒有最後的工作代碼。