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