java生成二维码
Java生成二维码我们用google的zxing,大牌产品
百度云盘地址:http://pan.baidu.com/s/1c0VXMPa 提取密码: bssv
步骤
一、设置二维码的纠错级别参数
Hashtable table = new Hashtable(); table.put(EncodeHintType.CHARACTER_SET, "utf-8"); //设置编码格式 table.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);//设置纠错级别
二、创建bit矩阵
BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height,table);
or
BitMatrix bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, width, height,table);
三、开始输出二维码
int matrixWidth = byteMatrix.getWidth();
BufferedImage image = new BufferedImage(matrixWidth-200, matrixWidth-200, BufferedImage.TYPE_INT_RGB);
image.createGraphics();
Graphics2D graphics = (Graphics2D) image.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, matrixWidth, matrixWidth);
// 使用比特矩阵画并保存图像
graphics.setColor(Color.BLACK);
for (int i = 0; i < matrixWidth; i++){
for (int j = 0; j < matrixWidth; j++){
if (byteMatrix.get(i, j)){
graphics.fillRect(i-100, j-100, 1, 1);
}
}
}
ImageIO.write(image, imageFormat, outputStream);
or
MatrixToImageWriter.writeToStream(bitMatrix, format, response.getOutputStream());//format:png/jpg
四、页面展示
<body> <h3>二维码生成</h3> <img src="<%=basePath%>/QrServlet"/> </body>
以上步骤拼接起来就可以生成二维码啦

浙公网安备 33010602011771号