Maven依赖

<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>javase</artifactId>
    <version>3.4.1</version>
</dependency>

二维码

生成二维码

import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;

public class DemoTest {
    public static void main(String[] args) throws Exception {
        // 二维码原理(将字符串信息通过一规则转换为图像)
        // 生成二维码
        String name = "d:/my.png";
        String content = "wwww.hcitlife.show";
        int width = 400;
        int height = 400;
        // BitMatrix bm = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height);

        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
        hints.put(EncodeHintType.MARGIN, 2);
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        BitMatrix bm = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);

        MatrixToImageWriter.writeToStream(bm, "png", new FileOutputStream(name));
        // BufferedImage bar = ImageIO.read(new File(name));
        // 解压读取二维码
    }

}

结果:
在这里插入图片描述

import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import javax.imageio.ImageIO;

import java.awt.Color;
import java.awt.image.BufferedImage;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;

public class DemoTest {
    public static void main(String[] args) throws Exception {
        // 二维码原理(将字符串信息通过一规则转换为图像)
        // 生成二维码
        String name = "d:/my.png";
        String content = "www.hcitlife.show";
        int width = 400;
        int height = 400;
        // BitMatrix bm = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE,
        // width, height);

        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
        hints.put(EncodeHintType.MARGIN, 2);
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        BitMatrix bm = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);

        MatrixToImageWriter.writeToStream(bm, "png", new FileOutputStream(name));

        BufferedImage bar = ImageIO.read(new File(name));

        BufferedImage ok = new BufferedImage(width, height, 1);
        ok.getGraphics().drawImage(bar, 0, 0, width, height, null);

        // System.out.println(String.format("%x", Color.RED.getRGB()));
        // int[][] px = new int[width][height];
        Random rand = new Random();
        Color c = new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255), rand.nextInt(255));
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                ok.setRGB(x, y, bar.getRGB(x, y) == -1 ? 0xffffffff : c.getRGB());
            }
            // if(x>5) break;
        }
        ImageIO.write(ok, "png", new File("d:/mymy.png"));

        // BufferedImage bar = ImageIO.read(new File(name));

        // 解压读取二维码
        System.out.println("end");
    }

}

在这里插入图片描述

解析二维码

import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

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

public class DemoTest {
    public static void main(String[] args) throws Exception {
        // 解析读取二维码
         //解析图像
        BufferedImage i = ImageIO.read(new File("c:/my.png"));
        LuminanceSource source = new BufferedImageLuminanceSource(i);
        BinaryBitmap image = new BinaryBitmap(new HybridBinarizer(source));
        try{
            Result result = new MultiFormatReader().decode(image);
            String info = result.getText();
            System.out.println(info);
            if(info==null || "".equals(info)){
                System.out.println("没有二维码信息");
            }else if(info.contains("weixin")){  //qq mp
                System.out.println("有微信二维码,不允许");
            }else{
                System.out.println("有二维,可以使用");
            }
        }catch(Exception e){
            System.out.println("图像中没有二维码");
        }
    }

}

在这里插入图片描述

条形码


import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.awt.Color;
import java.awt.Font;
import java.awt.RenderingHints;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;

public class DemoTest {

    public static void main(String[] args) throws Exception {
        // 生成产品条码效果
        String name = "d:/bar.png";
        String content = "6923790798701";
        int width = 300;
        int height = 100;

        BitMatrix bm = new MultiFormatWriter().encode(content, BarcodeFormat.EAN_13, width, height);

        MatrixToImageWriter.writeToStream(bm, "png", new FileOutputStream(name));

        BufferedImage bar2 = ImageIO.read(new File(name));
        BufferedImage bar = new BufferedImage(bar2.getWidth(), bar2.getHeight() + 30, 1);

        Graphics2D g = bar.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);

        g.setColor(Color.WHITE);
        g.fillRect(0, 0, bar2.getWidth(), bar2.getHeight() + 30);
        g.drawImage(bar2, 0, 15, bar2.getWidth(), bar2.getHeight(), null);
        g.fillRect(62, 100, 85, 15);
        g.fillRect(155, 100, 83, 15);
        g.setColor(Color.BLACK);
        g.setFont(new Font("", Font.PLAIN, 22));

        g.drawString(content.substring(0, 1), 40, 120);
        g.drawString(content.substring(1, 7), 68, 120);
        g.drawString(content.substring(7), 158, 120);
        g.dispose();

        ImageIO.write(bar, "png", new File("c:/bar2.png"));

    }

}

在这里插入图片描述