图片Base64编码

概念:

Base64加密方式是将三个八位的字节转化为四个六位的字节(不足八位的高位补00),3*8 = 4*6;,所以base64加密过后的内容比原来的大三分之一;

举例:加密“ace”,

ace转化为二进制为:‭01100001‬ ‭01100011‬ ‭01100101‬

转化为base64的四字节六位:011000 01‬‭0110 0011‬01 100101‬

那因为计算机是一字节八位的存数,所以高位补00后变为:00011000 0001‬‭0110 000011‬01 00100101‬

转化为十进制:24 22 13 37

查Base64对照表(默认版本RFC2045):我们得到最终结果:YWNl

 

 

代码例子:

 1 package base64;
 2  
 3 import java.io.FileInputStream;
 4 import java.io.FileOutputStream;
 5 import java.io.IOException;
 6 import java.io.InputStream;
 7 import java.io.OutputStream;
 8  
 9 import Decoder.BASE64Decoder;
10 import Decoder.BASE64Encoder;
11  
12 public class base64Change {
13     public static void main(String[] args) {
14         //这一步很重要很重要很重要,因为base64的数据会有data:base64img,
15         String str = "iVBORw0KGgoAAAANSUhEUgAAAXcAAAGQCAMAAABiX9D2AAAAAXNSR0IB2cksfwAAAchQTFRFAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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4QCaLxVFmuqwB1bnXEI4rxU57Wv4+huqy8uQfhMaLkiHo0u+3O2+uISxDn1v7Ve3Igq+3VLPXGMQBDNBPDnosmePhekaESM6NthLYLsT9vog4sEd4npB+o+1A4dftdCD9yEWEj+58Ajum+/K7hMu5DvGymNsS4BAxba7yq0o+m1bt33K6wzY2n5VhCG8NH3te3lH56YPvvf7x432mznYRyEFL3gKcrbuwO7MDR8J8VB1mZhhmvk1L0/3ofkOjCT8Gvtbk9W6Kd1T3fCnQ6iSOv+crvbkxWYbAyw0SYEvaBx53ZU56GSbF5od2uyAznR6EYSgOiQE83/2t2YLHE52OO3uykAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA8fk/Tmi9wqi3qsEAAAAASUVORK5CYII=";
16         generateImage(str,"F:\\tupian\\tupian.png");
17         
18     }
19      /**
20      * @param imgStr base64编码字符串
21      * @param path   图片路径-具体到文件
22      */
23     public static boolean generateImage(String imgStr, String path) {
24         if (imgStr == null)
25             return false;
26         BASE64Decoder decoder = new BASE64Decoder();
27         try {
28 // 解密
29             byte[] b = decoder.decodeBuffer(imgStr);
30 // 处理数据
31             for (int i = 0; i < b.length; ++i) {
32                 if (b[i] < 0) {
33                     b[i] += 256;
34                 }
35             }
36             OutputStream out = new FileOutputStream(path);
37             out.write(b);
38             out.flush();
39             out.close();
40             return true;
41         } catch (Exception e) {
42             
43             System.out.println(e);
44             return false;
45         }
46     }
47     //图片转化成base64字符串  
48     public static String GetImageStr()  
49     {//将图片文件转化为字节数组字符串,并对其进行Base64编码处理  
50         String imgFile = "F:\\tupian\\a.jpg";//待处理的图片
51                          // 地址也有写成"F:/deskBG/86619-107.jpg"形式的
52         InputStream in = null;  
53         byte[] data = null;  
54         //读取图片字节数组  
55         try   
56         {  
57             in = new FileInputStream(imgFile);          
58             data = new byte[in.available()];  
59             in.read(data);  
60             in.close();  
61         }   
62         catch (IOException e)   
63         {  
64             e.printStackTrace();  
65         }  
66         //对字节数组Base64编码  
67         BASE64Encoder encoder = new BASE64Encoder();  
68         return encoder.encode(data);//返回Base64编码过的字节数组字符串  
69     }
70     
71 }

 

posted @ 2021-12-10 17:03  低调的小白  阅读(3078)  评论(0编辑  收藏  举报