Java中实现图片和Base64的互相转化

前言
公司项目中用到了实名认证此,采用的第三方平台。后端中用到的单项功能为身份证信息人像对比功能,在写demo的过程中发现,它们所要求的图片信息为base64编码格式。

一、代码
package com.bajiao.wyq.tools.chuanglan;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Base64;

import javax.imageio.ImageIO;

public class ConvertImage {

/**
* 图片转Base64字符串
* @param imageFileName
* @return
*/
public static String convertImageToBase64Str(String imageFileName) {
ByteArrayOutputStream baos = null;
try {
//获取图片类型
String suffix = imageFileName.substring(imageFileName.lastIndexOf(".") + 1);
//构建文件
File imageFile = new File(imageFileName);
//通过ImageIO把文件读取成BufferedImage对象
BufferedImage bufferedImage = ImageIO.read(imageFile);
//构建字节数组输出流
baos = new ByteArrayOutputStream();
//写入流
ImageIO.write(bufferedImage, suffix, baos);
//通过字节数组流获取字节数组
byte[] bytes = baos.toByteArray();
//获取JDK8里的编码器Base64.Encoder转为base64字符
return Base64.getEncoder().encodeToString(bytes);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (baos != null) {
baos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}

/**
* Base64字符串转图片
* @param base64String
* @param imageFileName
*/
public static void convertBase64StrToImage(String base64String, String imageFileName) {
ByteArrayInputStream bais = null;
try {
//获取图片类型
String suffix = imageFileName.substring(imageFileName.lastIndexOf(".") + 1);
//获取JDK8里的解码器Base64.Decoder,将base64字符串转为字节数组
byte[] bytes = Base64.getDecoder().decode(base64String);
//构建字节数组输入流
bais = new ByteArrayInputStream(bytes);
//通过ImageIO把字节数组输入流转为BufferedImage
BufferedImage bufferedImage = ImageIO.read(bais);
//构建文件
File imageFile = new File(imageFileName);
//写入生成文件
ImageIO.write(bufferedImage, suffix, imageFile);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bais != null) {
bais.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
二、测试
public static void main(String[] args) {
System.out.println("----------------------------图片转Base64字符串---------------------------");
//图片文件路径
String imageFileName = "C:\\Users\\22.jpg";
//图片转Base64字符串
String base64Str = ConvertImage.convertImageToBase64Str(imageFileName);
System.out.println(base64Str);

System.out.println("----------------------------Base64字符串转图片---------------------------");
//新文件路径
String newFileName = "C:\\Users\\22.jpg";
//Base64字符串转图片
ConvertImage.convertBase64StrToImage(base64Str, newFileName);
System.out.println("生成的文件的路径是:"+newFileName);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

/**
* 创建父级文件夹
*
* @param file
* 完整路径文件名(注:不是文件夹)
*/
public static void createParentPath(File file) {
File parentFile = file.getParentFile();
if (null != parentFile && !parentFile.exists()) {
parentFile.mkdirs(); // 创建文件夹
createParentPath(parentFile); // 递归创建父级目录
}
}

/**
* 把原始字符串分割成指定长度的字符串列表
*
* @param inputString
* 原始字符串
* @param length
* 指定长度
* @return
*/
public static List<String> getStrList(String inputString, int length) {
int size = inputString.length() / length;
if (inputString.length() % length != 0) {
size += 1;
}
return getStrList(inputString, length, size);
}

/**
* 把原始字符串分割成指定长度的字符串列表
*
* @param inputString
* 原始字符串
* @param length
* 指定长度
* @param size
* 指定列表大小
* @return
*/
public static List<String> getStrList(String inputString, int length,
int size) {
List<String> list = new ArrayList<String>();
for (int index = 0; index < size; index++) {
String childStr = substring(inputString, index * length,
(index + 1) * length);
list.add(childStr);
}
return list;
}

/**
* 分割字符串,如果开始位置大于字符串长度,返回空
*
* @param str
* 原始字符串
* @param f
* 开始位置
* @param t
* 结束位置
* @return
*/
public static String substring(String str, int f, int t) {
if (f > str.length())
return null;
if (t > str.length()) {
return str.substring(f, str.length());
} else {
return str.substring(f, t);
}
}


文章知识
————————————————

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原文链接:https://blog.csdn.net/VIP_1205169154/article/details/132150930

posted @ 2024-06-07 17:30  枫树湾河桥  阅读(21)  评论(0编辑  收藏  举报
Live2D