亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

二維碼工具類

張軍 6845 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:/m.eyofj.com/pay/m.eyofj.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://m.eyofj.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://m.eyofj.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條評論
主站蜘蛛池模板: 日韩欧美亚洲一区 | 韩国理论片在线看2828dy | 久久香蕉国产线看观看99 | 四虎永久在线精品免费影视 | 亚洲成a人v欧美综合天 | 亚洲a成人7777777久久 | 五月婷六月婷婷 | 牛牛本精品99久久精品 | 国产精品亚洲午夜不卡 | 色噜噜狠狠一区二区三区 | 五月婷亚洲 | 香蕉在线视频网站 | 伊人第一路线 | 毛片免费毛片一级jjj毛片 | 精品在线一区二区三区 | 中文字幕亚洲无线码在线一区 | 亚洲欧美综合久久 | 亚洲图片欧美另类 | 国产精品欧美一区二区在线看 | 日韩天堂视频 | 国产99精品在线观看 | 色就操 | 欧美一级成人免费大片 | 国产精品爱久久久 | a毛片免费在线观看 | 日本b站一卡二不卡 | 久久久久在线 | 一级毛片免费视频观看 | 超91视频| 久久婷婷婷 | 久久93精品国产91久久综合 | 网络色综合久久 | 中文字幕在线精品不卡 | 第一区免费在线观看 | 天天干天天操天天操 | 日本最猛黑人xxxx猛交 | 国产一级黄色录像 | 精品无人区乱码1区2区 | 伦理亚洲| 香蕉视频网站在线播放 | 国产欧美日韩精品一区二区三区 |