二維碼
//黑色
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 :
二維碼