ZXing生成和解析二维码

引入zxing_3.2.0.jar和zxing-javase-3.2.0.jar包

 

编写工具类

package com.zns.common;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.ReaderException;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
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.QRCodeReader;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

/**
 * zxing二维码工具类
 *
 */
public class ZxingQRCodeUtils {
    /**
     * 生成二维码
     * 
     * @param content
     *            内容
     * @param width
     *            长度
     * @param height
     *            高度
     * @param margin
     *            留白边距
     * @param imageFormat
     *            生成的图片格式
     * @param logoPic
     *            logo文件
     * @param destFile
     *            生成的目标文件
     * @throws WriterException
     * @throws IOException
     */
    public static void createQrCode(String content, int width, int height, int margin, String imageFormat, File logoPic,
            File destFile) throws WriterException, IOException {
        HashMap hintMap = new HashMap();
        // 编码方式
        hintMap.put(EncodeHintType.CHARACTER_SET, "utf-8");
        // 纠错级别
        hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        // 二维码边缘留白距离
        hintMap.put(EncodeHintType.MARGIN, margin);
        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height,
                    hintMap);
            // BufferedImage bufferedImage =
            // MatrixToImageWriter.toBufferedImage(bitMatrix);
            MatrixToImageWriter.writeToFile(bitMatrix, imageFormat, destFile);
            BufferedImage bufferedImage = ImageIO.read(destFile);
            if (logoPic != null) {
                BufferedImage bufferedImageLogo = ImageIO.read(logoPic);

                int widthLogo = bufferedImageLogo.getWidth(null) > bufferedImage.getWidth() * 2 / 10
                        ? (bufferedImage.getWidth() * 2 / 10) : bufferedImageLogo.getWidth(null);
                int heightLogo = bufferedImageLogo.getHeight(null) > bufferedImage.getHeight() * 2 / 10
                        ? (bufferedImage.getHeight() * 2 / 10) : bufferedImageLogo.getHeight(null);

                // logo放在中心
                int x = (bufferedImage.getWidth() - widthLogo) / 2;
                int y = (bufferedImage.getHeight() - heightLogo) / 2;

                // logo放在右下角
                // int x = (bufferedImage.getWidth() - widthLogo);
                // int y = (bufferedImage.getHeight() - heightLogo);

                // 开始绘制图片
                Graphics2D g = bufferedImage.createGraphics();
                g.drawImage(bufferedImageLogo, x, y, widthLogo, heightLogo, null);
                g.dispose();
                bufferedImageLogo.flush();
                bufferedImage.flush();

            }
            ImageIO.write(bufferedImage, imageFormat, destFile);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 读取二维码内容
     * 
     * @param inputStream
     *            输入流
     * @return
     * @throws IOException
     */
    public static String readQrCodeText(InputStream inputStream) throws IOException {
        // 从输入流中获取字符串信息
        BufferedImage bufferedImage = ImageIO.read(inputStream);
        // 将图像转换为二进制位图源
        LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        QRCodeReader reader = new QRCodeReader();
        HashMap hintMap = new HashMap();
        hintMap.put(EncodeHintType.CHARACTER_SET, "utf-8");
        Result result = null;
        try {
            result = reader.decode(bitmap, hintMap);
        } catch (ReaderException e) {
            e.printStackTrace();
        }
        return result.getText();
    }
}

 

 

测试代码

File logoFile=new File("d:\\logo.jpg");
File destFile=new File("d:\\myqrcode.jpg");
//ZxingQRCodeUtils.createQrCode("hello world", 300, 300,0,"jpg", null,destFile);
ZxingQRCodeUtils.createQrCode("hello world", 300, 300,0,"jpg", logoFile,destFile);
System.out.println(ZxingQRCodeUtils.readQrCodeText(new FileInputStream(destFile)));

 

 

直接返回HttpServletResponse输出流,在springmvc 控制器方法内编写下面代码

File logoFile=new File("d:\\logo.jpg");
File destFile=new File("d:\\myqrcode.jpg");
ZxingQRCodeUtils.createQrCode("hello world", 300, 300,0,"jpg", logoFile,destFile);
BufferedImage bufferedImage=ImageIO.read(destFile);
ImageIO.write(bufferedImage, "jpg", resp.getOutputStream());

 

 

<img src="控制器url">即可加载显示生成的图片

上一篇:Linux操作系统


下一篇:随手练——LintCode 433 - 小岛数量