Java实现二维码生成的代码方法
程序员文章站
2024-02-15 08:59:52
1、支持qrcode、zxing 二维码生成、解析;
2、qrcode 方式生成二维码支持添加图片,如下:
package common;
impo...
1、支持qrcode、zxing 二维码生成、解析;
2、qrcode 方式生成二维码支持添加图片,如下:
package common; import java.awt.color; import java.awt.graphics2d; import java.awt.image; import java.awt.image.bufferedimage; import java.io.file; import java.io.ioexception; import java.util.hashmap; import java.util.map; import javax.imageio.imageio; import jp.sourceforge.qrcode.qrcodedecoder; import jp.sourceforge.qrcode.exception.decodingfailedexception; import com.google.zxing.barcodeformat; import com.google.zxing.binarizer; import com.google.zxing.binarybitmap; import com.google.zxing.encodehinttype; import com.google.zxing.luminancesource; import com.google.zxing.multiformatreader; import com.google.zxing.multiformatwriter; import com.google.zxing.notfoundexception; import com.google.zxing.writerexception; import com.google.zxing.common.bitmatrix; import com.google.zxing.common.hybridbinarizer; import com.swetake.util.qrcode; /** * 二维码生成工具类 * @author cloud * @data 2016-12-15 * qrcode */ public class qrcodeutil { //二维码颜色 private static final int black = 0xff000000; //二维码颜色 private static final int white = 0xffffffff; /** * <span style="font-size:18px;font-weight:blod;">zxing 方式生成二维码</span> * @param text <a href="javascript:void();" rel="external nofollow" >二维码内容</a> * @param width 二维码宽 * @param height 二维码高 * @param outputpath 二维码生成保存路径 * @param imagetype 二维码生成格式 */ public static void zxingcodecreate(string text, int width, int height, string outputpath, string imagetype){ map<encodehinttype, string> his = new hashmap<encodehinttype, string>(); //设置编码字符集 his.put(encodehinttype.character_set, "utf-8"); try { //1、生成二维码 bitmatrix encode = new multiformatwriter().encode(text, barcodeformat.qr_code, width, height, his); //2、获取二维码宽高 int codewidth = encode.getwidth(); int codeheight = encode.getheight(); //3、将二维码放入缓冲流 bufferedimage image = new bufferedimage(codewidth, codeheight, bufferedimage.type_int_rgb); for (int i = 0; i < codewidth; i++) { for (int j = 0; j < codeheight; j++) { //4、循环将二维码内容定入图片 image.setrgb(i, j, encode.get(i, j) ? black : white); } } file outputimage = new file(outputpath); //如果图片不存在创建图片 if(!outputimage.exists()) outputimage.createnewfile(); //5、将二维码写入图片 imageio.write(image, imagetype, outputimage); } catch (writerexception e) { e.printstacktrace(); system.out.println("二维码生成失败"); } catch (ioexception e) { e.printstacktrace(); system.out.println("生成二维码图片失败"); } } /** * <span style="font-size:18px;font-weight:blod;">二维码解析</span> * @param analyzepath 二维码路径 * @return * @throws ioexception */ @suppresswarnings({ "rawtypes", "unchecked" }) public static object zxingcodeanalyze(string analyzepath) throws exception{ multiformatreader formatreader = new multiformatreader(); object result = null; try { file file = new file(analyzepath); if (!file.exists()) { return "二维码不存在"; } bufferedimage image = imageio.read(file); luminancesource source = new luminancesourceutil(image); binarizer binarizer = new hybridbinarizer(source); binarybitmap binarybitmap = new binarybitmap(binarizer); map hints = new hashmap(); hints.put(encodehinttype.character_set, "utf-8"); result = formatreader.decode(binarybitmap, hints); } catch (notfoundexception e) { e.printstacktrace(); } return result; } /** * <span style="font-size:18px;font-weight:blod;">qrcode 方式生成二维码</span> * @param content 二维码内容 * @param imgpath 二维码生成路径 * @param version 二维码版本 * @param isflag 是否生成logo图片 为null不生成 */ public static void qrcodecreate(string content, string imgpath, int version, string logopath){ try { qrcode qrcodehandler = new qrcode(); //设置二维码排错率,可选l(7%) m(15%) q(25%) h(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小 qrcodehandler.setqrcodeerrorcorrect('m'); //n代表数字,a代表字符a-z,b代表其他字符 qrcodehandler.setqrcodeencodemode('b'); //版本1为21*21矩阵,版本每增1,二维码的两个边长都增4;所以版本7为45*45的矩阵;最高版本为是40,是177*177的矩阵 qrcodehandler.setqrcodeversion(version); //根据版本计算尺寸 int imgsize = 67 + 12 * (version - 1) ; byte[] contentbytes = content.getbytes("gb2312"); bufferedimage 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 < 130) { boolean[][] codeout = qrcodehandler.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 { system.err.println("qrcode content bytes length = " + contentbytes.length + " not in [ 0,130 ]. "); } /* 判断是否需要添加logo图片 */ if(logopath != null){ file icon = new file(logopath); if(icon.exists()){ int width_4 = imgsize / 4; int width_8 = width_4 / 2; int height_4 = imgsize / 4; int height_8 = height_4 / 2; image img = imageio.read(icon); gs.drawimage(img, width_4 + width_8, height_4 + height_8,width_4,height_4, null); gs.dispose(); bufimg.flush(); }else{ system.out.println("error: login图片不存在!"); } } gs.dispose(); bufimg.flush(); //创建二维码文件 file imgfile = new file(imgpath); if(!imgfile.exists()) imgfile.createnewfile(); //根据生成图片获取图片 string imgtype = imgpath.substring(imgpath.lastindexof(".") + 1, imgpath.length()); // 生成二维码qrcode图片 imageio.write(bufimg, imgtype, imgfile); } catch (exception e) { e.printstacktrace(); } } /** * <span style="font-size:18px;font-weight:blod;">qrcode二维码解析</span> * @param codepath 二维码路径 * @return 解析结果 */ public static string qrcodeanalyze(string codepath) { file imagefile = new file(codepath); bufferedimage bufimg = null; string decodeddata = null; try { if(!imagefile.exists()) return "二维码不存在"; bufimg = imageio.read(imagefile); qrcodedecoder decoder = new qrcodedecoder(); decodeddata = new string(decoder.decode(new imageutil(bufimg)), "gb2312"); } catch (ioexception e) { system.out.println("error: " + e.getmessage()); e.printstacktrace(); } catch (decodingfailedexception dfe) { system.out.println("error: " + dfe.getmessage()); dfe.printstacktrace(); } return decodeddata; } } 3、最后贴测试代码: package test; import java.awt.image.bufferedimage; import java.io.inputstream; import java.net.url; import javax.imageio.imageio; import common.imageutil; import common.qrcodeutil; import jp.sourceforge.qrcode.qrcodedecoder; /** * 二维码生成测试类 * @author cloud * @data 2016-11-21 * qrcodetest */ public class qrcodetest { public static void main(string[] args) throws exception { /** * qrcode 二维码生成测试 * qrcodeutil.qrcodecreate("http://blog.csdn.net/u014266877", "e://qrcode.jpg", 15, "e://icon.png"); */ /** * qrcode 二维码解析测试 * string qrcodeanalyze = qrcodeutil.qrcodeanalyze("e://qrcode.jpg"); */ /** * zxingcode 二维码生成测试 * qrcodeutil.zxingcodecreate("http://blog.csdn.net/u014266877", 300, 300, "e://zxingcode.jpg", "jpg"); */ /** * zxingcode 二维码解析 * string zxinganalyze = qrcodeutil.zxingcodeanalyze("e://zxingcode.jpg").tostring(); */ system.out.println("success"); } }
上一篇: Kotlin教程之基本数据类型
下一篇: java版微信公众平台后台接入