码迷,mamicode.com
首页 > 其他好文 > 详细

google的zxing二维码生成

时间:2015-03-11 17:17:29      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:二维码      

二维码生成有很多sdk,但本人认为zxing包最好用,以下就是二维码生成的类
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;  
import java.io.File;  
import java.io.IOException;

import javax.imageio.ImageIO;  

import org.apache.commons.lang3.StringUtils;

import com.google.zxing.BarcodeFormat;  
import com.google.zxing.MultiFormatWriter;  
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;  
      
/** 
 * @ClassName: GenerateQRCode 
 * @Description: 生成二维码 
 * @author  
 * @company  
 * @date 2015-3-10 
 * @version V1.0 
 */  
public final class QRCodeUtils {  
  
    private static final QRCodeUtils instance = new QRCodeUtils();  
  
    private QRCodeUtils() {  
    }  
  
    public static QRCodeUtils getInstance() {  
        return instance;  
    }  
  
    private static final int BLACK = 0xff000000;  
    private static final int WHITE = 0xFFFFFFFF;  
    
    private static final int WIDTH = 400;		//二维码宽
    private static final int HEIGHT = 400;		//二维码高
    
    private static final int totalWidth = 400;	//总宽
    private static final int totalHeight = 600;	//总高
    
   
    int fontHeight=20,lineHeight=20;			//字高,行高
    /** 
     * @Title: generate 
     * @Description: 生成二维码 
     * @param assetsName 
     *            二维码图片名称 
     * @param params 
     *            二维码信息 
     * @param width 
     *            生成的图片的宽 
     * @param height 
     *            生成的图片的高 
     * @param path 
     *            二维码图片存放目录 
     * @throws Exception 
     * @return String 二维码图片名称 
     * @author  
     * @date 2012-11-9 
     */  
    public String generate(String nick,String assetsName, String params, String path, String fileName) throws Exception {  
    	
    	File file = new File(path);  
    	  
        if (!file.exists()) {  
            file.mkdirs();  
        }  
        
        //获取logo文件
        String head=path+"logo.png";
        
        // 二维码图片存放路径  
        path = path.concat(fileName);  
        file = new File(path);  
        if (!file.exists()) {  
            file.createNewFile();  
        }  
  
        BitMatrix bitMatrix = new MultiFormatWriter().encode(params,  
                BarcodeFormat.QR_CODE, WIDTH, HEIGHT);  
        
        //二维码的buffered
        BufferedImage bufImgCode = toBufferedImage(bitMatrix);
        
        //构造新图像
        BufferedImage bufImg = new BufferedImage(totalWidth, totalHeight, BufferedImage.TYPE_INT_RGB); // 图片的大小
        Graphics2D gs = bufImg.createGraphics();
        gs.setBackground(Color.WHITE);
        gs.clearRect(0, 0, totalWidth, totalHeight);
        gs.setColor(Color.BLACK);
        
        //把二维码buffered画到gs里面
        if(bufImgCode!=null)
        	gs.drawImage(bufImgCode, null, 0, 100);
        //head 
        Image img = ImageIO.read(new File(head));
        //实例化一个Image对象。             
        gs.drawImage(img, 40, 30, null);
        if(StringUtils.isNotBlank(nick)){
        	gs.setFont(new Font("微软雅黑", Font.BOLD, 20));
            gs.drawString(nick,160, 80);
            gs.setFont(new Font("微软雅黑", Font.BOLD, 16));
            gs.drawString("为您推荐最热门的手机业务",160, 110);
            
        }
        
        //把文字写到二维码下面
        if (StringUtils.isNotBlank(assetsName)) {
            gs.setFont(new Font("微软雅黑", Font.BOLD, 20));
            gs.drawString("欢迎扫一扫二维码",50, 500);
            gs.drawString("办理"+assetsName,50, 540);
        }
        
        ImageIO.write(bufImg, "png", file);  
        return fileName;  
    }  
  
    public static void writeToFile(BitMatrix matrix, String format, File file)  
            throws Exception {  
  
        BufferedImage image = toBufferedImage(matrix);  
  
        ImageIO.write(image, format, file);  
  
    }  
  
    public static BufferedImage toBufferedImage(BitMatrix matrix) {  
  
        int width = matrix.getWidth();  
  
        int height = matrix.getHeight();  
  
        BufferedImage image = new BufferedImage(width, height,  
                BufferedImage.TYPE_INT_ARGB);  
  
        for (int x = 0; x < width; x++) {  
  
            for (int y = 0; y < height; y++) {  
  
                image.setRGB(x, y, matrix.get(x, y) == true ? BLACK : WHITE);  
  
            }  
  
        }  
  
        return image;  
    }  
    
    public static void main(String[] args) {
    	 String path = "E:\\";  
         try {  
        	 QRCodeUtils.getInstance().generate("XXX","移动流量数据包50元","www.baidu.com", path,"90006.png"); 
             System.out.println("生成二维码成功");  
   
         } catch (WriterException e) {  
             e.printStackTrace();  
         } catch (IOException e) {  
             e.printStackTrace();  
         } catch (Exception e) {  
             e.printStackTrace();  
         } 
	}
}  

google的zxing二维码生成

标签:二维码      

原文地址:http://blog.csdn.net/yeyincai/article/details/44200343

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!