JAVA生成二维码

1.导入google的zxing相关jar包
2.代码:

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

/**
 * 二维码生成器的类
 * @author 
 * @since 20180430
 *
 */
public class QRImageUtil {
	  //私有不可更改的变量:生成二维码图片的颜色
    private static final int BLACK = 0xFF000000;
    private static final int WHITE = 0xFFFFFFFF;
    //空的构造方法
    public QRImageUtil() {
    }
    /**
     * 静态方法
     * BufferedImage是Image的一个子类,BufferedImage生成的图片在内存里有一个图像缓冲区,利用这个缓冲区我们可以很方便的操作这个图片,
     * 通常用来做图片修改操作如大小变换、图片变灰、设置图片透明或不透明等。
     * @param matrix 编码形式
     * @return
     */
    public static BufferedImage toBufferedImage(BitMatrix matrix)
    {
            //图片的宽度和高度
            int width = matrix.getWidth();
            int height = matrix.getHeight();
            //BufferedImage.TYPE_INT_RGB将图片变为什么颜色
            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) ? BLACK : WHITE);
                    }
            }
            return image;
    }
    /** 
     * 设置 logo  
     * @param matrixImage 源二维码图片 
     * @return 返回带有logo的二维码图片 
     * @throws IOException 
     * @author Administrator sangwenhao 
     */  
     public static BufferedImage LogoMatrix(BufferedImage matrixImage) throws IOException{  
         /** 
          * 读取二维码图片,并构建绘图对象 
          */  
         Graphics2D g2 = matrixImage.createGraphics();  
           
         int matrixWidth = matrixImage.getWidth();  
         int matrixHeigh = matrixImage.getHeight();  
           
         /** 
          * 读取Logo图片 
          */  
         BufferedImage logo = ImageIO.read(new File("F:\\logo1.png"));  
  
         //开始绘制图片  
         g2.drawImage(logo,matrixWidth/5*2,matrixHeigh/5*2, matrixWidth/5, matrixHeigh/5, null);//绘制       
         BasicStroke stroke = new BasicStroke(5,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);   
         g2.setStroke(stroke);// 设置笔画对象  
         //指定弧度的圆角矩形  
         RoundRectangle2D.Float round = new RoundRectangle2D.Float(matrixWidth/5*2, matrixHeigh/5*2, matrixWidth/5, matrixHeigh/5,20,20);  
         g2.setColor(Color.white);  
         g2.draw(round);// 绘制圆弧矩形  
           
         //设置logo 有一道灰色边框  
         BasicStroke stroke2 = new BasicStroke(1,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);   
         g2.setStroke(stroke2);// 设置笔画对象  
         RoundRectangle2D.Float round2 = new RoundRectangle2D.Float(matrixWidth/5*2+2, matrixHeigh/5*2+2, matrixWidth/5-4, matrixHeigh/5-4,20,20);  
         g2.setColor(new Color(128,128,128));  
         g2.draw(round2);// 绘制圆弧矩形  
           
         g2.dispose();  
         matrixImage.flush() ;  
         return matrixImage ;  
     }  
      
    /**
     * 静态方法 用于生成图片
     * @param matrix 编码形式
     * @param format 图片类型
     * @param file 文件(图片路径,图片名称)
     * @throws IOException
     */
    public static void writeToFile(BitMatrix matrix,String format,File file) throws IOException
    {
            BufferedImage image = toBufferedImage(matrix);
            //生成带logo的二维码
            image = LogoMatrix(image);  
            if(!ImageIO.write(image, format, file))
            {
                    throw new IOException("Could not write an image of format " + format + " to " + file);
            }
    }
    /**
     * 输出
     * @param matrix
     * @param format
     * @param stream
     * @throws IOException
     */
    public static void writeToStream(BitMatrix matrix,String format,OutputStream stream) throws IOException
    {
            BufferedImage image = toBufferedImage(matrix);
            //生成带logo的二维码
            image = LogoMatrix(image); 
            if(!ImageIO.write(image, format, stream))
            {
                    throw new IOException("Could not write an image of format " + format);
            }
    }
    
    public static void generateQr(String url) throws Exception{ //二维码中保存的信息
        String content = "http://11.21.22.22/static/apk/app-release.apk";
        //生成的二维码保存的路径
        String path = "E:\\";
        MultiFormatWriter multiFormatWrite = new MultiFormatWriter();
        Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();  
        // 指定纠错等级,纠错级别(L 7%、M 15%、Q 25%、H 30%)  
       hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);  
       // 内容所使用字符集编码  
       hints.put(EncodeHintType.CHARACTER_SET, "utf-8");     
//     hints.put(EncodeHintType.MAX_SIZE, 350);//设置图片的最大值  
//     hints.put(EncodeHintType.MIN_SIZE, 100);//设置图片的最小值  
       hints.put(EncodeHintType.MARGIN, 1);//设置二维码边的空度,非负数  
        // 按照指定的宽度,高度和附加参数对字符串进行编码
        //生成二维码
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        BitMatrix bitMatrix = multiFormatWrite.encode(content, BarcodeFormat.QR_CODE, 400, 400, hints);
        File file1 = new File(path,sdf.format(new Date())+".jpg");
        // 写入文件
        writeToFile(bitMatrix, "jpg", file1);
        System.out.println("二维码图片生成成功!");
    }
    
    public static void main(String[] args) throws Exception {
    	generateQr("lll");
	}
    
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值