码迷,mamicode.com
首页 > 其他好文 > 详细

二维码

时间:2017-06-05 15:45:58      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:get   ++   private   nbsp   writer   tac   tab   cep   type   

//黑色

private static final int BLACK = 0xFF000000;

//白色
private static final int WHITE = 0xFFFFFFFF;

 

public static BufferedImage genImage(String content,int width, int height){

Hashtable hints = new Hashtable();
// 内容所使用编码
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix matrix = null;
try {

//生成二维码数据
matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
} catch (WriterException e) {
e.printStackTrace();
}
if(matrix!=null){
int realWidth = matrix.getWidth();
int realHeight = matrix.getHeight();

//将二维码数据填充到对应的二维码图片中,即黑白框
BufferedImage image = new BufferedImage(realWidth, realHeight, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
}
}
return image;
}
return null;
}

--------------------------------

BufferedImage :Image是一个抽象类,BufferedImage 是Image 的实现类。其主要最用是将图片加载到内存当中

BitMatrix :

二维码

标签:get   ++   private   nbsp   writer   tac   tab   cep   type   

原文地址:http://www.cnblogs.com/zhangbc-Blog/p/6944993.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!