二维码生成Java实现代码
程序员文章站
2024-02-13 23:34:52
本文实例为大家分享了二维码生成java实现代码,供大家参考,具体内容如下
package com.yihaomen.barcode;
import java...
本文实例为大家分享了二维码生成java实现代码,供大家参考,具体内容如下
package com.yihaomen.barcode; 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.io.outputstream; import java.util.hashtable; import java.util.random; 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; /** * 二维码工具类 * */ public class qrcodeutil { private static final string charset = "utf-8"; private static final string format_name = "jpg"; // 二维码尺寸 private static final int qrcode_size = 300; // logo宽度 private static final int width = 60; // logo高度 private static final int height = 60; private static bufferedimage createimage(string content, string imgpath, boolean needcompress) throws exception { hashtable<encodehinttype, object> hints = new hashtable<encodehinttype, object>(); hints.put(encodehinttype.error_correction, errorcorrectionlevel.h); hints.put(encodehinttype.character_set, charset); hints.put(encodehinttype.margin, 1); bitmatrix bitmatrix = new multiformatwriter().encode(content, barcodeformat.qr_code, qrcode_size, 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); } } if (imgpath == null || "".equals(imgpath)) { return image; } // 插入图片 qrcodeutil.insertimage(image, imgpath, needcompress); return image; } /** * 插入logo * * @param source * 二维码图片 * @param imgpath * logo图片地址 * @param needcompress * 是否压缩 * @throws exception */ private static void insertimage(bufferedimage source, string imgpath, boolean needcompress) throws exception { file file = new file(imgpath); if (!file.exists()) { system.err.println(""+imgpath+" 该文件不存在!"); return; } image src = imageio.read(new file(imgpath)); int width = src.getwidth(null); int height = src.getheight(null); if (needcompress) { // 压缩logo if (width > width) { width = width; } if (height > height) { height = 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 = source.creategraphics(); int x = (qrcode_size - width) / 2; int y = (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(); } /** * 生成二维码(内嵌logo) * * @param content * 内容 * @param imgpath * logo地址 * @param destpath * 存放目录 * @param needcompress * 是否压缩logo * @throws exception */ public static void encode(string content, string imgpath, string destpath, boolean needcompress) throws exception { bufferedimage image = qrcodeutil.createimage(content, imgpath, needcompress); mkdirs(destpath); string file = new random().nextint(99999999)+".jpg"; imageio.write(image, format_name, new file(destpath+"/"+file)); } /** * 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常) * @author lanyuan * email: mmm333zzz520@163.com * @date 2013-12-11 上午10:16:36 * @param destpath 存放目录 */ public static void mkdirs(string destpath) { file file =new file(destpath); //当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常) if (!file.exists() && !file.isdirectory()) { file.mkdirs(); } } /** * 生成二维码(内嵌logo) * * @param content * 内容 * @param imgpath * logo地址 * @param destpath * 存储地址 * @throws exception */ public static void encode(string content, string imgpath, string destpath) throws exception { qrcodeutil.encode(content, imgpath, destpath, false); } /** * 生成二维码 * * @param content * 内容 * @param destpath * 存储地址 * @param needcompress * 是否压缩logo * @throws exception */ public static void encode(string content, string destpath, boolean needcompress) throws exception { qrcodeutil.encode(content, null, destpath, needcompress); } /** * 生成二维码 * * @param content * 内容 * @param destpath * 存储地址 * @throws exception */ public static void encode(string content, string destpath) throws exception { qrcodeutil.encode(content, null, destpath, false); } /** * 生成二维码(内嵌logo) * * @param content * 内容 * @param imgpath * logo地址 * @param output * 输出流 * @param needcompress * 是否压缩logo * @throws exception */ public static void encode(string content, string imgpath, outputstream output, boolean needcompress) throws exception { bufferedimage image = qrcodeutil.createimage(content, imgpath, needcompress); imageio.write(image, format_name, output); } /** * 生成二维码 * * @param content * 内容 * @param output * 输出流 * @throws exception */ public static void encode(string content, outputstream output) throws exception { qrcodeutil.encode(content, null, output, false); } /** * 解析二维码 * * @param file * 二维码图片 * @return * @throws exception */ public static string decode(file file) throws exception { bufferedimage image; image = imageio.read(file); if (image == null) { return null; } bufferedimageluminancesource source = new bufferedimageluminancesource( image); binarybitmap bitmap = new binarybitmap(new hybridbinarizer(source)); result result; hashtable<decodehinttype, object> hints = new hashtable<decodehinttype, object>(); hints.put(decodehinttype.character_set, charset); result = new multiformatreader().decode(bitmap, hints); string resultstr = result.gettext(); return resultstr; } /** * 解析二维码 * * @param path * 二维码图片地址 * @return * @throws exception */ public static string decode(string path) throws exception { return qrcodeutil.decode(new file(path)); } public static void main(string[] args) throws exception { string text = "http://www.yihaomen.com"; qrcodeutil.encode(text, "c:/me.jpg", "c:/barcode", true); } }
大家可以参考专题:java二维码进行学习
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: java 爬虫详解及简单实例