首页>代码>java QRCode二维码生成器>/QRCode/src/cn/edu/zafu/QRCode.java
package cn.edu.zafu;

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;

public class QRCode {

	public static void getQrcodeImgByContents(String contents, String imgPath) {
		int width = 140;
		int height = 140;
		try {
			Qrcode qrcode = new Qrcode();
		    // 设置二维码排错率,可选L(7%)、M(15%)、Q(25%)、H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小  
			qrcode.setQrcodeErrorCorrect('M');
			qrcode.setQrcodeEncodeMode('B');
			// 设置设置二维码尺寸,取值范围1-40,值越大尺寸越大,可存储的信息越大
			qrcode.setQrcodeVersion(7);

			BufferedImage bufImg = new BufferedImage(width, height,
					BufferedImage.TYPE_INT_BGR);

			Graphics2D gs = bufImg.createGraphics();
			gs.setBackground(Color.WHITE);
			gs.clearRect(0, 0, width, height);
			gs.setColor(Color.BLACK);
			//偏移量
			int pixoff = 2;

			byte[] contentBytes = contents.getBytes("UTF-8");
			if (contentBytes.length != 0 && contentBytes.length < 120) {
				boolean[][] codeOut = qrcode.calQrcode(contentBytes);
				for (int i = 0; i < codeOut.length; i++) {
					for (int j = 0; j < codeOut.length; j++) {
						if (codeOut[j][i]) {
							gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
						}
					}
				}
			} else {
				System.out.println("大小超出限制");
			}
			Image img = ImageIO.read(new File("logo5.png"));//实例化一个Image对象。
			gs.drawImage(img, 45, 45, 50,50, null);
			
			
			gs.dispose();
			bufImg.flush();
			File imgFile = new File(imgPath);
			ImageIO.write(bufImg, "png", imgFile);
			System.out.println("二维码生成成功!");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
		StringBuffer str = new StringBuffer();
		str.append("代码:290800000002273");
		str.append("\n");
		str.append("名称:HJJJKK");
		String content= "29081933423445";
		getQrcodeImgByContents(content, "1.png");
	}
}
最近下载更多
lilihehe  LV1 2023年10月19日
zxsyaa  LV1 2023年5月9日
2552655  LV1 2023年1月29日
June6666666666  LV1 2022年12月6日
微信网友_6224558473859072  LV1 2022年11月18日
OneTea  LV1 2022年9月29日
老舰了你  LV1 2022年9月24日
x0000_00_00  LV1 2022年9月5日
Useradmin128  LV1 2022年8月29日
168668  LV1 2022年7月22日
最近浏览更多
kingoneyang  LV13 2023年11月30日
lilihehe  LV1 2023年10月19日
qingye  LV1 2023年6月16日
zxh2023  LV1 2023年6月3日
zxsyaa  LV1 2023年5月9日
liminshabi  LV2 2023年4月14日
kkkukujiajia1 2023年2月20日
暂无贡献等级
2552655  LV1 2023年1月29日
June6666666666  LV1 2022年12月6日
leejunye 2022年11月21日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友