博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java实现二维码生成
阅读量:7015 次
发布时间:2019-06-28

本文共 3470 字,大约阅读时间需要 11 分钟。

hot3.png

package com.zzl.controller.demo;import com.google.zxing.*;import com.google.zxing.client.j2se.BufferedImageLuminanceSource;import com.google.zxing.client.j2se.MatrixToImageWriter;import com.google.zxing.common.BitMatrix;import com.google.zxing.common.HybridBinarizer;import com.google.zxing.qrcode.QRCodeWriter;import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;import javax.imageio.ImageIO;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.nio.ByteBuffer;import java.nio.CharBuffer;import java.nio.charset.CharacterCodingException;import java.nio.charset.Charset;import java.nio.charset.CharsetEncoder;import java.util.Hashtable;/** * Class Object is the root of the class hierarchy. * Every class has Object as a superclass. All objects, * including arrays, implement the methods of this class. * * @author Administrator * @version 1.0 * @see * @since JDK1.7 * 

* History * Created by Administrator on 2017/12/11 0011. */public class QRcodeDemo { private static Hashtable hints = new Hashtable(); { //设置二维码排错率,可选L(7%) M(15%) Q(25%) H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.Q); } public static void main(String[] args) throws Exception{ String filePath = "C:\\Users\\Administrator\\Desktop\\1.jpg"; encode("http://www.baidu.com/", filePath); decode(filePath); } // qrcode 编码 public static void encode(String conent, String filePath) throws Exception{ BitMatrix matrix = null; int h = 200; int w = 200; QRCodeWriter writer = new QRCodeWriter(); try { matrix = writer.encode(conent, BarcodeFormat.QR_CODE, h, w, hints); File file = new File(filePath); MatrixToImageWriter.writeToFile(m, "PNG", file); } catch (com.google.zxing.WriterException e) { System.out.println(e.getMessage()); } } // qrcode 解码 public static void decode(String file) { try { Result result = null; BufferedImage image = null; image = ImageIO.read(new File(file)); LuminanceSource source = new BufferedImageLuminanceSource(image); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); Hashtable

hints = new Hashtable
(); hints.put(DecodeHintType.CHARACTER_SET, "UTF-8"); result = new MultiFormatReader().decode(bitmap, hints); String rtn = result.getText(); System.out.println(rtn); System.out.println(rtn.length()); } catch (Exception ex) { System.out.println(ex.toString()); } }}

生成的二维码图片:

143941_3ZRi_2431191.png

上面二维码旁边会有一圈白边的,如果要想去掉白边,加上下面这段代码

在输出前调用deleteWhite()方法:

m= deleteWhite(m);//删除白边MatrixToImageWriter.writeToFile(m, "PNG", file);
private static BitMatrix deleteWhite(BitMatrix matrix) {        int[] rec = matrix.getEnclosingRectangle();        int resWidth = rec[2] + 1;        int resHeight = rec[3] + 1;        BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);        resMatrix.clear();        for (int i = 0; i < resWidth; i++) {            for (int j = 0; j < resHeight; j++) {                if (matrix.get(i + rec[0], j + rec[1]))                    resMatrix.set(i, j);            }        }        return resMatrix;    }

145421_LorM_2431191.png

转载于:https://my.oschina.net/alarm1673/blog/1590428

你可能感兴趣的文章
Java 多线程学习笔记
查看>>
CDMA.ANYDATA 短信接收注意事项(针对乱码问题)
查看>>
c语言 函数可变参数列表
查看>>
UML概述
查看>>
ListView优化-getView优化
查看>>
Firefox 按一下Alt键 出现菜单!
查看>>
zepto中的属性设置
查看>>
oracle复习
查看>>
浅谈软件系统的标准化和产品化
查看>>
C++中sizeof操作符与strlen函数
查看>>
中文名文件上传到linux服务器上以后文件名会乱码(openoffice)
查看>>
C#语言学习--基础部分(二) 方法重载
查看>>
阅读笔记《构建之法》一
查看>>
Android 虚拟机 程序安装目录
查看>>
深入学习Hive应用场景及架构原理
查看>>
07-01 Java 封装
查看>>
HDU_1143_tri tiling
查看>>
codeforces_1075_C. The Tower is Going Home
查看>>
使用BBED模拟Oracle数据库坏块
查看>>
C# 关于XML的简单操作实例
查看>>