第八篇:JAVA生成二维码(zxing)

上一篇博客中介绍了条码的使用示例,这一篇继续介绍如何使用Java生成二维码。

package com.hq.util;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;

/**
 * 二维码创建,需添加jar包:zxing-core.jar
 * 
 * @author jianggujin
 * 
 */
public class QRCreater
{

   /**
    * 生成二维码文件
    * 
    * @param content
    *           二维码内容
    * @param size
    *           二维码大小
    * @param file
    *           生成文件
    * @throws IOException
    * @throws WriterException
    */
   public void write(String content, int size, File file) throws IOException,
         WriterException
   {
      ImageIO.write(
            toBufferedImage(new MultiFormatWriter().encode(content,
                  BarcodeFormat.QR_CODE, size, size), null, 0), "JPEG", file);
   }

   /**
    * 生成二维码并写入指定输出流
    * 
    * @param content
    *           二维码内容
    * @param size
    *           二维码大小
    * @param os
    *           输出流
    * @throws IOException
    * @throws WriterException
    */
   public void write(String content, int size, OutputStream os)
         throws IOException, WriterException
   {
      ImageIO.write(
            toBufferedImage(new MultiFormatWriter().encode(content,
                  BarcodeFormat.QR_CODE, size, size), null, 0), "JPEG", os);
   }

   /**
    * 生成带图标的二维码文件
    * 
    * @param content
    *           二维码内容
    * @param size
    *           二维码大小
    * @param icon
    *           图标文件
    * @param iconSize
    *           图标大小
    * @param file
    *           生成文件
    * @throws IOException
    * @throws WriterException
    */
   public void write(String content, int size, File icon, int iconSize,
         File file) throws IOException, WriterException
   {
      ImageIO.write(
            toBufferedImage(new MultiFormatWriter().encode(content,
                  BarcodeFormat.QR_CODE, size, size), icon, iconSize), "JPEG",
            file);
   }

   /**
    * 生成带图标的二维码并写入指定输出流
    * 
    * @param content
    *           二维码内容
    * @param size
    *           二维码大小
    * @param icon
    *           图标文件
    * @param iconSize
    *           图标大小
    * @param os
    *           输出流
    * @throws IOException
    * @throws WriterException
    */
   public void write(String content, int size, File icon, int iconSize,
         OutputStream os) throws IOException, WriterException
   {
      ImageIO.write(
            toBufferedImage(new MultiFormatWriter().encode(content,
                  BarcodeFormat.QR_CODE, size, size), icon, iconSize), "JPEG",
            os);
   }

   /**
    * 创建二维码的BufferedImage图像
    * 
    * @param content
    *           二维码内容
    * @param size
    *           二维码大小
    * @return image
    * @throws IOException
    * @throws WriterException
    */
   public BufferedImage toBufferedImage(String content, int size)
         throws IOException, WriterException
   {
      return toBufferedImage(new MultiFormatWriter().encode(content,
            BarcodeFormat.QR_CODE, size, size), null, 0);
   }

   /**
    * 创建带图标的二维码的BufferedImage图像
    * 
    * @param content
    *           二维码内容
    * @param size
    *           二维码大小
    * @param icon
    *           图标文件
    * @param iconSize
    *           图标大小
    * @return image
    * @throws IOException
    * @throws WriterException
    */
   public BufferedImage toBufferedImage(String content, int size, File icon,
         int iconSize) throws IOException, WriterException
   {
      return toBufferedImage(new MultiFormatWriter().encode(content,
            BarcodeFormat.QR_CODE, size, size), icon, iconSize);
   }

   /**
    * 创建二维码的BufferedImage图像
    * 
    * @param matrix
    * @param icon
    *           图标文件
    * @param iconSize
    *           图标尺寸
    * @return image
    * @throws IOException
    */
   public BufferedImage toBufferedImage(BitMatrix matrix, File icon,
         int iconSize) throws IOException
   {
      int color = 0xFF000000;
      int width = matrix.getWidth();
      int height = matrix.getHeight();
      BufferedImage image = new BufferedImage(width, height,
            BufferedImage.TYPE_INT_RGB);
      for (int x = 0; x < width; x++)
      {
         for (int y = 0; y < height; y++)
         {
            image.setRGB(x, y, matrix.get(x, y) ? color : 0xFFFFFFFF);
         }
      }
      Graphics2D graphics = image.createGraphics();
      if (icon != null && icon.exists())
      {
         BufferedImage bufferedImage = ImageIO.read(icon);
         if (iconSize <= 0)
         {
            iconSize = 50;
         }
         graphics.drawImage(bufferedImage, (image.getWidth() - iconSize) / 2,
               (image.getHeight() - iconSize) / 2, iconSize, iconSize, null);
      }
      graphics.dispose();
      image.flush();
      return image;
   }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值