二维码

package shjt.license.util;

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

import javax.imageio.ImageIO;

import com.swetake.util.Qrcode;

public class QRcodeUtil {

	static int width = 90;
	static int height = 90;

	public static String getQRcode(String filepath) {
		try {
			Qrcode testQrcode = new Qrcode();

			testQrcode.setQrcodeErrorCorrect('M');

			testQrcode.setQrcodeEncodeMode('B');

			testQrcode.setQrcodeVersion(7);

			String testString = "http://218.242.180.164:8001/";

			byte[] d = testString.getBytes("gbk");

			BufferedImage bi = new BufferedImage(width, height, 12);

			Graphics2D g = bi.createGraphics();

			g.setBackground(Color.WHITE);

			g.clearRect(0, 0, width, height);

			g.setColor(Color.BLACK);

			if ((d.length > 0) && (d.length < 120)) {
				boolean[][] s = testQrcode.calQrcode(d);
				for (int i = 0; i < s.length; i++) {
					for (int j = 0; j < s.length; j++) {
						if (s[j][i]) {
							g.fillRect(j * 2, i * 2, 2, 2);
						}
					}

				}

			}

			g.dispose();

			bi.flush();

			File f = new File(filepath);

			if (!f.exists())
				f.mkdirs();
			ImageIO.write(bi, "jpg", f);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return filepath;
	}
}

  

原文地址:https://www.cnblogs.com/1025804158ysb/p/7489076.html