码迷,mamicode.com
首页 > Web开发 > 详细

用Data Url (data:image/jpg;base64,)将小图片生成数据流形式

时间:2018-05-10 17:14:26      阅读:382      评论:0      收藏:0      [点我收藏+]

标签:svc   word   hrp   aot   本地   ilb   span   CND   bec   

<img src="data:image/jpg;base64,iVBORw0KGgoAAAANSUhEUgAAAKAAAAAwCAMAAAChd4FcAAAAA3NCSVQICAjb4U/gAAACRlBMVEUAAADi6OSMjIxSUlIrXja5trg8rFEDRhErokJ8w4pJrVxmZmYERRIinTn++P07nk/X0Naww7Stra0pdTj37/dlunUXWiS53L+ZmZmOy5pahGJKdVIQEBAsXzdni24VmC7MzMw1kEYzMzPZ4Nul1K6Wr5tWs2jp8eseaC0ufz5mZmaEoorI1cszpUkXTSJErFix2bi1tbVKSkp7e3skWC9wvn5zlHq6yL3S3dSXzqJBcEspKSnY6dxCQkLI486mvKulpaVAbUkTSx8ICAgjcDIhISHh7eM6Ojonnz1QsGP///+Dxo9Re1paWlqNqZOZzJmasJ6s1rMZGRk4pkx5wYYcYyodmzViuHPFxcVEtFl5mH/V3def0qlhh2rZ7d7F2MiEhIQ4Z0Lv8+/F4csvo0QbUidAp1OEoIoqfDozZjOx27kOSho6mkw2j0iftaMyi0S2xroSUiBrvHuFx5J0v4Lm7+eNqJOc0KVBq1TU59gpoEBskXRzc3Pg2N6tubDe5d99nYRNr1+ZmZkbWyg9pFHN18779/patWuZzJkzZT6Sq5e0xbfAz8OtvbUhazC12ryJpY6+3sXD0MbE4MmJyJUgVCtSsmXQ5tWjuKd5wobd8OEfYy0ZUCS9vb3v7+8LRxgtgD0xZzw1j0cnWjJiimp2mX2An4aUtZye1qkYmjDm5ube3t7W1tbk9+fM5NA4lEombjRWfl9Ke1K2270xhUGbs6BrjnI7akSY1aSrvq+5yr1Kc0q8zL/19fVrvXMxYjt5mX+1vbVNR7MSAAAACXBIWXMAAAsSAAALEgHS3X78AAAAFnRFWHRDcmVhdGlvbiBUaW1lADAzLzI0LzEzWoVZMQAAABx0RV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6TgTvco3vyubgjsPkBJ38WOJa+ZV1l2D+QDYwOe9ikA4QjJ1xZyTUONOaYnMI8ZhM8jpxGTxhQA357jkmcMAucqQsN6YDF2Wj3OyjKYQrzRwFEk9bAxvsIL321/jy9smDEUG9BH36PgWcNkm95O++sBIDFCB4BYnYAwibIGiFHIFAREG3ANQ/QdcV4vtEE7I7Drm8lAaFQMTH2nrWZDfeL9CMw0DmQ+7JQB8ju7qrMpj2od4oxiMorIQN6DGhiMgB5ndOpM9nFpTrOg1qt0QQrHf7GRr8OhB1HXTwCodaJgzX8yl1BpxMRlTCUgNkYb88WhYw7nQD1J9bpTwOKjf+aCXQ7IOA9dsDxCNAoTqaAbgDdi/EaUvKICoqEFYHNfZYvpfhb7+MUdwzDXf4chw24Bhp1Th1eVenUVepgt1+l4LUBYOzy+3O6/CC7i5MAjSaBa3SgQw3A5emOHW75aHeFFQhzgdrCfUQcexp4rn5rAmIGlkZ+5VhDXHQvhTYwHkOhtsRxRZxQhB7Y+bj8b3wRjBGCEOelIQiKDCy6FCRsh1zCVKxRQ7BzIlBcmKHiWHHgm+VMf8BkYd2tg8l1DiD/QXkXSkvF0upGakisnXI5PBxfxiq/5Zdngdpbu32vcLM2kLvKHfZkv4Wp35NAkFVdccNdWlt8E1lt6cSjigv8J/SeRBARYfPWXF/n29zczr658cvwE3vTJJrUQHmeP9Iy3m9mB7zthJ/M287/AdxppEv2qVN4AAAAAElFTkSuQmCC" />

  就是一张图片的Data URL,利用base64编码把图片数据翻译成标准ASCII字符。

 相当于

