|
@@ -0,0 +1,379 @@
|
|
|
+package com.huimv.process.utils;
|
|
|
+
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import sun.misc.BASE64Decoder;
|
|
|
+import sun.misc.BASE64Encoder;
|
|
|
+
|
|
|
+import java.io.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Project : huimv.shiwan
|
|
|
+ * @Package : com.huimv.biosafety.uface.controller
|
|
|
+ * @Description : TODO
|
|
|
+ * @Version : 1.0
|
|
|
+ * @Author : ZhuoNing
|
|
|
+ * @Create : 2020-12-25
|
|
|
+ **/
|
|
|
+@Component
|
|
|
+@Slf4j
|
|
|
+public class PictureUtil {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Method : Base64ToImage
|
|
|
+ * @Description :
|
|
|
+ * @Params : [imgStr, imgFilePath]
|
|
|
+ * @Return : boolean
|
|
|
+ * @Author : ZhuoNing
|
|
|
+ * @Date : 2021/1/20
|
|
|
+ * @Time : 13:51
|
|
|
+ */
|
|
|
+ public boolean base64ToImage(String imageBase64, String imgFilePath) { // 对字节数组字符串进行Base64解码并生成图片
|
|
|
+ if (imageBase64 == null) {
|
|
|
+ // 图像数据为空
|
|
|
+ log.error("当前图像数据为空");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ imageBase64 = imageBase64.replace("data:image/jpg;base64,","");
|
|
|
+ BASE64Decoder decoder = new BASE64Decoder();
|
|
|
+ try {
|
|
|
+ // Base64解码
|
|
|
+ byte[] b = decoder.decodeBuffer(imageBase64);
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param imgStr base64编码字符串
|
|
|
+ * @param path 图片路径-具体到文件
|
|
|
+ * @return
|
|
|
+ * @Description: 将base64编码字符串转换为图片
|
|
|
+ * @Author:
|
|
|
+ * @CreateTime:
|
|
|
+ */
|
|
|
+ public boolean generateImage(String imgStr, String path) {
|
|
|
+ if (imgStr == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ imgStr = imgStr.replace("image/jpg;base64,", "");
|
|
|
+
|
|
|
+ System.out.println(">>" + imgStr);
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ OutputStream out = new FileOutputStream(path);
|
|
|
+ out.write(b);
|
|
|
+ out.flush();
|
|
|
+ out.close();
|
|
|
+ return true;
|
|
|
+ } catch (Exception e) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return
|
|
|
+ * @Description: 根据图片地址转换为base64编码字符串
|
|
|
+ * @Author:
|
|
|
+ * @CreateTime:
|
|
|
+ */
|
|
|
+// public String getImageStr(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);
|
|
|
+// }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 本地文件(图片、excel等)转换成Base64字符串
|
|
|
+ *
|
|
|
+ * @param imgPath
|
|
|
+ */
|
|
|
+ public String convertFileToBase64(String imgPath) {
|
|
|
+ byte[] data = null;
|
|
|
+ // 读取图片字节数组
|
|
|
+ try {
|
|
|
+ InputStream in = new FileInputStream(imgPath);
|
|
|
+ System.out.println("文件大小(字节)=" + in.available());
|
|
|
+ data = new byte[in.available()];
|
|
|
+ in.read(data);
|
|
|
+ in.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ // 对字节数组进行Base64编码,得到Base64编码的字符串
|
|
|
+ BASE64Encoder encoder = new BASE64Encoder();
|
|
|
+ String base64Str = encoder.encode(data);
|
|
|
+ return base64Str;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将base64字符串,生成文件
|
|
|
+ */
|
|
|
+ public File convertBase64ToFile(String fileBase64String, String filePath, String fileName) {
|
|
|
+ BufferedOutputStream bos = null;
|
|
|
+ FileOutputStream fos = null;
|
|
|
+ File file = null;
|
|
|
+ try {
|
|
|
+ File dir = new File(filePath);
|
|
|
+ if (!dir.exists() && dir.isDirectory()) {//判断文件目录是否存在
|
|
|
+ dir.mkdirs();
|
|
|
+ }
|
|
|
+
|
|
|
+ BASE64Decoder decoder = new BASE64Decoder();
|
|
|
+ byte[] bfile = decoder.decodeBuffer(fileBase64String);
|
|
|
+
|
|
|
+ file = new File(filePath + File.separator + fileName);
|
|
|
+ fos = new FileOutputStream(file);
|
|
|
+ bos = new BufferedOutputStream(fos);
|
|
|
+ bos.write(bfile);
|
|
|
+ return file;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ } finally {
|
|
|
+ if (bos != null) {
|
|
|
+ try {
|
|
|
+ bos.close();
|
|
|
+ } catch (IOException e1) {
|
|
|
+ e1.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (fos != null) {
|
|
|
+ try {
|
|
|
+ fos.close();
|
|
|
+ } catch (IOException e1) {
|
|
|
+ e1.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * @Method : savePicture1
|
|
|
+ * @Description :
|
|
|
+ * @Params : [name, image_base64]
|
|
|
+ * @Return : void
|
|
|
+ * @Author : ZhuoNing
|
|
|
+ * @Date : 2021/1/20
|
|
|
+ * @Time : 12:13
|
|
|
+ */
|
|
|
+// public void savePicture1(String name, String image_base64) {
|
|
|
+// try {
|
|
|
+//// System.out.println("===imgStr.length()====>" + image.length()
|
|
|
+//// + "=====imgStr=====>" + image);
|
|
|
+// } catch (Exception e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+// int stateInt = 1;
|
|
|
+// if (image_base64 != null && image_base64.length() > 0) {
|
|
|
+// try {
|
|
|
+//
|
|
|
+// // 将字符串转换成二进制,用于显示图片
|
|
|
+// // 将上面生成的图片格式字符串 imgStr,还原成图片显示
|
|
|
+// byte[] imgByte = hex2byte(image_base64);
|
|
|
+// System.out.println("imgByte 输出>>" + imgByte);
|
|
|
+// InputStream in = new ByteArrayInputStream(imgByte);
|
|
|
+// System.out.println();
|
|
|
+// String imgPath = "D:\\pic\\";
|
|
|
+// File file = new File(imgPath, image_base64); //可以是任何图片格式.jpg,.png等
|
|
|
+// FileOutputStream fos = new FileOutputStream(file);
|
|
|
+//
|
|
|
+// byte[] b = new byte[1024];
|
|
|
+// int nRead = 0;
|
|
|
+// while ((nRead = in.read(b)) != -1) {
|
|
|
+// fos.write(b, 0, nRead);
|
|
|
+// }
|
|
|
+// fos.flush();
|
|
|
+// fos.close();
|
|
|
+// in.close();
|
|
|
+//
|
|
|
+// } catch (Exception e) {
|
|
|
+// stateInt = 0;
|
|
|
+// e.printStackTrace();
|
|
|
+// } finally {
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将二进制转换成图片保存
|
|
|
+ // * @param imgStr 二进制流转换的字符串
|
|
|
+ * @param imgPath 图片的保存路径
|
|
|
+ * @param imgName 图片的名称
|
|
|
+ * @return
|
|
|
+ * 1:保存正常
|
|
|
+ * 0:保存失败
|
|
|
+ */
|
|
|
+// public static int saveToImgByBytes(File imgFile,String imgPath,String imgName){
|
|
|
+//
|
|
|
+// int stateInt = 1;
|
|
|
+// if(imgFile.length() > 0){
|
|
|
+// try {
|
|
|
+// File file=new File(imgPath,imgName);//可以是任何图片格式.jpg,.png等
|
|
|
+// FileOutputStream fos=new FileOutputStream(file);
|
|
|
+//
|
|
|
+// FileInputStream fis = new FileInputStream(imgFile);
|
|
|
+//
|
|
|
+// byte[] b = new byte[1024];
|
|
|
+// int nRead = 0;
|
|
|
+// while ((nRead = fis.read(b)) != -1) {
|
|
|
+// fos.write(b, 0, nRead);
|
|
|
+// }
|
|
|
+// fos.flush();
|
|
|
+// fos.close();
|
|
|
+// fis.close();
|
|
|
+//
|
|
|
+// } catch (Exception e) {
|
|
|
+// stateInt = 0;
|
|
|
+// e.printStackTrace();
|
|
|
+// } finally {
|
|
|
+// }
|
|
|
+// }
|
|
|
+// return stateInt;
|
|
|
+// }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 二进制转字符串
|
|
|
+ * @param b
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+// public static String byte2hex(byte[] b) // 二进制转字符串
|
|
|
+// {
|
|
|
+// StringBuffer sb = new StringBuffer();
|
|
|
+// String stmp = "";
|
|
|
+// for (int n = 0; n < b.length; n++) {
|
|
|
+// stmp = Integer.toHexString(b[n] & 0XFF);
|
|
|
+// if (stmp.length() == 1) {
|
|
|
+// sb.append("0" + stmp);
|
|
|
+// } else {
|
|
|
+// sb.append(stmp);
|
|
|
+// }
|
|
|
+//
|
|
|
+// }
|
|
|
+// return sb.toString();
|
|
|
+// }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 字符串转二进制
|
|
|
+ *
|
|
|
+ * @param str 要转换的字符串
|
|
|
+ * @return 转换后的二进制数组
|
|
|
+ */
|
|
|
+// public static byte[] hex2byte(String str) { // 字符串转二进制
|
|
|
+// if (str == null) {
|
|
|
+// return null;
|
|
|
+// }
|
|
|
+// str = str.trim();
|
|
|
+// int len = str.length();
|
|
|
+// if (len == 0 || len % 2 == 1) {
|
|
|
+// return null;
|
|
|
+// }
|
|
|
+// byte[] b = new byte[len / 2];
|
|
|
+// try {
|
|
|
+// for (int i = 0; i < str.length(); i += 2) {
|
|
|
+// b[i / 2] = (byte) Integer
|
|
|
+// .decode("0X" + str.substring(i, i + 2)).intValue();
|
|
|
+// }
|
|
|
+// return b;
|
|
|
+// } catch (Exception e) {
|
|
|
+// return null;
|
|
|
+// }
|
|
|
+// }
|
|
|
+ ////////////////////////////////////////
|
|
|
+// public boolean savePicture2(String imgData, String imgFilePath) throws IOException { // 对字节数组字符串进行Base64解码并生成图片
|
|
|
+// if (imgData == null) { // 图像数据为空
|
|
|
+// return false;
|
|
|
+// }
|
|
|
+// 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 (FileNotFoundException e) {
|
|
|
+// // TODO Auto-generated catch block
|
|
|
+// e.printStackTrace();
|
|
|
+// } catch (IOException e) {
|
|
|
+// // TODO Auto-generated catch block
|
|
|
+// e.printStackTrace();
|
|
|
+// } finally {
|
|
|
+// out.flush();
|
|
|
+// out.close();
|
|
|
+// return true;
|
|
|
+// }
|
|
|
+// }
|
|
|
+ ////////////////////////////////////////////////////////////////////////
|
|
|
+// public static boolean GenerateImage(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;
|
|
|
+// }
|
|
|
+// }
|
|
|
+// OutputStream out = new FileOutputStream(path);
|
|
|
+// out.write(b);
|
|
|
+// out.flush();
|
|
|
+// out.close();
|
|
|
+// return true;
|
|
|
+// } catch (Exception e) {
|
|
|
+// return false;
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 示例
|
|
|
+ */
|
|
|
+ public static void main(String[] args) {
|
|
|
+ PictureUtil pictureUtil = new PictureUtil();
|
|
|
+// String strImg = pictureUtil.getImageStr("D:/pic/86619-106.jpg");
|
|
|
+// System.out.println(strImg);
|
|
|
+// pictureUtil.generateImage(strImg, "D:/pic/86619-107.jpg");
|
|
|
+
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
+ String imgBase64Str = pictureUtil.convertFileToBase64("D:\\pic\\dream.jpg");
|
|
|
+ System.out.println("本地图片转换Base64:" + imgBase64Str);
|
|
|
+ System.out.println("Base64字符串length=" + imgBase64Str.length());
|
|
|
+ pictureUtil.convertBase64ToFile(imgBase64Str, "D:\\pic\\out", "test.jpg");
|
|
|
+ System.out.println("duration:" + (System.currentTimeMillis() - start));
|
|
|
+ }
|
|
|
+}
|