package com.feinno.wbs.web.util;
import java.awt.BasicStroke;
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.io.FileOutputStream;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.feinno.xframe.util.LogUtils;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* 二维码工具类
* @author yuandalong
*
*/
public class QrUtil {
private static final Logger log = LoggerFactory.getLogger(QrUtil.class);
/**
* 生成二维码
* @param str
* @param response
*/
public static void encode(String str,String logoPath,String eclevel,String qrcolor, HttpServletResponse response,int qrWidth,int qrHeight,int pix)
{
try{
Map<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");//使用小写的编码,大写会出现]Q2\000026开头内容
//ErrorCorrectionLevel.H 容错率:容错率越高,二维码的有效像素点就越多.
if (StringUtils.equals(eclevel, "M")) {
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
}else if (StringUtils.equals(eclevel, "L")) {
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
}else if (StringUtils.equals(eclevel, "Q")) {
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.Q);
}else if (StringUtils.equals(eclevel, "H")) {
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
}else{
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
}
hints.put(EncodeHintType.MARGIN, 0);//margin 边框设置
BitMatrix martrix = new MultiFormatWriter().encode(str,
BarcodeFormat.QR_CODE, qrWidth, qrHeight, hints);
//二维码
int bgColor = 0xFF000000;
if (!StringUtils.isBlank(qrcolor)) {
bgColor = Integer.parseInt(qrcolor.substring(4), 16);// 转换成int
}
BufferedImage bufferImage = new BufferedImage(martrix.getWidth(), martrix.getHeight(), BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < martrix.getWidth(); x++) {
for (int y = 0; y < martrix.getHeight(); y++) {
bufferImage.setRGB(x, y, martrix.get(x, y) ? bgColor : 0xFFFFFFFF);//填充,可设置颜色 颜色的取值为后6位
}
}
if (!StringUtils.isEmpty(logoPath)) {
File file = new File(logoPath);
if (file.exists()) {
int width = (int) (qrWidth / pix);
int height = (int)(qrHeight / pix);
Image thumb = generatThumbnails(file, null, width, height, true);
if (thumb != null) {
//插入logo
Graphics2D graph = bufferImage.createGraphics();
int w = thumb.getWidth(null);
int h = thumb.getHeight(null);
int x = (qrWidth - thumb.getWidth(null)) / 2; //设置logo的插入位置
int y = (qrHeight - thumb.getHeight(null)) / 2;
graph.drawImage(thumb, x, y, w, h, null);
Shape shape = new RoundRectangle2D.Float(x, y, w, h, 16, 16); // 后面两个参数是设置周边圆角,数值越大圆角越大
graph.setStroke(new BasicStroke(3f));
graph.draw(shape);
graph.dispose();
}
}
}
ImageIO.write(bufferImage, "jpg", response.getOutputStream());
}catch(Exception e){
log.error(LogUtils.getExceptionTrace(e));
}
}
/**
* 生成logo缩略图
* @param file 输入的文件流
* @param outputPath 输出路径
* @param width 缩略图宽
* @param height 缩略图高
* @param proportion 是否等比例缩放
*/
private static Image generatThumbnails(File file, String outputPath, int width, int height, boolean proportion)
{
log.info("缩略图宽:{}, 高:{}", new Object[]{width, height});
try {
BufferedImage img = ImageIO.read(file);
if (img.getWidth(null) == -1) {
log.info("图片无法读取!");
return null;
}
if (width <=0 || height <= 0) {
log.info("新生成的缩略图宽高不得小于0!");
return null;
}
int newWidth;
int newHeight;
if (proportion) {
//等比例压缩
double rate1 = ((double)img.getWidth(null)) / (double)width + 0.1;
double rate2 = ((double)img.getHeight(null)) / (double)height + 0.1;
log.info("缩放比例1:{}, 缩放比例2:{}, 原生宽度:{}, 原生高度:{}", new Object[]{rate1, rate2, img.getWidth(null), img.getHeight(null)});
//按照缩放比率大的进行缩放
double rate = rate1 > rate2 ? rate1 : rate2;
newWidth = (int) (((double) img.getWidth(null)) / rate);
newHeight = (int) (((double) img.getHeight(null)) / rate);
}else{
newWidth = width; // 输出的图片宽度
newHeight = height;
}
log.info("缩略图新的宽度:{}, 新的高度:{}", new Object[]{newWidth,newHeight});
BufferedImage tag = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
/**
* Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的
* 优先级比速度高 生成的图片质量比较好 但速度慢
*/
Image thumb = img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
tag.getGraphics().drawImage(thumb, 0, 0, null);
if (!StringUtils.isEmpty(outputPath)) {
FileOutputStream out = new FileOutputStream(outputPath);
// JPEGImageEncoder可适用于其他图片类型的转换
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
out.close();
}
return thumb;
} catch (IOException e) {
log.error(LogUtils.getExceptionTrace(e));
return null;
}
}
}
相关推荐
java 生成二维码 ZXing,使用ZXing生成二维码,jar 包和 案例
vb生成二维码QRCODE,使用zxing; 方便简单地在vs form上生成二维码; 使用于.NET2.0以上。
在做课设的时候要用到二维码,用到com.google.zxing,zxing.jar用于二维码生成,来分享一下~
用于普通的生成二维码 代码,控制二维码尺寸和密度等等
asp.net 动态生成二维码 需要引用的dll
ZXing,一个支持在图像中解码和生成条形码(如二维码、PDF 417、EAN、UPC、Aztec、Data Matrix、Codabar)的库。ZXing(“zebra crossing”)是一个开源的、多格式的、用Java实现的一维/二维条码图像处理库,具有到其他...
Java实现二维码生成 1、支持QRcode、ZXing 二维码生成、解析; 2、QRCode 方式生成二维码支持添加图片
支持一键生成二维码 一键扫描二维码 使用方便快捷。 如有问题可留言
zxing生成二维码,带logo的二维码,图片Base64编码。qrcode生成二维码。
这是Zxing的源码,没有打包成jar利于学习使用。...使用时注意path的默认地址,Main类中是生成二维码的代码,GetMain类中是读取二维码的代码。 eclipse中Ctrl+Shift+R快速查找类 希望对你有所帮助。
1.通过QRCode.jar包生成二维码,可设置二维码图片格式,二维码图片存放路径,二维码尺寸,二维码颜色 2.二维码扫描内容分为两种,1种为链接式,如:www.zdkc.com,通过链接展示访问的内容,1种为json数据展示,通过...
Android利用zxing生成二维码,识别二维码超详细、超简易demo,解压后用Android studio可直接打开,附教程:https://blog.csdn.net/mountain_hua/article/details/80646089
使用zxing生成解析二维码的实例 简单的几行代码就搞定 使用java实现的。 jar使用的2.2的版本支持JDK6 开发参考代码:https://my.oschina.net/dong706/blog/1608392
提供一刻android端的最新二维码生成与创建的库,直接引入到项目中,进行主项目依赖,简单快捷的创建和生成二维码
java二维码zxing-code生成附带源码。Test.java测试类 ,测试生成和解析的过程
对zxing的core3.4.0新增了使用byte数组生成二维码的方法和解码方法,新增内容详细代码和使用方法参考: 改写步骤:zxing使用byte数组生成二维码和解析二维码。...使用方法:java压缩字符串并生成二维码...
使用ZXing框架动态生成二维码,带有logo图片 , @Override public void onClick(View v) { /* * 按钮点击显示二维码 * */ //加一个测试地址,之后我们的二维码就是用这个地址来生成。扫描后直接进入百度,这个...
1)VS2019 C#开发生成二维码源码 2)根据内容生成条码图片 3)可以显示图片底部显示文字信息 全开源放心下载
JAVA生成Zxing二维码、读取二维码内容信息、利用二维码生成手机名片、生成带LOGO的二维码名片。附有效果图
Google二维码生成解析zxing库 Android 代码,可以实现二维码生成,亲测可用。 代码为2016年6月9日从GitHub下载的,版本为3.2.1 GitHub对应地址代码为: https://github.com/zxing/zxing jar包原始下载地址为:...