<img src="http://www.oschina.net/img/logo_s2.png"/>

Data URL是在本地直接绘制图片,不是从服务器加载,所以节省了HTTP连接,起到加速网页的作用。
data:image/jpg;    声明数据协议及类型名称
base64,               编码形式为base64
/9j/4AAQSkZ……    base64编码结果

用base64转图片:http://base64.xpcha.com/pic.html

java实现图片与base64字符串之间的转换:
  1. import java.io.FileInputStream;  
  2. import java.io.FileOutputStream;  
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import java.io.OutputStream;  
  6.   
  7. import sun.misc.BASE64Decoder;  
  8. import sun.misc.BASE64Encoder;  
  9.   
  10. public class Base64Test   
  11. {  
  12.     public static void main(String[] args)  
  13.     {  
  14.         String strImg = GetImageStr();  
  15.         System.out.println(strImg);  
  16.         GenerateImage(strImg);  
  17.     }  
  18.     //图片转化成base64字符串  
  19.     public static String GetImageStr()  
  20.     {//将图片文件转化为字节数组字符串,并对其进行Base64编码处理  
  21.         String imgFile = "d://test.jpg";//待处理的图片  
  22.         InputStream in = null;  
  23.         byte[] data = null;  
  24.         //读取图片字节数组  
  25.         try   
  26.         {  
  27.             in = new FileInputStream(imgFile);          
  28.             data = new byte[in.available()];  
  29.             in.read(data);  
  30.             in.close();  
  31.         }   
  32.         catch (IOException e)   
  33.         {  
  34.             e.printStackTrace();  
  35.         }  
  36.         //对字节数组Base64编码  
  37.         BASE64Encoder encoder = new BASE64Encoder();  
  38.         return encoder.encode(data);//返回Base64编码过的字节数组字符串  
  39.     }  
  40.       
  41.     //base64字符串转化成图片  
  42.     public static boolean GenerateImage(String imgStr)  
  43.     {   //对字节数组字符串进行Base64解码并生成图片  
  44.         if (imgStr == null) //图像数据为空  
  45.             return false;  
  46.         BASE64Decoder decoder = new BASE64Decoder();  
  47.         try   
  48.         {  
  49.             //Base64解码  
  50.             byte[] b = decoder.decodeBuffer(imgStr);  
  51.             for(int i=0;i<b.length;++i)  
  52.             {  
  53.                 if(b[i]<0)  
  54.                 {//调整异常数据  
  55.                     b[i]+=256;  
  56.                 }  
  57.             }  
  58.             //生成jpeg图片  
  59.             String imgFilePath = "d://222.jpg";//新生成的图片  
  60.             OutputStream out = new FileOutputStream(imgFilePath);      
  61.             out.write(b);  
  62.             out.flush();  
  63.             out.close();  
  64.             return true;  
  65.         }   
  66.         catch (Exception e)   
  67.         {  
  68.             return false;  
  69.         }  
  70.     }  
  71. }  

用Data Url (data:image/jpg;base64,)将小图片生成数据流形式

标签:svc   word   hrp   aot   本地   ilb   span   CND   bec   

原文地址:https://www.cnblogs.com/yangxiaomei/p/9020099.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!