首页 > 代码库 > 二维码
二维码
//黑色
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 :
二维码