生成一个简单的二维码:
1、导入二维码jar包:QRCode.jar;
2、两个类:二维码图片对象类:TwoCodeImage.java;
/**
* 二维码图片对象
* @author liupan
*
*/
public class TwoCodeImage implements QRCodeImage {
private BufferedImage bufImg;
public TwoCodeImage(BufferedImage bufImg) {
this.bufImg = bufImg;
}
@Override
public int getHeight() {
return bufImg.getHeight();
}
@Override
public int getPixel(int x, int y) {
return bufImg.getRGB(x, y);
}
@Override
public int getWidth() {
return bufImg.getWidth();
}
}
二维码生成公共工具类:TwoCodeUtils.java;
public class TwoCodeUtils {
public static String encoder(Object obj , String ent_type) throws BusinessException {
//String action = Global.MAP_PUBLIC_URL.get (ent_type); //获取企业类型 对应的外网公开查询的请求地址
D_scyp_register dInfo=null;
D_yljg_register dyljg=null;
String path ="";
// if(action == null) {
// new BusinessException ("二维码生成错误!");
// }
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
String content = "";
if(ent_type.equals("SCYP")){
dInfo=(D_scyp_register) obj;
path=Global.getAbsUploadPath(Global.QRCODE_PATH) + dInfo.getLicencecode() + ".png"; //生成二维码图片的路径“upload/qrcode/+许可证号.png”
content +="许可证号:" + dInfo.getLicencecode();
content +="\n企业名称:" + dInfo.getCorp_name();
content+="\n分类码:"+dInfo.getClass_code();
content+="\n注册地址:"+dInfo.getRegisteraddress();
content+="\n生产地址和生产范围:"+dInfo.getProd_scope();
if(dInfo.getSocial_credit_code().length()==18){
content +="\n社会信用代码:" + dInfo.getSocial_credit_code().subSequence(0, 10)+"****"+ dInfo.getSocial_credit_code().substring(14, 18);
}else{
content +="\n社会信用代码:" + dInfo.getSocial_credit_code();
}
content +="\n法定代表人:" + dInfo.getArtificialperson();
content +="\n企业负责人:" + dInfo.getEnterpriseprincipa();
content +="\n质量负责人:" + dInfo.getMassprincipal();
content +="\n有效期至:" + sdf.format(dInfo.getValid_end_date());
content += "\n发证机关:" + dInfo.getGrant_org();
content += "\n签发人:" + dInfo.getSigner();
content +="\n发证日期:" + sdf.format(dInfo.getGrant_date());
content +="\n日常监管机构:" + dInfo.getDaily_supervise_organ();
content +="\n日常监管人员:" + dInfo.getDaily_supervise_person();
content +="\n监督举报电话:" + 12331; //二维码的内容
content +="\n证书查询地址:" + "http://www.hebfda.gov.cn/CL0182/";
}else if(ent_type.equals("YLJG")){
dyljg=(D_yljg_register) obj;
path=Global.getAbsUploadPath(Global.QRCODE_PATH) + dyljg.getLicencecode() + ".png"; //生成二维码图片的路径“upload/qrcode/+许可证号.png”
content +="许可证号:" + dyljg.getLicencecode();
content +="\n医疗机构名称:" + dyljg.getCorp_name();
content+="\n注册地址:"+dyljg.getRegisteraddress();
content+="\n配置地址和配置范围:"+dyljg.getPzfw();
if(dyljg.getSocial_credit_code().length()==18){
content +="\n社会信用代码:" + dyljg.getSocial_credit_code().subSequence(0, 10)+"****"+ dyljg.getSocial_credit_code().substring(14, 18);
}else{
content +="\n社会信用代码:" + dyljg.getSocial_credit_code();
}
content +="\n医疗机构类别:" + dyljg.getEnterprisetype();
content +="\n法定代表人:" + dyljg.getArtificialperson();
content +="\n制剂室负责人:" + dyljg.getZjsfzr();
content +="\n质量负责人:" + dyljg.getMassprincipal();
content +="\n有效期至:" + sdf.format(dyljg.getValid_end_date());
content += "\n发证机关:" + dyljg.getGrant_org();
content += "\n签发人:" + dyljg.getSigner();
content +="\n发证日期:" + sdf.format(dyljg.getGrant_date());
content +="\n日常监管机构:" + dyljg.getDaily_supervise_organ();
content +="\n日常监管人员:" + dyljg.getDaily_supervise_person();
content +="\n监督举报电话:" + 12331; //二维码的内容
content +="\n证书查询地址:" + "http://www.hebfda.gov.cn/CL0182/";
}
TwoCodeUtils.encoderQRCode(content, path); //开始生成
/*System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$"+Global.XZSP_ADDRESS );
System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"+Global.QRCODE_PATH ); //局端访问网址
path = Global.XZSP_ADDRESS +Global.QRCODE_PATH + dInfo.getLicencecode() + ".png"; //二维码图片的最终路径
*/ return path;
}
/**
* 生成二维码(QRCode)图片
* @param content 存储内容
* @param imgPath 图片路径
*/
public static void encoderQRCode(String content, String imgPath) throws BusinessException {
TwoCodeUtils.encoderQRCode(content, imgPath, "png", 20);
}
/**
* 生成二维码(QRCode)图片
* @param content 存储内容
* @param output 输出流
*/
public static void encoderQRCode(String content, OutputStream output) throws BusinessException {
TwoCodeUtils.encoderQRCode(content, output, "png", 20);
}
/**
* 生成二维码(QRCode)图片
* @param content 存储内容
* @param output 输出流
* @param imgType 图片类型
*/
public static void encoderQRCode(String content, OutputStream output, String imgType) throws BusinessException {
TwoCodeUtils.encoderQRCode(content, output, imgType , 20);
}
/**
* 生成二维码(QRCode)图片
* @param content 存储内容
* @param imgPath 图片路径
* @param imgType 图片类型
*/
public static void encoderQRCode(String content, String imgPath, String imgType) throws BusinessException {
TwoCodeUtils.encoderQRCode(content, imgPath, imgType , 20);
}
/**
* 生成二维码(QRCode)图片
* @param content 存储内容
* @param imgPath 图片路径
* @param imgType 图片类型
* @param size 二维码尺寸
*/
public static void encoderQRCode (String content , String imgPath , String imgType , int size) throws BusinessException {
try {
BufferedImage bufImg = TwoCodeUtils.qRcodeCommon(content, imgType, size);
File imgFile = new File(imgPath);
ImageIO.write(bufImg, imgType, imgFile);
} catch (Exception e) {
throw new BusinessException("二维码生成错误!");
}
}
/**
* 生成二维码(QRCode)图片
* @param content 存储内容
* @param output 输出流
* @param imgType 图片类型
* @param size 二维码尺寸
*/
public static void encoderQRCode(String content , OutputStream output , String imgType , int size) throws BusinessException {
try {
BufferedImage bufImg = TwoCodeUtils.qRcodeCommon(content , imgType ,size);
// 生成二维码QRCode图片
ImageIO.write(bufImg, imgType , output);
} catch (Exception e) {
throw new BusinessException("二维码生成错误!");
}
}
/**
* 生成二维码(QRCode)图片的公共方法
* @param content 存储内容
* @param imgType 图片类型
* @param size 二维码尺寸
* @return
*/
public static BufferedImage qRcodeCommon (String content , String imgType , int size) throws BusinessException {
BufferedImage bufImg = null;
try{
Qrcode qrcode = new Qrcode();
// 设置二维码排错率,可选L(7%)、M(15%)、Q(25%)、H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小
qrcode.setQrcodeErrorCorrect('M');
qrcode.setQrcodeEncodeMode('B');
// 设置设置二维码尺寸,取值范围1-40,值越大尺寸越大,可存储的信息越大
qrcode.setQrcodeVersion(size);
// 获得内容的字节数组,设置编码格式
byte [] contentBytes = content.getBytes("utf-8");
int imgSize = 67 + 12 * (size - 1);
bufImg = new BufferedImage(imgSize ,imgSize , BufferedImage.TYPE_INT_RGB);
Graphics2D gs = bufImg.createGraphics();
// 设置背景颜色
gs.setBackground(Color.WHITE);
gs.clearRect(0, 0, imgSize, imgSize);
// 设定图像颜色> BLACK
gs.setColor(Color.black);
//设置偏移量,不设置可能导致解析出错
int pixoff = 2;
// 输出内容> 二维码
if (contentBytes.length > 0 && contentBytes.length < 800) {
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 {
throw new BusinessException("二维码生成错误!");
}
gs.dispose();
bufImg.flush();
} catch (Exception e) {
throw new BusinessException("二维码生成错误!");
}
return bufImg;
}
/**
* 解析二维码(QRCode)
* @param imgPath 图片路径
* @return
*/
public static String decoderQRCode(String imgPath) throws BusinessException {
String content = null;
// QRCode 二维码图片的文件
File file = new File(imgPath);
try {
BufferedImage bufImg = ImageIO.read(file);
QRCodeDecoder decoder = new QRCodeDecoder();
content = new String (decoder.decode(new TwoCodeImage(bufImg)) ,"utf-8");
} catch (Exception e) {
throw new BusinessException("解析二维码错误!");
}
return content;
}
/**
* 解析二维码(QRCode)
* @param input 输入流
* @return
*/
public static String decoderQRCode(InputStream input) throws BusinessException {
String content = null;
BufferedImage bufImg = null;
try {
bufImg = ImageIO.read(input);
QRCodeDecoder decoder = new QRCodeDecoder();
content = new String(decoder.decode(new TwoCodeImage(bufImg)) , "utf-8");
} catch (Exception e) {
throw new BusinessException("解析二维码错误!");
}
return content;
}}
调用工具类的生成方法,就OK了!
分享到:
相关推荐
C#.NET生成普通二维码方法及生成带图片二维码方法源码Demo,两种二维码都有,简单套用,代码易懂,有什么问题可以发送邮件至792166417@qq.com
主要介绍了nodejs实现超简单生成二维码的方法,结合实例形式分析了nodejs基于qr-image插件生成二维码的相关操作技巧,需要的朋友可以参考下
1.通过QRCode.jar包生成二维码,可设置二维码图片格式,二维码图片存放路径,二维码尺寸,二维码颜色 2.二维码扫描内容分为两种,1种为链接式,如:www.zdkc.com,通过链接展示访问的内容,1种为json数据展示,通过...
qrcode生成二维码在很多游戏应用被广泛使用,白鹭里可以直接使用,如果是其他引擎,只需要修改qrcode.draw()方法即可实现,简单,游戏应用内动态生成二维码超级方便
很多时候有批量生成二维码的需求,虽然有很多在线工具,但使用场景都无法完全匹配;所幸自己写个工具,方便工作,提高效率! 使用方法很简单,一键搞定;
1)VS2019 C#开发生成二维码源码 2)根据内容生成条码图片 3)可以显示图片底部显示文字信息 4)二维码支持导出PDF,方便打印 5)不使用第三方插件才能生成二维码 全开源放心下载
使用matlab生成二维码的一种简单方式,不过需要自己配置zxing包。zxing 包自行去github下载,在matlab路径中把zxing包添加上就可以生成二维码了。zxing包的下载地址在压缩包里
java生成二维码简单DEMO,生成图片到目录下,稍微改下可以直接用流输出显示。
vb生成二维码QRCODE,使用zxing; 方便简单地在vs form上生成二维码; 使用于.NET2.0以上。
JAVA生成二维码Demo源码,直接导入运行即可。一共只有一个包和两个类,很简单的小Demo,适合新手学习。
直接复制粘贴代码就行,简单便捷实用,生成二维码,可支持中文!
Android利用zxing生成二维码,识别二维码超详细、超简易demo,解压后用Android studio可直接打开,附教程:https://blog.csdn.net/mountain_hua/article/details/80646089
qrcode前端生成二维码,一个很简单很实用的二维码生成例子,只要修改成你想要的内容即可,非常实用,特此推荐给想自己制作二维码的小伙伴们~
利用jquery+jquery.qrcode.js实现的简单的二维码生成例子,其中包含中文转码。验证过了,IOS和安卓已验证可以正常扫描出来。
这是android app生成二维码最简单的方法,里面代码几行实现。 我有些教程,教程里面有如何把zxing库导入到工程,方法很简单。 要是学不会,不会用的话来骂我……
通过一个一般处 理程序提供一个生成二维码的方法,在页面的ImageUrl调用生成二维码图像。 用户可以根据输入的内容生成二维码,也 可以根据上传的二维码图解析出对应的内容。
使用asp.net生成二维码的简单示例. 其中主要是调用接口生成二维码图像在网页上显示
php生成二维码插件,简单好用 嗯嗯
简单实现二维码生成,无论是字符,汉字,数字都可以生成二维码。可以直接复制粘贴到自己的项目中。
用C#生成二维码实例,无错,打开即可测试,简单易懂