• 图片与base64互转


    
    import sun.misc.BASE64Decoder;
    import sun.misc.BASE64Encoder;
    
    import java.io.*;
    
    /**
     * @Author ZhengQinfeng
     * @Date 2020/6/18 21:31
     * @dec
     */
    public class ImageBase64 {
        /**
         * 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
         *
         * @param imgPath 图片文件
         * @return base64转码字符串
         */
        public static String GetImageStr(String imgPath) {//
            String imgFile = imgPath;// 待处理的图片
            InputStream in = null;
            byte[] data = null;
            String encode = null; // 返回Base64编码过的字节数组字符串
            // 对字节数组Base64编码
            BASE64Encoder encoder = new BASE64Encoder();
            try {
                // 读取图片字节数组
                in = new FileInputStream(imgFile);
                data = new byte[in.available()];
                in.read(data);
                encode = encoder.encode(data);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return encode;
        }
    
        /**
         * 对字节数组字符串进行Base64解码并生成图片.
         * 使用前端框vant上传图片,转码成base64之后, 需要截取掉"data:image/jpeg;base64,", 不然会失败
         *
         * @param imgData     base64字符串
         * @param imgFilePath 生成的图片文件
         */
    
        public static void GenerateImage(String imgData, String imgFilePath) { //
            if (imgData == null) {// // 图像数据为空
                return;
            }
            BASE64Decoder decoder = new BASE64Decoder();
            OutputStream out = null;
            try {
                out = new FileOutputStream(imgFilePath);
                // Base64解码
                byte[] b = decoder.decodeBuffer(imgData);
                for (int i = 0; i < b.length; ++i) {
                    if (b[i] < 0) {// 调整异常数据
                        b[i] += 256;
                    }
                }
                out.write(b);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    out.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
        public static void main(String[] args) throws IOException {
            String s = GetImageStr("D:\app\image\180817_37bdi0ji0j6bc58jb3j616b71l1dj_640x960.jpg");
            System.out.println(s);
            GenerateImage(s, "D:\app\image\1111.jpg");
        }
    }
    
  • 相关阅读:
    redis集群的测试
    nginx+tomcat集群
    linux 端口占用查看 netstat -tunpl | grep 6379
    Openfire 的安装和配置
    openfire自定义发送消息
    druid+spring配置
    java Servlet Filter 拦截Ajax请求,统一处理session超时的问题
    Tomcat8安装, 安全配置与性能优化
    ssh 免密码登录【用】
    SSH-KeyGen 的用法
  • 原文地址:https://www.cnblogs.com/z-qinfeng/p/13160315.html
Copyright © 2020-2023  润新知