当前位置: 首页 > news >正文

Java将图片转换成Base64字符串

Java将图片转换成Base64字符串

将Base64字符串转换成二维码。

public class ImageUtil {

    /**
     * 本地图片转换成base64字符串
     * @param imgFile
     *            图片本地路径
     * @return
     */
    public static String ImageToBase64ByLocal(String imgFile) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
        InputStream in = null;
        byte[] data = null;
        // 读取图片字节数组
        try {
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);// 返回Base64编码过的字节数组字符串
    }

    /**
     * base64字符串转换成图片
     * @param imgStr
     *            base64字符串
     * @param imgFilePath
     *            图片存放路径
     * @return
     */
    public static boolean Base64ToImage(String imgStr, String imgFilePath) { // 对字节数组字符串进行Base64解码并生成图片
        if (StringUtil.isNullOrEmpty(imgStr)) { // 图像数据为空
            return false;
        }
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // Base64解码
            byte[] b = decoder.decodeBuffer(imgStr);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {// 调整异常数据
                    b[i] += 256;
                }
            }
            OutputStream out = new FileOutputStream(imgFilePath);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    
    public static byte[] Base64ToByte(String imgStr) {
        if (StringUtil.isNullOrEmpty(imgStr)) { // 图像数据为空
            return null;
        }
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // Base64解码
            byte[] b = decoder.decodeBuffer(imgStr);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {// 调整异常数据
                    b[i] += 256;
                }
            }
            return b;
        } catch (Exception e) {
            return null;
        }
    }
    
    public static InputStream Base64ToInputStream(String imgStr) {
        byte[] b = Base64ToByte(imgStr);
        if(null == b) {
            return null;
        }
        ByteArrayInputStream bais = new ByteArrayInputStream(b);
        return bais;
    }
    
    public static String handleBase64Str(String base64Str) {
        String markStr = "base64,";
        int indexOf = base64Str.indexOf(markStr);
        if(indexOf != -1) {
            return base64Str.substring(indexOf + (markStr.length()));
        }
        return base64Str;
    }
    
}

 

作者:Se7end

声明:本博客文章为原创,只代表本人在工作学习中某一时间内总结的观点或结论。转载时请在文章页面明显位置给出原文链接。

转载于:https://www.cnblogs.com/se7end/p/9598742.html

相关文章:

  • MyBatis原理-拦截器
  • Django项目 第一课 【nvm、node、npm安装及使用】
  • 牛客网暑期ACM多校训练营(第三场) H Diff-prime Pairs(欧拉筛法)
  • CF 1036 B Diagonal Walking v.2 —— 思路
  • 系统完整性检查工具--Tripwire和AIDE
  • tp5 路由定义
  • 随机图片
  • Vue框架的两种使用方式
  • WPF的x:名称空间
  • 15 个 Android 通用流行框架大全
  • BZOJ1926: [Sdoi2010]粟粟的书架
  • php 进行跨域操作
  • 定义和实现相同的顺序
  • 编程语言分类
  • 关于win10下JDK环境变量的配置以及关于JDK的一些说明
  • 【干货分享】SpringCloud微服务架构分布式组件如何共享session对象
  • Babel配置的不完全指南
  • CentOS学习笔记 - 12. Nginx搭建Centos7.5远程repo
  • JavaScript设计模式系列一:工厂模式
  • log4j2输出到kafka
  • Spring-boot 启动时碰到的错误
  • SpringCloud(第 039 篇)链接Mysql数据库,通过JpaRepository编写数据库访问
  • SwizzleMethod 黑魔法
  • 前端技术周刊 2019-02-11 Serverless
  • 使用Gradle第一次构建Java程序
  • 一起来学SpringBoot | 第十篇:使用Spring Cache集成Redis
  • 浅谈sql中的in与not in,exists与not exists的区别
  • ​直流电和交流电有什么区别为什么这个时候又要变成直流电呢?交流转换到直流(整流器)直流变交流(逆变器)​
  • (10)Linux冯诺依曼结构操作系统的再次理解
  • (3)llvm ir转换过程
  • (C++17) optional的使用
  • (PWM呼吸灯)合泰开发板HT66F2390-----点灯大师
  • (SpringBoot)第二章:Spring创建和使用
  • (超简单)使用vuepress搭建自己的博客并部署到github pages上
  • (二开)Flink 修改源码拓展 SQL 语法
  • (翻译)terry crowley: 写给程序员
  • (附源码)python房屋租赁管理系统 毕业设计 745613
  • (附源码)spring boot车辆管理系统 毕业设计 031034
  • (附源码)springboot青少年公共卫生教育平台 毕业设计 643214
  • (官网安装) 基于CentOS 7安装MangoDB和MangoDB Shell
  • (转)3D模板阴影原理
  • .[backups@airmail.cc].faust勒索病毒的最新威胁:如何恢复您的数据?
  • .Net CF下精确的计时器
  • .net Stream篇(六)
  • .NET 使用 JustAssembly 比较两个不同版本程序集的 API 变化
  • .Net 应用中使用dot trace进行性能诊断
  • .net 怎么循环得到数组里的值_关于js数组
  • .net经典笔试题
  • .net流程开发平台的一些难点(1)
  • @FeignClient 调用另一个服务的test环境,实际上却调用了另一个环境testone的接口,这其中牵扯到k8s容器外容器内的问题,注册到eureka上的是容器外的旧版本...
  • @ModelAttribute使用详解
  • [2018][note]用于超快偏振开关和动态光束分裂的all-optical有源THz超表——
  • [BZOJ2850]巧克力王国
  • [error] 17755#0: *58522 readv() failed (104: Connection reset by peer) while reading upstream
  • [ESP32 IDF]web server