package test;

 import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.Hashtable; import javax.imageio.ImageIO; import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64; public class Test { public static void main(String[] args) throws Exception {
String text = "你好"; int width = 100;
int height = 100;
String format = "png";
Hashtable hints = new Hashtable();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
File outputFile = new File("new.png");
MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile); byte[] b = toByteArray(new File("new.png"));
new Base64();
String s = Base64.encode(b);
// String s = new String(b, "utf-8");
System.out.println(s);
// new Base64();
ByteArrayInputStream in = new ByteArrayInputStream(Base64.decode(s));
BufferedImage image = ImageIO.read(in);
File newFile = new File("new2.png");
ImageIO.write(image, "png", newFile); } public static byte[] toByteArray(File imageFile) throws Exception {
BufferedImage img = ImageIO.read(imageFile);
ByteArrayOutputStream buf = new ByteArrayOutputStream((int) imageFile.length());
try {
ImageIO.write(img, "jpg", buf);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return buf.toByteArray();
} }

最新文章

  1. 是时候 UWP 了 !
  2. jQuery的$.ajax
  3. 15个来自 CodePen 的酷炫 CSS 动画效果【下篇】
  4. TextBoxFor控件的扩展---Bootstrap在mvc上的应用
  5. Eclipse调试方法及快捷键
  6. α发布后的感想(组长作业)
  7. 如何在redhat下安装办公软件(openoffice)
  8. footer居底
  9. NET Core驱动已出,支持EF Core
  10. PHP基础设计模式——工厂模式
  11. spring.NET的依赖注入
  12. jquery-easyUI第一篇【介绍、入门、使用常用的组件】
  13. Oracle常用的数值函数,日期函数
  14. Haproxy 配置 ACL 处理不同的 URL 请求
  15. cemtos7.2搭建samba
  16. Android O seLinux 编译错误
  17. lambdas vs. method groups
  18. c++各种排序的简单实现
  19. 解决uc浏览器不支持vw单位的方法
  20. photoshop CS5制作具有立体感的按钮

热门文章

  1. vue2-dragula vue拖拽组件
  2. 2.10 学习总结 之 JQ加强
  3. HDU - 6201 transaction transaction transaction(spfa求最长路)
  4. UVA - 548 Tree(二叉树的递归遍历)
  5. CGridCtrl只点击规定行中的按钮才弹出对话框
  6. linux下安装redis,按照redis官网安装不成功需要提前安装c++环境(安装成功并可以测试)
  7. shell中取字符串子串的几种方式 截取substr
  8. 057-while循环
  9. office组件导入导出常见异常记录
  10. UVA 10564 计数DP