Java to achieve the two dimensional code function of the instance code

  • 2020-06-03 06:31:44
  • OfStack

Recently, I suddenly wanted to write some articles, so I started to write some technical code that I felt useful and you might use. It is convenient to learn and communicate with each other.

In this article, Java is mainly used to realize 2-d code:

Before writing the code, I will first talk about the overall idea, in order to facilitate better and faster implementation of functions.

(1). First, to realize the 2-d code function, it is necessary to import the core jar package of ES8en. google. zxing.

(2). The so-called 2-dimensional code is to put the required content into a picture, so here we first create a picture with parameters, the method is as follows:


private static BufferedImage createImage(String content, String imgPath, boolean needCompress) throws Exception {
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
hints.put(EncodeHintType.MARGIN, 1);
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE,
hints);
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
}
}
if (imgPath == null || "".equals(imgPath)) {
return image;
}
//  Insert the picture 
QRCodeUtil.insertImage(image, imgPath, needCompress);
return image;
}
// insert logo  If you don't need to logo This method can be executed 
private static void insertImage(BufferedImage source, String imgPath, boolean needCompress) throws Exception {
File file = new File(imgPath);
if (!file.exists()) {
System.err.println("" + imgPath + "   The file does not exist! ");
return;
}
Image src = ImageIO.read(new File(imgPath));
int width = src.getWidth(null);
int height = src.getHeight(null);
if (needCompress) { //  The compression LOGO
if (width > WIDTH) {
width = WIDTH;
}
if (height > HEIGHT) {
height = HEIGHT;
}
Image image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH);
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(image, 0, 0, null); //  Draw a scaled down diagram 
g.dispose();
src = image;
}
//  insert LOGO
Graphics2D graph = source.createGraphics();
int x = (QRCODE_SIZE - width) / 2;
int y = (QRCODE_SIZE - height) / 2;
graph.drawImage(src, x, y, width, height, null);
Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
graph.setStroke(new BasicStroke(3f));
graph.draw(shape);
graph.dispose();
}

(3). So far a 2-d code image generation method is written, is not very simple, the next is to call this method.


/**
*  generate 2 D code ( embedded LOGO)
* 
* @param content
*       content 
* @param imgPath
*      LOGO address 
* @param destPath
*       Storage directory 
* @param needCompress
*       Whether the compression LOGO
* @throws Exception
*/
public static void encode(String content, String imgPath, String destPath, boolean needCompress) throws Exception {
BufferedImage image = QRCodeUtil.createImage(content, imgPath, needCompress);
mkdirs(destPath);
String file = new Random().nextInt(99999999) + ".jpg";
ImageIO.write(image, FORMAT_NAME, new File(destPath + "/" + file));
}
/**
*  When the folder does not exist, mkdirs Multiple levels of directories are automatically created mkdir . (mkdir An exception is thrown if the parent directory does not exist )
* 
* @author LongJin
* @date 2013-12-11  In the morning 10:16:36
* @param destPath
*       Storage directory 
*/
public static void mkdirs(String destPath) {
File file = new File(destPath);
//  When the folder does not exist, mkdirs Multiple levels of directories are automatically created mkdir . (mkdir An exception is thrown if the parent directory does not exist )
if (!file.exists() && !file.isDirectory()) {
file.mkdirs();
}
}

(4). If you don't want logo, just pass the path of logo to null on call.

(5). So far a 2 - dimensional code generation tool is completed, of course, someone may need to parse 2 - dimensional code, so here the 2 - dimensional code analysis method is also written, but also convenient for me to view in the future.


/**
*  parsing 2 D code 
* 
* @param file
*      2 D code pictures 
* @return
* @throws Exception
*/
public static String decode(File file) throws Exception {
BufferedImage image;
image = ImageIO.read(file);
if (image == null) {
return null;
}
BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result;
Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
result = new MultiFormatReader().decode(bitmap, hints);
String resultStr = result.getText();
return resultStr;
}

Related articles: