如何用java制作二维码

首先:进入这个网址
github.com/zxing/。
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
将他复制成功后,就让他导出
这里写图片描述

就成这样啦
这里写图片描述

然后就新建个项目把刚才的包导入新建的项目
这里写图片描述

就像这样
这里写图片描述

接下来开始进行包的配置
这里写图片描述

这里写图片描述


这里写图片描述

这里写图片描述

这样大部分就弄完啦
接下来代码:
制作二维码

package com._520_make;

import java.io.File;
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 CreatCode {
    public static void main(String[] args) {
        /*
         * 定义二维码的长,宽,高;
         */
        int width = 300;
        int height = 300;
        String format = "png";
        String content = "https://www.50s.cc/";

        //定义二维码主要的参数

        HashMap hints = new HashMap();
        //用来设置字体的内容
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        //用来设置纠错等级
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        //用来设置边距
        hints.put(EncodeHintType.MARGIN, 2);


        //生成二维码
        try {
            //运用MultiFormatWriter().encode()需要在后面加上hints
             BitMatrix bitMatrix=new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height,hints);
             //生成的二维码存放路径
             Path file = new File("D:/img.png").toPath();
             MatrixToImageWriter.writeToPath(bitMatrix, format, file);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        /*
         * 在这个最后需要抛出异常
         */

    }

}

读取二维码

package com._520_make;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
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 ReadCode {

    public static void main(String[] args) throws Exception {
        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");
        try {
            Result result = formatReader.decode(binaryBitmap, hints);
            System.out.println("解析结果:"+result);
        } catch (NotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

另外如果要设置直接扫码就能进网址就把:
String content = “https://+网址”;
大致就这样拉。

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值