A method of generating simple QR code

1. Two dimensional code based on zxing

Download address of related files: https://github.com/zxing/zxing/releases

Generate QR code

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
public class CreateQrcode {

	public static void main(String[] args) throws IOException {
		int width=300;
		int height=300;
		String format="png";
		String content="hello world";
		//configuration parameter
		HashMap 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("D:/img.img.png").toPath();
		MatrixToImageWriter.writeToPath(bitmatrix, format, file);
		} catch (WriterException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

Analysis of QR code

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import javax.imageio.ImageIO;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;

public class ReadQrcode {

	public static void main(String[] args) throws IOException, NotFoundException {
		MultiFormatReader formatReader=new MultiFormatReader();
		File file=new File("D:/img.png");
		BufferedImage image=ImageIO.read(file);
		BinaryBitmap binaryBitmap=new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
		HashMap hints=new HashMap();
		hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
		Result result=formatReader.decode(binaryBitmap,hints);
		System.out.println("Analysis results:"+result.toString());
		System.out.println("QR code format type:"+result.getBarcodeFormat());
		System.out.println("QR code text content:"+result.getText());
	}

}

 

2.jquery generates QR code

js download link: https://github.com/jeromeetienne/jquery-qrcode

First, include it in your web page with the usual script tag.

<script type="text/javascript" src="jquery.qrcode.min.js"></script>

Then create a DOM element that contains the generated qrcode image.

<div id="qrcode"></div>

Then add qrcode to the container

$('#qrcode').qrcode("this plugin is great");

You can set the height and width of the generated qrcode:

$('#qrcode').qrcode({width: 64,height: 64,text: "size doesn't matter"});

Example:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Generate QR code</title>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-3.3.1.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.qrcode.min.js"></script>
</head>
<body>
The generated QR code is as follows:<br>
   <div id="qrcode"></div>
 <script type="text/javascript">
	   $('#qrcode').qrcode("this plugin is great");
</script>
</body>
</html>

Note: the JS file of jQuery should also be referenced, and should be referenced before jquery.qrcode.min.js.

Tags: QRCode Google Java JQuery

Posted on Fri, 10 Jan 2020 10:33:16 -0800 by superdan_35