package com.thinkgem.jeesite.modules.api.wechat;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder; import java.io.*;
/**
* 字符串和图片相互转换
* @author zhouhe
* @date 2020/7/3 17:23
*/
public class ImgToStr {
/**
* 字符串转图片
* @param imgStr --->图片字符串
* @param filename --->图片名
* @return
*/
public static boolean generateImage(String imgStr, String filename) { 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("D://"+filename);
out.write(b);
out.flush();
out.close();
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false; } /**
* 图片转字符串
* @param filePath --->文件路径
* @return
*/
public static String getImageStr(String filePath) {
InputStream inputStream = null;
byte[] data = null;
try {
inputStream = new FileInputStream(filePath);
data = new byte[inputStream.available()];
inputStream.read(data);
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
// 加密
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);
} /*
* 测试代码
*/
public static void main(String[] args) {
String imageStr = getImageStr("D:\\222.png");
System.out.println(imageStr);
System.out.println(imageStr.length());
boolean generateImage = generateImage(imageStr, "223.png");
System.out.println(generateImage);
}
}

最新文章

  1. yii模块下面的组件
  2. Index was outside the bounds of the array. (Microsoft.SqlServer.Smo)
  3. 打通移动App开发的任督二脉、实现移动互联创业的中国梦
  4. 关于android的单位dp与px
  5. 实时获取UITextField内容
  6. css 默认样式
  7. [GeekBand] 探讨C++新标准之新语法——C++ 11~14
  8. hdu 1851 A Simple Game 博弈论
  9. vue中多个input绑定enter按键事件
  10. javaWeb学习之Listener监听
  11. Python Scrapy项目创建(基础普及篇)
  12. 【.NET Core项目实战-统一认证平台】第八章 授权篇-IdentityServer4源码分析
  13. Word操作——通配符
  14. android spf中存list&lt;string&gt;
  15. ElasticSearch6.5.0 【安装IK分词器】
  16. kafka组件makemirror处理跨机房业务的应用
  17. 中标麒麟Linux7 如何关闭广播消息
  18. CefSharp 支持mp4
  19. mysql多列索引
  20. 在MFC内竟然不可以解析“Mat”

热门文章

  1. python实现Excel的表头与索引之间的转换
  2. Unity组件Toggle详解
  3. 下载接口时出现:Try to run this command from the system terminal. Make sure that you use the correct version of &#39;pip&#39; installed for your Python interpreter located at &#39;D:\python\demo\venv\Scripts\...的错误
  4. slitaz中tazpkg更改软件源
  5. 安装MinGW-C++开发环境2--软件安装
  6. python打包生成exe报错
  7. vue获取标签对象的方式
  8. css 卡片hover效果
  9. drop table后,约束会被删除吗?
  10. fpga always赋值