java生成二维码

来源:互联网 发布:java web 线程池 编辑:程序博客网 时间:2024/06/09 08:13

说到二维码,现在真是已经到处都是了,那么,什么是二维码?在Java中如何生成呢?
二维码 ,又称“二维条码”, 二维条形码最早发明于日本,它是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的,在代码编制上巧妙地利用构成计算机内部逻辑基础的“0”、“1”比特流的概念,使用若干个与二进制相对应的几何形体来表示文字数值信息,通过图象输入设备或光电扫描设备自动识读以实现信息自动处理。它具有条码技术的一些共性:每种码制有其特定的字符集;每个字符占有一定的宽度;具有一定的校验功能等。同时还具有对不同行的信息自动 识别功能、及处理图形旋转变化等特点。
Java语言的话生成二维码有很多开发的jar包,如ZXing,QRCode,前者是谷歌开发的后者则是小日本开发的.

ZXing的写法

  • 下载jar包
  • 编写工具类
package com.service;import com.google.zxing.common.BitMatrix;import javax.imageio.ImageIO;import java.awt.*;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import java.io.OutputStream;public class QRCodeMaker {    private  Color foreground = Color.white;    private  Color background = Color.black;    /**     * 默认构造器     */    public QRCodeMaker() {    }    /**     * 初始化前景色和背景色.     * @param foreground 前景色     * @param background 背景色     */    public QRCodeMaker(Color foreground,Color background){        this.foreground=foreground;        this.background=background;    }    /**     * BitMatrix转到BufferedImage.     * @param bitMatrix 编码过得文字     * @return     * 返回bufferedImage     */    public BufferedImage toBufferedImage(BitMatrix bitMatrix){        int width=bitMatrix.getWidth();        int hight=bitMatrix.getHeight();        BufferedImage image=new BufferedImage(width,hight,BufferedImage.TYPE_INT_RGB);        for(int i=0;i<width;i++){            for(int j=0;j<hight;j++){                image.setRGB(i,j,bitMatrix.get(i,j)?background.getRGB():foreground.getRGB());            }        }        return image;    }    /**     * 写到输出流中.     * @param bitMatrix 编码过得文字     * @param outputStream 输出流     * @param format 图片格式     */    public void writeToStream(BitMatrix bitMatrix,OutputStream outputStream,String format){        BufferedImage img=toBufferedImage(bitMatrix);        try {            ImageIO.write(img,format,outputStream);        } catch (IOException e) {            e.printStackTrace();        }    }    /**     * 写到文件.     * @param matrix 编码过得文字     * @param format 图片格式     * @param file 目的文件     */    public  void writeToFile(BitMatrix matrix, String format, File file){        BufferedImage image = toBufferedImage(matrix);        try {            ImageIO.write(image, format, file);        } catch (IOException e) {            e.printStackTrace();        }    }}
  • 使用工具类生成二维码
import java.io.File;import java.util.Hashtable;import com.google.zxing.BarcodeFormat;import com.google.zxing.EncodeHintType;import com.google.zxing.MultiFormatWriter;import com.google.zxing.common.BitMatrix;public class TestZxing {    public static void main(String[] args) throws Exception {        String text = "hello world!";        // 二维码的大小        int width = 250;        int height = 250;        //二维码的图片格式        String format = "gif";         // 内容所使用编码        Hashtable<EncodeHintType,String> hints =                                       new Hashtable<EncodeHintType,String>();        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");        BitMatrix bitMatrix = new MultiFormatWriter()                  .encode(text, BarcodeFormat.QR_CODE, width, height, hints);        // 生成二维码        File outputFile = new File("d:" + File.separator + "csyor.com.gif");        new QRCodeMaker.writeToFile(bitMatrix, format, outputFile);    }}

QRCode-maker用法

这是一个封装好的工具包,
原文地址

  1. 下载jar包
  2. 操作代码
package com.test;import java.awt.Color;import org.marker.qrcode.QRCode;/** * 二维码测试 * @author marker * */public class Test {    public static void main(String[] args) {        //获取二维码生成实例        QRCode code = QRCode.getInstance();        /* 个性化设置 */        code.setColor(Color.DARK_GRAY);        code.setBackground(Color.orange);        //生成二维码并保存到C盘根目录        code.encoder("http://aijava.cn", "C:\abc.png");        //读取二维码图片并解析        String info = code.decoder("C:\abc.png");        System.out.println(info);    }}
0 0
原创粉丝点击