用Data Url (data:image/jpg;base64,)将小图片生成数据流形式
Posted 太阳花神
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用Data Url (data:image/jpg;base64,)将小图片生成数据流形式相关的知识,希望对你有一定的参考价值。
<img src="data:image/jpg;base64,iVBORw0KGgoAAAANSUhEUgAAAKAAAAAwCAMAAAChd4FcAAAAA3NCSVQICAjb4U/gAAACRlBMVEUAAADi6OSMjIxSUlIrXja5trg8rFEDRhErokJ8w4pJrVxmZmYERRIinTn++P07nk/X0Naww7Stra0pdTj37/dlunUXWiS53L+ZmZmOy5pahGJKdVIQEBAsXzdni24VmC7MzMw1kEYzMzPZ4Nul1K6Wr5tWs2jp8eseaC0ufz5mZmaEoorI1cszpUkXTSJErFix2bi1tbVKSkp7e3skWC9wvn5zlHq6yL3S3dSXzqJBcEspKSnY6dxCQkLI486mvKulpaVAbUkTSx8ICAgjcDIhISHh7eM6Ojonnz1QsGP///+Dxo9Re1paWlqNqZOZzJmasJ6s1rMZGRk4pkx5wYYcYyodmzViuHPFxcVEtFl5mH/V3def0qlhh2rZ7d7F2MiEhIQ4Z0Lv8+/F4csvo0QbUidAp1OEoIoqfDozZjOx27kOSho6mkw2j0iftaMyi0S2xroSUiBrvHuFx5J0v4Lm7+eNqJOc0KVBq1TU59gpoEBskXRzc3Pg2N6tubDe5d99nYRNr1+ZmZkbWyg9pFHN18779/patWuZzJkzZT6Sq5e0xbfAz8OtvbUhazC12ryJpY6+3sXD0MbE4MmJyJUgVCtSsmXQ5tWjuKd5wobd8OEfYy0ZUCS9vb3v7+8LRxgtgD0xZzw1j0cnWjJiimp2mX2An4aUtZye1qkYmjDm5ube3t7W1tbk9+fM5NA4lEombjRWfl9Ke1K2270xhUGbs6BrjnI7akSY1aSrvq+5yr1Kc0q8zL/19fVrvXMxYjt5mX+1vbVNR7MSAAAACXBIWXMAAAsSAAALEgHS3X78AAAAFnRFWHRDcmVhdGlvbiBUaW1lADAzLzI0LzEzWoVZMQAAABx0RVh0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gTvco3vyubgjsPkBJ38WOJa+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字符串之间的转换:
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import sun.misc.BASE64Decoder;
- import sun.misc.BASE64Encoder;
- public class Base64Test
- {
- public static void main(String[] args)
- {
- String strImg = GetImageStr();
- System.out.println(strImg);
- GenerateImage(strImg);
- }
- //图片转化成base64字符串
- public static String GetImageStr()
- {//将图片文件转化为字节数组字符串,并对其进行Base64编码处理
- String imgFile = "d://test.jpg";//待处理的图片
- InputStream in = null;
- byte[] data = null;
- //读取图片字节数组
- try
- {
- in = new FileInputStream(imgFile);
- data = new byte[in.available()];
- in.read(data);
- in.close();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- //对字节数组Base64编码
- BASE64Encoder encoder = new BASE64Encoder();
- return encoder.encode(data);//返回Base64编码过的字节数组字符串
- }
- //base64字符串转化成图片
- public static boolean GenerateImage(String imgStr)
- { //对字节数组字符串进行Base64解码并生成图片
- if (imgStr == null) //图像数据为空
- return false;
- BASE64Decoder decoder = new BASE64Decoder();
- try
- {
- //Base64解码
- byte[] b = decoder.decodeBuffer(imgStr);
- for(int i=0;i<b.length;++i)
- {
- if(b[i]<0)
- {//调整异常数据
- b[i]+=256;
- }
- }
- //生成jpeg图片
- String imgFilePath = "d://222.jpg";//新生成的图片
- OutputStream out = new FileOutputStream(imgFilePath);
- out.write(b);
- out.flush();
- out.close();
- return true;
- }
- catch (Exception e)
- {
- return false;
- }
- }
- }
以上是关于用Data Url (data:image/jpg;base64,)将小图片生成数据流形式的主要内容,如果未能解决你的问题,请参考以下文章