生成二维码
现在办事情扫二维码很方便也很普及,java代码生成二维码:
引用对应的Service层的action层(业务管理层):
// 引入生成二维码实现工具类的后台
import com.xxx.xxx.xxx.service.QRCodeUtil;
// 创造对象实例 private QRCodeUtil QRCodeUtil = new QRCodeUtil();
// 扫描二维码之后所要跳转的页面地址 String urlcontent ="http://service.xxx.com/xxx/weixin/Pages/Service/xxxxDetail.html?ProjectGuid="+Project.getRowguid()+"&userGuid=admin&EditM=0";
// 调用QRCodeUtil中的generateQRCodeStream生成二维码字节数组
byte[] codebyte = QRCodeUtil.generateQRCodeStream(urlcontent, 100, 100);
// 调用QRCodeUtil中的insertImage将生成二维码图片放在固定的位置
QRCodeUtil.insertImage(codebyte, doc, "projectdetailtag");
Service层(业务逻辑层/实施层)其中所调用的二维码实现工具类的后台如下:
package com.xxx.xxxx.xxx.service; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Hashtable; import java.util.Random; import javax.imageio.ImageIO; import com.aspose.words.Document; import com.aspose.words.DocumentBuilder; import com.xxx.xxxx.BaseService; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; @SuppressWarnings("serial") public class QRCodeUtil extends BaseService { // public QRCodeUtil() { // super(); // } // // public QRCodeUtil(DataSourceConfig dataSource) { // super(dataSource); // } private static final String CHARSET = "utf-8"; private static final String FORMAT_NAME = "JPG"; private static final int QRCODE_SIZE = 300; // 二维码尺寸,宽度和高度均是300 private static final int WIDTH = 80; //LOGO宽度 private static final int HEIGHT = 80; //LOGO高度 /** * 生成二维码字节数组(字节流) * @param contents * @param width * @param height * @return * @throws WriterException * @throws FileNotFoundException * @throws IOException * @exception/throws [违例类型] [违例说明] * @see [类、类#方法、类#成员] */ public byte[] generateQRCodeStream(String contents, int width, int height) throws WriterException, FileNotFoundException, IOException{ QRCodeWriter writer = new QRCodeWriter(); BitMatrix matrix = writer.encode(contents, BarcodeFormat.QR_CODE, width, height); ByteArrayOutputStream imageStream = new ByteArrayOutputStream(); MatrixToImageWriter.writeToStream(matrix, "PNG", imageStream); byte[] taginfo = imageStream.toByteArray(); return taginfo; } /** * word标签中插入二维码图片 * @param imageByteStream * @param doc * @param tag 标签名称 */ public void insertImage(byte[] imageByteStream, Document doc, String tag) throws IOException, Exception { DocumentBuilder build = new DocumentBuilder(doc); build.moveToBookmark(tag); build.insertImage(imageByteStream); } /** * 生成二维码的方法 * @param content 目标url * @param imgPath logo图片地址 * @param needCompress 是否压缩logo * @return 二维码图片 * @throws Exception */ public static BufferedImage createImage(String content, String imgPath,boolean needCompress) throws Exception { Hashtable hints = new Hashtable(); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);//指定二维码的纠错等级为高级 hints.put(EncodeHintType.CHARACTER_SET, CHARSET);//指定字符编码为“utf-8” //hints.put(EncodeHintType.MARGIN, 1); //设置图片的边距 //参数1:内容,目标url,参数2:固定写法,参数3:二维码的宽度,参数4:二维码的高度,参数5:二维码属性设置 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);//生成的二维码image 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)) {//logo图片地址为null或空时 return image; //只返回二维码图片,无中间的logo } // 插入logo图片 参数1:二维码图片,参数2:logo图片地址,参数3:压缩图片 //QRCodeUtil.insertImage(image, imgPath, needCompress); return image; } /** * 生成二维码(内嵌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(99999) + ".jpg"; ImageIO.write(image, FORMAT_NAME, new File(destPath + "/" + file)); } /** * 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常) * @param destPath 存放目录 */ public static void mkdirs(String destPath) { File file = new File(destPath); if (!file.exists() && !file.isDirectory()) { file.mkdirs(); } } }
打听下这个变量名谁起的