Java中将base64编码字符串转换为图片的代码

作者:Asurplus 时间:2023-07-14 12:23:51 

前一段时间,在做摄像头拍照上传,摄像头拍的照片为base64编码格式的字符串,需要上传至项目中,则需要使用到将base64编码字符串转换为图片

1、将base64编码字符串转换为图片的代码如下 ImageUtil.java:

package util;

import javax.servlet.http.HttpServletRequest;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Base64;
import java.util.UUID;

public class ImageUtil {

/**
* @Description: 将base64编码字符串转换为图片
* @Author:
* @CreateTime:
* @param file base64编码字符串
* @param path 图片路径-具体到文件
* @return
*/
public static String generateImage(String file, String path, HttpServletRequest request) {
// 解密
try {
// 项目绝对路径
String savePath = request.getSession().getServletContext().getRealPath("upload");
// 图片分类路径+图片名+图片后缀
String imgClassPath = path.concat(UUID.randomUUID().toString()).concat(".jpg");
// 解密
Base64.Decoder decoder = Base64.getDecoder();
// 去掉base64前缀 data:image/jpeg;base64,
file = file.substring(file.indexOf(",", 1) + 1, file.length());
byte[] b = decoder.decode(file);
// 处理数据
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {
b[i] += 256;
}
}
// 保存图片
OutputStream out = new FileOutputStream(savePath.concat(imgClassPath));
out.write(b);
out.flush();
out.close();
// 返回图片的相对路径 = 图片分类路径+图片名+图片后缀
return imgClassPath;
} catch (IOException e) {
return null;
}
}
}

补充:Java实现图片转base64字符串和图片互相转换

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.*;

/**
* @Description:
* @Author: Han
* @CreateDate: 2022/9/7
**/
public class Test010 {

public static void main(String[] args) {
       String base64Str = imageToBase64Str("D:\\SoftWare\\图片素材\\头像\\432.jpeg");
       System.out.println(base64Str);

boolean b = base64StrToImage(base64Str, "D:\\002.jpg");
       System.out.println(b);
   }

/**
    * 图片转base64字符串
    *
    * @param imgFile 图片路径
    * @return
    */
   public static String imageToBase64Str(String imgFile) {
       InputStream inputStream = null;
       byte[] data = null;
       try {
           inputStream = new FileInputStream(imgFile);
           data = new byte[inputStream.available()];
           inputStream.read(data);
           inputStream.close();
       } catch (IOException e) {
           e.printStackTrace();
       }
       // 加密
       BASE64Encoder encoder = new BASE64Encoder();
       return encoder.encode(data);
   }

/**
    * base64编码字符串转换为图片,并写入文件
    *
    * @param imgStr base64编码字符串
    * @param path   图片路径
    * @return
    */
   public static boolean base64StrToImage(String imgStr, String path) {
       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 tempFile = new File(path);
           if (!tempFile.getParentFile().exists()) {
               tempFile.getParentFile().mkdirs();
           }
           OutputStream out = new FileOutputStream(tempFile);
           out.write(b);
           out.flush();
           out.close();
           return true;
       } catch (Exception e) {
           return false;
       }
   }

}

来源:https://lizhou.blog.csdn.net/article/details/105065697

标签:Java,base64,编码,图片
0
投稿

猜你喜欢

  • SpringBoot 过滤器 Filter使用实例详解

    2021-11-08 13:46:00
  • 介绍Jersey-Jersey入门基础

    2023-11-18 06:34:26
  • JavaWeb实现用户登录与注册功能(服务器)

    2022-12-19 13:28:31
  • 基于spring @Cacheable 注解的spel表达式解析执行逻辑

    2023-07-03 19:46:45
  • 关于jdk9、jdk10、jdk11、jdk12、jdk13新特性说明

    2021-07-19 17:06:34
  • Spring Boot集成Redis实现缓存机制(从零开始学Spring Boot)

    2023-03-21 15:11:38
  • Android中FontMetrics的几个属性全面讲解

    2023-11-14 14:57:20
  • Android Touch事件分发过程详解

    2021-08-28 20:11:33
  • java应用占用内存过高排查的解决方案

    2023-09-21 12:47:30
  • 简单讲解java中throws与throw的区别

    2022-06-01 05:16:55
  • Java读取json数据并存入数据库的操作代码

    2023-09-23 06:00:57
  • C# JWT权限验证的实现

    2022-11-24 00:57:13
  • 使用游长编码对字符串压缩 Run Length编码示例

    2022-02-18 06:58:51
  • 创建Android守护进程实例(底层服务)

    2021-11-12 01:37:09
  • unity 切换场景不销毁物体问题的解决

    2022-04-29 11:26:06
  • Android编程之OpenGL绘图技巧总结

    2022-07-25 05:47:55
  • C#中的char、string和StringBuilder的使用详解

    2022-12-12 12:59:50
  • Android studio实现简单计算器的编写

    2022-08-21 05:58:55
  • Android init.rc文件详解及简单实例

    2023-08-02 08:55:09
  • SpringBoot 整合 ElasticSearch操作各种高级查询搜索

    2023-03-25 17:12:40
  • asp之家 软件编程 m.aspxhome.com