QRCode二维码与PDF417码生成与读取JAVA+HTML

二维码

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageConfig;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class QRCodeTest {
    public static void main(String[] args) {
        try {
            String s = "202101051020295,http://www.abc.com";
            String[] datas = s.split(",");
            for (String data: datas ) {
                QREncode(data,"D:\\新建文件夹\\",data,"gif");
               QRReader(new File("D:\\新建文件夹\\"+data+".gif"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 生成二维码
     * @param content 二维码内容
     * @param path 生成的路径
     * @param filename 生成的图像文件名
     * @param format 生成的图像文件格式
     * @throws WriterException
     * @throws IOException
     */
    public static void QREncode(String content,String path,String filename,String format) throws WriterException, IOException {
//        String content = "个人博客:https://www.cnblogs.com/huanzi-qch/";//二维码内容
        int width = 300; // 图像宽度
        int height = 300; // 图像高度
//        String                      format = "gif";// 图像类型
        Map<EncodeHintType, Object> hints  = new HashMap<>();
        //内容编码格式
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        // 指定纠错等级
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        //设置二维码边的空度,非负数
        hints.put(EncodeHintType.MARGIN, 1);
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
        String fullFileName = path.concat(filename).concat(".").concat(format);
        MatrixToImageWriter.writeToPath(bitMatrix, format, new File(fullFileName).toPath());// 输出原图片
        MatrixToImageConfig matrixToImageConfig = new MatrixToImageConfig(0xFF000001, 0xFFFFFFFF);
        /*
            问题:生成二维码正常,生成带logo的二维码logo变成黑白
            原因:MatrixToImageConfig默认黑白,需要设置BLACK、WHITE
            解决:https://ququjioulai.iteye.com/blog/2254382
         */
        BufferedImage bufferedImage = LogoMatrix(MatrixToImageWriter.toBufferedImage(bitMatrix,matrixToImageConfig), new File("D:\\logo.png"));
       // BufferedImage bufferedImage = LogoMatrix(toBufferedImage(bitMatrix), new File("D:\\logo.png"));
        String fullFileName1 = path.concat(filename).concat("1").concat(".").concat(format);
       ImageIO.write(bufferedImage, format, new File(fullFileName1));//输出带logo图片
        System.out.println("输出成功.");
    }

    /**
     * 识别二维码
     */
    public static void QRReader(File file) throws IOException, NotFoundException {
        MultiFormatReader formatReader = new MultiFormatReader();
        //读取指定的二维码文件
        BufferedImage bufferedImage =ImageIO.read(file);
        BinaryBitmap binaryBitmap= new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(bufferedImage)));
        //定义二维码参数
        Map  hints= new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        com.google.zxing.Result result = formatReader.decode(binaryBitmap, hints);
        //输出相关的二维码信息
        System.out.println("解析结果:"+result.toString());
        System.out.println("二维码格式类型:"+result.getBarcodeFormat());
        System.out.println("二维码文本内容:"+result.getText());
        bufferedImage.flush();
    }

    /**
     * 二维码添加logo
     * @param matrixImage 源二维码图片
     * @param logoFile logo图片
     * @return 返回带有logo的二维码图片
     * 参考:https://blog.csdn.net/weixin_39494923/article/details/79058799
     */
    public static BufferedImage LogoMatrix(BufferedImage matrixImage, File logoFile) throws IOException {
        /**
         * 读取二维码图片,并构建绘图对象
         */
        Graphics2D g2 = matrixImage.createGraphics();

        int matrixWidth = matrixImage.getWidth();
        int matrixHeigh = matrixImage.getHeight();

        /**
         * 读取Logo图片
         */
        BufferedImage logo = ImageIO.read(logoFile);

        //开始绘制图片
        g2.drawImage(logo,matrixWidth/5*2,matrixHeigh/5*2, matrixWidth/5, matrixHeigh/5, null);//绘制
        BasicStroke stroke = new BasicStroke(5,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
        g2.setStroke(stroke);// 设置笔画对象
        //指定弧度的圆角矩形
        RoundRectangle2D.Float round = new RoundRectangle2D.Float(matrixWidth/5*2, matrixHeigh/5*2, matrixWidth/5, matrixHeigh/5,20,20);
        g2.setColor(Color.white);
        g2.draw(round);// 绘制圆弧矩形

        //设置logo 有一道灰色边框
        BasicStroke stroke2 = new BasicStroke(1,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
        g2.setStroke(stroke2);// 设置笔画对象
        RoundRectangle2D.Float round2 = new RoundRectangle2D.Float(matrixWidth/5*2+2, matrixHeigh/5*2+2, matrixWidth/5-4, matrixHeigh/5-4,20,20);
        g2.setColor(new Color(128,128,128));
        g2.draw(round2);// 绘制圆弧矩形

        g2.dispose();
        matrixImage.flush() ;
        return matrixImage ;
    }
}


        
        <dependency>
            <groupId>com.google.zxinggroupId>
            <artifactId>javaseartifactId>
            <version>3.2.1version>
        dependency>

 
        <dependency>
            <groupId>com.google.zxinggroupId>
            <artifactId>coreartifactId>
            <version>3.3.3version>
             
        dependency>

BarPdf417AsBase64

 <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.14</version>
        </dependency>
          <dependency>
            <groupId>com.lowagie</groupId>
            <artifactId>iText</artifactId>
            <version>2.1.5</version>
        </dependency>


package com.doing.utils;

import org.apache.commons.codec.binary.Base64;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.charset.StandardCharsets;

public class BarcodePDF417Utils {

 /**
     * @Author Mr伍
     * @Description //TODO 
     * @Date  2021/3/4
     * @Param [codeString]  字符串
     * @return java.lang.String  返回base64字符串
     **/

    public static String createPdf417AsBase64(String codeString){
        com.lowagie.text.pdf.BarcodePDF417 pdf = new com.lowagie.text.pdf.BarcodePDF417();
        pdf.setText(codeString.getBytes(StandardCharsets.UTF_8));
        Image pdfImg = pdf.createAwtImage(Color.black, Color.white);
        BufferedImage img = new BufferedImage((int)pdfImg.getWidth(null), (int)pdfImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
        Graphics g = img.getGraphics();
        g.drawImage(pdfImg, 0, 0, Color.white, null);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
//        ImageIO.write(img, "PNG", bos);
        try {
            ImageIO.write(img, "BMP", bos);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Base64 base64 = new Base64();
        String s = base64.encodeToString(bos.toByteArray());
        return s;
    }
}

二维码

QRCode二维码与PDF417码生成与读取JAVA+HTML_第1张图片

PDF417码

在这里插入图片描述

前端HTML生成二维码带log(兼容所有游览器)

DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>title>
	head>
	<body>
		<script type="text/javascript" src="js/jquery-1.11.1.js" >script>
		<script type="text/javascript" src="js/jquery.qrcode.js" >script>
        <script type="text/javascript" src="js/qrcode.js" >script> 
        <script type="text/javascript" src="js/utf.js" >script>
		 <p>Render in tablep>
		 <div id="qrcodeTable">div>
		 <p>Render in canvasp>
		 <div id="qrcodeCanvas">div>
		 <script> 
		    jQuery('#qrcodeTable').qrcode({
		         render    : "table",                <!--二维码生成方式 -->
		         text    : "http://www.baidu.com" , <!-- 二维码内容  -->
		         width : "200",               //二维码的宽度
                 height : "200",
				 
		     });    
			 
		     jQuery('#qrcodeCanvas').qrcode({
		     	 render    : "canvas",
		         text    : "https://u.wechat.com/MInSjZ0QLtR0PTfqq3D5iO0",
		         width : "200",               //二维码的宽度
                 height : "200",              //二维码的高度
                 background : "#ffffff",       //二维码的后景色
                 foreground : "#000000",        //二维码的前景色
                 src: 'img/1.jpg'             //二维码中间的图片
		     });    
		 script>
		 body>
html>

QRCode二维码与PDF417码生成与读取JAVA+HTML_第2张图片

jquery.qrcode.js

链接:https://pan.baidu.com/s/1rqDyDC1ttJjdun2_6ckWWw 
提取码:j45u

jquery-1.11.1.js

链接: https://pan.baidu.com/s/1I2Xa7TTNDl6l5NRdVqwVNA 提取码: itbq 复制这段内容后打开百度网盘手机App,操作更方便哦

qrcode.js

链接:https://pan.baidu.com/s/1kgIgsTG1Duz0LGfWtb1Rcw 
提取码:9c7u

utf.js

链接:https://pan.baidu.com/s/1rBxd8FnH0uF354OMFW0EtA 
提取码:q8iv

你可能感兴趣的:(jAVA工具,智能识别图片,File处理工具,java,xcode,图像识别,html,前端)