二维码工具类

张军 3769 0

二维码工具类

package zj.bar.util;

import java.awt.BasicStroke;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Shape;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Hashtable;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Result;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import zj.bar.bean.BufferedImageLuminanceSource;
import zj.bar.bean.QRCodeToString;
import zj.bar.bean.StringToQRCode;
import zj.common.exception.ServiceException;

/**
 * 二维码工具类
 * 
 */
public class QRCodeUtil {
	/**
	 * 创建图片
	 * 
	 * @param param_content
	 * @param imgPath
	 * @param needCompress
	 * @return
	 */
	public static void createImage(StringToQRCode P_bean) {
		try {
			Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
			hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
			hints.put(EncodeHintType.CHARACTER_SET, P_bean.param_charset);
			hints.put(EncodeHintType.MARGIN, 1);
			BitMatrix bitMatrix = new MultiFormatWriter().encode(P_bean.param_content, BarcodeFormat.QR_CODE, P_bean.param_qrcode_size, P_bean.param_qrcode_size, hints);
			int width = bitMatrix.getWidth();
			int height = bitMatrix.getHeight();
			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, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
				}
			}
			// 插入图片
			P_bean.rtn_stream_cover_img = image;
			if (P_bean.param_file_logo == null || !P_bean.param_file_logo.exists()) {
				return;
			}
			insertImage(P_bean);
		} catch (Exception e) {
			throw new ServiceException(e);
		}
	}

	/**
	 * 插入LOGO
	 * 
	 * @param source
	 *            二维码图片
	 * @param imgPath
	 *            LOGO图片地址
	 * @param needCompress
	 *            是否压缩
	 * @throws Exception
	 */
	public static void insertImage(StringToQRCode P_bean) {
		try {
			if (!P_bean.param_file_logo.exists()) {
				System.err.println("" + P_bean.param_file_logo + "   该文件不存在!");
				return;
			}
			Image src = ImageIO.read(P_bean.param_file_logo);
			int width = src.getWidth(null);
			int height = src.getHeight(null);
			if (P_bean.param_compress) { // 压缩LOGO
				if (width > P_bean.param_width) {
					width = P_bean.param_width;
				}
				if (height > P_bean.param_height) {
					height = P_bean.param_height;
				}
				Image image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH);
				BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
				Graphics g = tag.getGraphics();
				g.drawImage(image, 0, 0, null); // 绘制缩小后的图
				g.dispose();
				src = image;
			}
			// 插入LOGO
			Graphics2D graph = P_bean.rtn_stream_cover_img.createGraphics();
			int x = (P_bean.param_qrcode_size - width) / 2;
			int y = (P_bean.param_qrcode_size - height) / 2;
			graph.drawImage(src, x, y, width, height, null);
			Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
			graph.setStroke(new BasicStroke(3f));
			graph.draw(shape);
			graph.dispose();
		} catch (Exception e) {
			throw new ServiceException(e);
		}
	}

	/**
	 * 生成二维码(内嵌LOGO)
	 * 
	 * @param param_content
	 *            内容
	 * @param imgPath
	 *            LOGO地址
	 * @param destPath
	 *            存放目录
	 * @param needCompress
	 *            是否压缩LOGO
	 * @throws Exception
	 */
	public static void encode(StringToQRCode P_bean) {
		try {
			createImage(P_bean);
			mkdirs(P_bean.rtn_file_dest);
			// String fileName = new Random().nextInt(99999999) + ".jpg";
			ImageIO.write(P_bean.rtn_stream_cover_img, P_bean.param_format_name, P_bean.rtn_file_dest);
		} catch (Exception e) {
			throw new ServiceException(e);
		}
	}

	/**
	 * 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)
	 * 
	 * @author lanyuan Email: mmm333zzz520@163.com
	 * @date 2013-12-11 上午10:16:36
	 * @param destPath
	 *            存放目录
	 */
	public static void mkdirs(File file) {
		// 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)
		if (file.isDirectory()) {
			if (!file.exists()) {
				file.mkdirs();
			}
		} else {
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
		}
	}

	/**
	 * 解析二维码
	 * 
	 * @param file
	 *            二维码图片
	 * @return
	 * @throws Exception
	 */
	public static void decode(QRCodeToString P_bean) {
		try {
			if (P_bean.param_file == null) {
				return;
			}
			BufferedImage image = ImageIO.read(P_bean.param_file);
			if (image == null) {
				return;
			}
			BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
			BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
			Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
			hints.put(DecodeHintType.CHARACTER_SET, P_bean.param_charset);
			Result result = new MultiFormatReader().decode(bitmap, hints);
			P_bean.rtn_content = result.getText();
		} catch (Exception e) {
			throw new ServiceException(e);
		}
	}

	public static void main(String[] args) {
		try {
			StringToQRCode P_string_to_qrcode = new StringToQRCode();
			P_string_to_qrcode.param_content = "weixin://wxpay/bizpayurl?pr=dQpQGGrzz";
			P_string_to_qrcode.rtn_file_dest = new File("E:/www.zhangjunbk.com/pay/www.zhangjunbk.com.jpg");
			encode(P_string_to_qrcode);
			System.out.println("生成图片对象:" + P_string_to_qrcode.rtn_file_dest);
			System.out.println("生成图片流:" + P_string_to_qrcode.rtn_stream_cover_img);
			QRCodeToString P_qrcode_to_string = new QRCodeToString();
			P_qrcode_to_string.param_file = P_string_to_qrcode.rtn_file_dest;
			decode(P_qrcode_to_string);
			System.out.println("返回内容:" + P_qrcode_to_string.rtn_content);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

二维码类StringToQRCode

/**
 * @Description  QRCode.java
 * @author 张军
 * @date 2022年2月18日 上午1:51:07
 * @version V1.0
 */
package zj.bar.bean;

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

/**
 * 二维码类
 * 
 * @version 1.00 (2014.09.15)
 * @author SHNKCS 张军 {@link <a target=_blank href="http://www.zhangjunbk.com">张军个人网站</a>&nbsp;&nbsp;&nbsp;&nbsp;<a target=_blank href="http://user.qzone.qq.com/360901061/">张军QQ空间</a>}
 */
public class StringToQRCode {
	public String param_charset = "utf-8";
	public String param_format_name = "JPG";
	/** 二维码尺寸 **/
	public int param_qrcode_size = 300;
	/** LOGO宽度 **/
	public int param_width = 60;
	/** LOGO高度 **/
	public int param_height = 60;
	/** 二维码图片内容 **/
	public String param_content;
	/** LOGO图片地址 **/
	public File param_file_logo;
	/** 是否压缩 **/
	public boolean param_compress = true;
	/** logo覆盖插入 **/
	public BufferedImage rtn_stream_cover_img;
	/** 二维码图片 **/
	public File rtn_file_dest;

}

二维码类QRCodeToString

/**
 * @Description  QRCode.java
 * @author 张军
 * @date 2022年2月18日 上午1:51:07
 * @version V1.0
 */
package zj.bar.bean;

import java.io.File;

/**
 * 二维码类
 * 
 * @version 1.00 (2014.09.15)
 * @author SHNKCS 张军 {@link <a target=_blank href="http://www.zhangjunbk.com">张军个人网站</a>&nbsp;&nbsp;&nbsp;&nbsp;<a target=_blank href="http://user.qzone.qq.com/360901061/">张军QQ空间</a>}
 */
public class QRCodeToString {
	public String param_charset = "utf-8";
	/** 二维码图片 **/
	public File param_file;
	/** 二维码图片内容 **/
	public String rtn_content;

}



更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论