• base642photo


    /**
         * pic to base64Str
         * @param path 读取路径
         * @return
         */
        public static String GetImageStr(String path) {//转化成base64字符串
            String imgFile = path;
            InputStream in = null;
            byte[] data = null;
            try {
                in = new FileInputStream(imgFile);
                data = new byte[in.available()];
                in.read(data);
                in.close();
                BASE64Encoder encoder = new BASE64Encoder();
                return encoder.encode(data);// 返回Base64编码过的字节数组字符串
            } catch (IOException e) {
                logger.error("图片转换base64字符串出错"+e.getMessage());
            }
            return "";
        }
        /**
         *  base64Str to pic
         * @param imgStr base64 字符串
         * @param path 存储路径
         * @return
         */
        public static boolean GenerateImage(String imgStr,String path) { // 对字节数组字符串进行Base64解码并生成
            if (imgStr == null) {
                return false;
            }
            BASE64Decoder decoder = new BASE64Decoder();
            try {
                byte[] b = decoder.decodeBuffer(imgStr);
                for (int i = 0; i < b.length; ++i) {
                    if (b[i] < 0) {// 调整异常数据
                        b[i] += 256;
                    }
                }
                File file = new File(path);
                if (!file.getParentFile().exists()){
                    file.getParentFile().mkdirs();
                }

                OutputStream out = new FileOutputStream(new File(path));
                out.write(b);
                out.flush();
                out.close();
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }

  • 相关阅读:
    django 2.2, celery 4.3,出现 kombu.exceptions.EncodeError: cannot serialize '_io.BufferedReader' object 的分析解决
    python 算法
    zabbix--api学习之路--get_hostgroup获取
    zabbix-api学习之路--auth获取
    短链(ShortURL)的Java实现
    几种I/O模型功能和性能对比
    JMeter压测Rest请求
    git如何设置账号密码
    TCP和UDP之间的区别和联系
    Java BigDecimal和double
  • 原文地址:https://www.cnblogs.com/zsqfightyourway/p/9449303.html
Copyright © 2020-2023  润新知