/**
* 将图片转换成Base64编码
* @param imgFile 待处理图片 /Applications/开发常用文件/mmexport1555768366578.jpg
* @return
*/
public static String getImgStr(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();
}
return new String(Base64.encodeBase64(data));
}

/**
* 对字节数组字符串进行Base64解码并生成图片
* @param imgStr 图片数据
* @param imgFilePath 保存图片全路径地址
* @return
*/
public static boolean generateImage(String imgStr,String imgFilePath){
if (imgStr == null) { //图像数据为空
return false;
}
try {
//Base64解码
byte[] b = Base64.decodeBase64(imgStr);
for(int i=0;i<b.length;++i) {
if(b[i]<0) {//调整异常数据
b[i]+=256;
}
}
//生成jpeg图片
OutputStream out = new FileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();
return true;
} catch (Exception e) {
return false;
}
}

/**
* * MultipartFile转成InputStream
*  将图片转换成Base64编码

  * 

    InputStream in = null;
byte[] data = null;
//读取图片字节数组
try {
byte [] byteArr = uploadFiles.getBytes();
in = new ByteArrayInputStream(byteArr);
data = new byte[in.available()];
in.read(data);
in.close();
}
catch (IOException e) {
e.printStackTrace();
} catch (Exception e1) {
e1.getMessage();
e1.printStackTrace();
}
System.err.println(new String(Base64.encodeBase64(data)).length());
System.err.println(new String(Base64.encodeBase64(data)));
return new String(Base64.encodeBase64(data));
}

相关文章:

  • 2021-12-28
  • 2021-11-17
  • 2022-12-23
  • 2021-11-20
  • 2022-02-15
  • 2022-12-23
  • 2021-10-19
猜你喜欢
  • 2022-12-23
  • 2021-06-05
  • 2021-06-05
  • 2021-11-21
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案