注意:如果想要二维码跳到指定的页面链接,要在链接前加入“http://”,否则只是一个文本。
zxing
生成二维码代码如下:
public class CreateQRCode {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
int width = 300;
int height = 300;
String format = "png";
String content = "hello world";
// 定义二维码的参数
@SuppressWarnings("rawtypes")
Map hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
hints.put(EncodeHintType.MARGIN, 2);
// 生成二维码
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
Path file = new File("E:/img.png").toPath();
MatrixToImageWriter.writeToPath(bitMatrix, format, file);
} catch (Exception e) {
e.printStackTrace();
}
}
}
读取二维码内容代码如下:
public class ReadQRCode {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
try {
MultiFormatReader formatReader = new MultiFormatReader();
File file = new File("E:/img.png");
BufferedImage image = ImageIO.read(file);
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(
new BufferedImageLuminanceSource(image)));
// 定义二维码的参数
@SuppressWarnings("rawtypes")
Map hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
Result result = formatReader.decode(binaryBitmap, hints);
System.out.println("解析结果:" +result.toString());
System.out.println("二维码格式类型:" +result.getBarcodeFormat());
System.out.println("二维码文本内容:" +result.getText());
} catch (NotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
jquery.qrcode
将jquery.qrcode.min.js和jquery添加到您的网页中
1 2 |
|
然后创建一个DOM元素去包含生成qr码。
1 |
|
然后你在此容器中的添加qrcode
1 2 3 4 5 |
|