<img src="data:image/gif;base64,R0lGODlhDwAPAKECAAAAzMzM///// wAAACwAAAAADwAPAAACIISPeQHsrZ5ModrLlN48CXF8m2iQ3YmmKqVlRtW4ML wWACH+H09wdGltaXplZCBieSBVbGVhZCBTbWFydFNhdmVyIQAAOw==" alt="Base64 encoded image" width="150" height="150"/>这种data: URI的格式能把Base64(或其他数据)可以内嵌在image标签的属性当中(或者CSS中)。我们可以看到在大部分浏览器中的显示效果:
这种做法有利有弊,好处是浏览器可以在一个连接中得到完成的页面内容,不好的地方时图像的大小会增加1/3。因此,这种内嵌的方法适合对小的图形元素比如图标、圆角等等进行处理,从而减少浏览器打开的连接数,但对大的照片、图片(量少而大)等等则不应该使用Base64编码以免影响下载速度。
为了得到刚才的Base64编码,我们将上一篇的Java修改成Struts Action,并借用了JIMI进行图形的读取和格式转换,Base64编码器则改为更普遍的Apache Commons组件,代码如下:
Java代码
public class Base64ImageAction extends ActionSupport { private final static String galleryName = "gallery";private static String parent = null;private String encodeString = null;public String getEncodeString() { return encodeString;}public void setEncodeString(String encodeString) { this.encodeString = encodeString;}private String getImageFullPath() { parent = new File(this.getClass().getClassLoader().getResource(File.separator).getPath()).getParent()+File.separator+"flag.jpg";}public String execute() { ByteArrayOutputStream output = new ByteArrayOutputStream(); try {JimiReader reader = Jimi.createJimiReader(this.getImageFullPath());Image image = reader.getImage();Jimi.putImage("image/png", image, output);output.flush();output.close();this.encodeString = Base64.encodeBase64String(output.toByteArray()); } catch (IOException e) {e.printStackTrace(); } catch (JimiException e) {e.printStackTrace(); } return SUCCESS;} }对应的View端是个十分简单的Freemarker模板:
<html> <head> <title>Hello,World</title> </head> <body> <img src="data:image/png;base64,${encodeString}" /> </body> </html>处理古代浏览器
// a regular expression to test for Base64 data var BASE64_DATA = /^data:.*;base64/i; // path to the PHP module that will decode the encoded data var base64Path = "/my/path/base64.php"; function fixBase64(img) {// check the image sourceif (BASE64_DATA.test(img.src)) {// pass the data to the PHP routineimg.src = base64Path + "?" + img.src.slice(5);} }; // fix images on page load onload = function() {for (var i = 0; i < document.images.length; i++) {fixBase64(document.images[i]);} };服务器端的Struts可以参考上面的例子做反向操作,具体从略。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head><title>Demo JavaScript PNG Viewer</title></head> <body onload="show(gravatar);"> <script src="../Source/Base64.js" type="text/javascript"></script> <script src="../Source/Deflate.js" type="text/javascript"></script> <script src="../Source/PNG.js" type="text/javascript"></script> <script type="text/javascript"> var gravatar = "iVBORw0KGgoAAAANSUhEUgAAA.......数据从略......55CYII="; String.prototype.padRight = function(c, n){var txt = "";for(var i=0;i<n-this.length;i++) txt += c;return txt + this; }; function show(data){var png = new PNG(data);var img = document.getElementById("image"), limg = document.getElementById("largeimage");document.getElementById("nativeimage").src = "data:image/png;base64," + data;img.innerHTML = "";limg.innerHTML = "";img.style.width = png.width + "px";img.style.height = png.height + "px";limg.style.width = (png.width * 3) + "px";limg.style.width = (png.height * 3) + "px";var line;while(line = png.readLine()){ for (var x = 0; x < line.length; x++){var px = document.createElement("div"), px2 = document.createElement("div");px.className = px2.className = "pixel";px.style.backgroundColor = px2.style.backgroundColor = "#" + line[x].toString(16).padRight("0", 6);img.appendChild(px);limg.appendChild(px2); }} } </script> <div id="image"></div> <div id="largeimage"></div> <img id="nativeimage" /> </body> </html>以上所述是小编给大家介绍的JS在浏览器中解析Base64编码图像,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!