首页 > 其他 > 详细

base64编码转图片,图片转base64编码

时间:2019-08-09 19:19:07      阅读:70      评论:0      收藏:0      [点我收藏+]

亲测可用:

代码:

 1 import sun.misc.BASE64Decoder;
 2 import sun.misc.BASE64Encoder;
 3 
 4 import java.io.*;
 5 
 6 public class Base64Util {
 7 
 8     /*把base64编码转换为图片*/
 9     public static boolean generateImage(String imgStr, String path) {
10         if (imgStr == null)
11             return false;
12         BASE64Decoder decoder = new BASE64Decoder();
13         try {
14             byte[] b = decoder.decodeBuffer(imgStr);
15             for (int i = 0; i < b.length; i++) {
16                 if (b[i] < 0) {
17                     b[i] += 256;
18                 }
19             }
20             OutputStream out = new FileOutputStream(path);
21             out.write(b);
22             out.flush();
23             out.close();
24             return true;
25         } catch (Exception e) {
26             e.printStackTrace();
27             return false;
28         }
29     }
30 
31     /*把图片转换为base64编码*/
32     public static String getImageStr(String imgFile) {
33         InputStream inputStream = null;
34         byte[] data = null;
35         try {
36             inputStream = new FileInputStream(imgFile);
37             data = new byte[inputStream.available()];
38             inputStream.read(data);
39             inputStream.close();
40         } catch (IOException e) {
41             e.printStackTrace();
42         }
43         BASE64Encoder encoder = new BASE64Encoder();
44         return encoder.encode(data);
45     }
46     /*此为以上方法的用法,可以忽略*/
47     /*public static void main(String[] args) {
48         *//*把图片转换为base64编码*//*
49         String strImg = getImageStr("这里传图片的路径:c:/users/图片.png");
50         System.out.println(strImg);
51         *//*把base64编码转换为图片*//*
52         generateImage(strImg,strImg*//*strImg这里是base64编码*//*);
53     }*/
54 }

 

base64编码转图片,图片转base64编码

原文:https://www.cnblogs.com/wangquanyi/p/11328907.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!