QrCode生成二维码


package com.aoleisite.cn.qrcode.casee;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

import com.swetake.util.Qrcode;
/**
 * 生成带logo的二维码
 * @author admin
 *
 */
public class TestQrCode {
	/**
	 * 测试
	 * @param args
	 */
	public static void main(String[] args) {
		String content = "ceshiceshi ";
		String imagePath = "I:\\QRCodeTest\\luooluo.jpg";
		String logoPath = "C:/Users/admin/Desktop/zhanghmFile/images/IMG_0159.JPG";
		qrCodeUtil(content,imagePath,logoPath,11);
	}
	
	/**
	 * 生成二维码(QRCode)图片 
	 * @param content 二维码图片的内容 
	 * @param imagePath生成二维码图片完整的路径 
	 * @param logoPath 二维码图片中间的logo路径 
	 * @param version二维码size
	 */
	public static void qrCodeUtil(String content,String imagePath,String logoPath,Integer version){
		//设置二维码的宽高
		/*int width=240;
		int height=240;*/
		
		/*int width=67 + 12*(version-1);
        int height=67+12*(version-1);*/
		try {
			//创建对象
			Qrcode qrCode = new Qrcode();
			//设置:容错率(7% L、15%M、25%Q、30%H)说明:容错率越高。存储数据越有限
			qrCode.setQrcodeErrorCorrect('M');
			//设置版本号	-40 共40个版本(1 -->21x21模块;40  -->177x177模块)		
			qrCode.setQrcodeVersion(version);
			//设置参数模型:N代表数字、A代表a-Z、B代表其他字节
			qrCode.setQrcodeEncodeMode('B');
			
			int imageSize = 67+12*(version-1);
			//将字符串打散成字节数组,并设置编码格式
			byte[] b = content.getBytes("UTF-8");	
			System.out.println(b.length);
			//创建画板:java自带的类
			BufferedImage bImage = new BufferedImage(imageSize, imageSize, BufferedImage.TYPE_INT_RGB);
			//根据画板拿画笔
			Graphics2D gs = bImage.createGraphics();
			
			
			gs.setBackground(Color.white);//设置二维码的背景色为白色			
			gs.clearRect(0, 0, imageSize, imageSize);//清除杂色
			
			gs.setColor(Color.black);//设置画笔(图像)的颜色:一般为黑色		
			int off = 2;//设置偏移量
			//输出内容》二维码
			if(b.length>0 && b.length<250){
				//将一维字节数组转化为二维,并限制最大字节数为119,印version为7
				boolean[][] codeOut = qrCode.calQrcode(b);
				for(int i=0;i
   
   
    
    bImage.getWidth()*2/10?bImage.getWidth()*2/10:imLogo.getWidth(null);
			int logoHeight = imLogo.getHeight(null)>bImage.getHeight()*2/10?bImage.getHeight()*2/10:imLogo.getHeight(null);
			/**
			 * logo放在二维码中心
			 */
			int x = (bImage.getWidth()-logoWidth)/2;
			int y = (bImage.getHeight()-logoHeight)/2;
			gs.drawImage(imLogo, x, y, logoWidth, logoHeight, null);
			//资源释放
			gs.dispose();
			//画板的内容的清空
			bImage.flush();
			
			//文件位置:"I:\\QRCodeTest\\张华明.jpg"
			File file = new File(imagePath);
			if(!file.exists()){
				file.createNewFile();
				//写入磁盘文件,生成二维码code图片
				ImageIO.write(bImage, "PNG", file);				
			}
			
		} catch (Exception e) {
			System.err.print("生成二维码IOException");
			e.printStackTrace();
		}
		
	}

}

   
   

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张小洛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值