public static String encodeToBase64(String content){

MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
Map hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); //设置字符集编码类型
BitMatrix bitMatrix = null;
try {
bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 300, 300,hints);
BufferedImage image = toBufferedImage(bitMatrix);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
//输出二维码图片流
try {
ImageIO.write(image, "png",outputStream);
return Base64.encodeBase64String(outputStream.toByteArray());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (WriterException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return null;

}

基础转换

public void putImage(String key, Image image) throws IOException {
// Image->bufferreImage
BufferedImage bimg = new BufferedImage(image.getWidth(null),
image.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics g = bimg.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();

// bufferImage->base64
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageIO.write(bimg, "jpg", outputStream);
BASE64Encoder encoder = new BASE64Encoder();
String base64Img = encoder.encode(outputStream.toByteArray());

// 输出
this.content = this.content.replace("@{" + key + "}",
"<img src= \"data:image/png;base64," + base64Img + "\"/>");
}

public void putImage(String key, Image image, int width, int height)
throws IOException {
// Image->bufferreImage
BufferedImage bimg = new BufferedImage(image.getWidth(null),
image.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics g = bimg.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();

// bufferImage->base64
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageIO.write(bimg, "jpg", outputStream);
BASE64Encoder encoder = new BASE64Encoder();
String base64Img = encoder.encode(outputStream.toByteArray());

this.content = this.content.replace("@{" + key + "}",
"<img src= \"data:image/png;base64," + base64Img
+ "\" width=\"" + width + "\" height=\"" + height
+ "\"/>");
}

最新文章

  1. WebRTC音频预处理单元APM的整体编译及使用
  2. Beta Daily Scrum 第四天
  3. Factstone Benchmark
  4. JavaScript中创建类,赋值给ajax中的data参数
  5. bzoj 2434: [Noi2011]阿狸的打字机
  6. springMVC3 ckeditor3.6 图片上传 JS回调
  7. 格式化分区,报/dev/sdb1 is apparently in use by the system; will not make a filesystem here!
  8. 一次完整的HTTP事务是怎样一个过程?(转)
  9. Backward Digit Sums(POJ 3187)
  10. 第七届河南省赛G.Code the Tree(拓扑排序+模拟)
  11. react component onClick 函数带参数
  12. python 开发接口(一)
  13. windows 10 screenshot keyboard shortcut
  14. java List 根据属性排序
  15. 从网络上筛选&quot;流媒体&quot;的相关文章
  16. mysql判断两个时间段是否有交集
  17. Redis服务器开启远程访问
  18. 一段程序的分析——C++析构器,何时析构
  19. 父组件中vuex方法更新state,子组件不能及时更新并渲染的解决方法
  20. 神经网络前向后向传播(理论推导+代码) 单层神经网络相当于logistic regression

热门文章

  1. 第四篇Django之模板语言
  2. Junit学习使用
  3. Oracle:文字与字符串不匹配
  4. C语言预处理理论-宏定义1
  5. Express ~ 获取表单 get 和 post 提交方式传送参数的对比
  6. 17 —— 服务端渲染 —— art-template
  7. date linux系统校正时间
  8. select * 和select 1 以及 select count(*) 和select count(1)的区别
  9. 前端框架vue学习笔记
  10. python数据拼接: pd.concat