栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java简单生成二维码实例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java简单生成二维码实例

手敲了两种展现形式的二维码
  • 生成二维码图片
  • 网页生成二维码

生成二维码图片(中文乱码已处理)

package com.example.QRCode;

import com.alibaba.fastjson.JSONObject;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

public class QRCode01 {

    public static void main(String[] args) {
        try {
            //二维码包含信息内容
            //存入一个对象
            Map map=new HashMap();
            map.put("id","1001");
            map.put("name","花花");

            //将json对象转化为json字符串
            String text = JSONObject.toJSONString(map);

            generateQRCodeImage(text, 350, 350, QR_CODE_IMAGE_PATH);
            System.out.println("二维码已生成");
        } catch (WriterException e) {
            System.out.println( e.getMessage());
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }

    }


    //二维码图片生成路径
    private static final String QR_CODE_IMAGE_PATH = "C:/Users/86153/Desktop/11.PNG";

    //二维码生成方法
    private static void generateQRCodeImage(String text, int width, int height, String filePath) throws WriterException, IOException, IOException {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();

        //中文扫码会乱码
        HashMap map = new HashMap<>();
        map.put(EncodeHintType.CHARACTER_SET, "utf-8");

        //保存为二维码图片
        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height,map);

        Path path = FileSystems.getDefault().getPath(filePath);

        //生成图片存放路径
        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);

    }

}

网页生成二维码(方法一样)

package com.example.QRCode;

import com.alibaba.fastjson.JSONObject;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
public class QRCode02 {

    @GetMapping("/qrcode")
    public String qrcode() {
        return "qrcode";
    }

    @GetMapping(value="/qrimage")
    public ResponseEntity getQRImage() {

        //二维码包含信息内容
        //存入一个对象
        Map map=new HashMap();
        map.put("id","1001");
        map.put("name","花花");

        //将json对象转化为json字符串
        String info = JSONObject.toJSONString(map);

        byte[] qrcode = null;
        try {
            qrcode = getQRCodeImage(info, 360, 360);
        } catch (WriterException e) {
            System.out.println("Could not generate QR Code, WriterException :: " + e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("Could not generate QR Code, IOException :: " + e.getMessage());
        }

        // Set headers
        final HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_PNG);

        return new ResponseEntity (qrcode, headers, HttpStatus.CREATED);
    }


    public static byte[] getQRCodeImage(String text, int width, int height) throws WriterException, IOException {

        QRCodeWriter qrCodeWriter = new QRCodeWriter();

        //中文扫码会乱码
        HashMap map = new HashMap<>();
        map.put(EncodeHintType.CHARACTER_SET, "utf-8");

        保存为二维码图片
        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height,map);

        ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream();

        MatrixToImageWriter.writeToStream(bitMatrix, "PNG", pngOutputStream);
        byte[] pngData = pngOutputStream.toByteArray();
        return pngData;
    }



}

qrcode.html




 
 



 
  QrCodeImage
 


 



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1000360.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号