zoukankan      html  css  js  c++  java
  • 图片与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");
        }
    }
    
  • 相关阅读:
    游戏活动分析
    移动端页面设计指南
    20条开发AIR Native Extension的建议
    Feathers UI 扩展实例 For Starling Framework
    优化 Flash 性能 Flash 开发中心
    简单的不雅词语过滤类
    简单TSql备份所有数据库
    列出Server上5张最大的表
    Asp.net MVC RTM1.0使用NUnit做测试项目
    使用SingleTagSectionHandler实现简单配置节
  • 原文地址:https://www.cnblogs.com/z-qinfeng/p/13160315.html
Copyright © 2011-2022 走看看