博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
生成二维码到本地
阅读量:5945 次
发布时间:2019-06-19

本文共 7137 字,大约阅读时间需要 23 分钟。

1.循环生成多张二维码

for (int i = 1; i <= 1000; i++) {
String code = UUID.randomUUID().toString().toUpperCase().replaceAll("-", ""); BottleSn bottleSn = new BottleSn(); bottleSn.setCode(code); bottleSnRepository.save(bottleSn); QRCodeUtil.generateQRImage("http://pangtun.silver-fortune.com.cn/h5/trackbottle/" + code, "D:/Pic/logo-pangtun.png", "D:/Pic/Picture/", i + ".png", "png"); } 2.工具类
package com.sfec.snmgr.track.utils; 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.Hashtable; import javax.imageio.ImageIO; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.client.j2se.MatrixToImageConfig; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import com.sfec.snmgr.track.vo.QRCodeParams; public class QRCodeUtil {
private static int width = 120; //二维码宽度 private static int height = 120; //二维码高度 private static int onColor = 0xFF000000; //前景色 private static int offColor = 0xFFFFFFFF; //背景色 private static int margin = 1; //白边大小,取值范围0~4 private static ErrorCorrectionLevel level = ErrorCorrectionLevel.L; //二维码容错率 /** * 生成二维码 * @param params * QRCodeParams属性:txt、fileName、filePath不得为空; */ public static void generateQRImage(QRCodeParams params)throws Exception{
if(params == null || params.getFileName() == null || params.getFilePath() == null || params.getTxt() == null){
throw new Exception("参数错误"); } try{
initData(params); String imgPath = params.getFilePath(); String imgName = params.getFileName(); String txt = params.getTxt(); if(params.getLogoPath() != null && !"".equals(params.getLogoPath().trim())){
generateQRImage(txt, params.getLogoPath(), imgPath, imgName, params.getSuffixName()); }else{
generateQRImage(txt, imgPath, imgName, params.getSuffixName()); } }catch(Exception e){
e.printStackTrace(); } } /** * 生成二维码 * @param txt //二维码内容 * @param imgPath //二维码保存物理路径 * @param imgName //二维码文件名称 * @param suffix //图片后缀名 */ public static void generateQRImage(String txt, String imgPath, String imgName, String suffix){
File filePath = new File(imgPath); if(!filePath.exists()){
filePath.mkdirs(); } File imageFile = new File(imgPath,imgName); Hashtable
hints = new Hashtable
(); // 指定纠错等级 hints.put(EncodeHintType.ERROR_CORRECTION, level); // 指定编码格式 hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); hints.put(EncodeHintType.MARGIN, margin); //设置白边 try {
MatrixToImageConfig config = new MatrixToImageConfig(onColor, offColor); BitMatrix bitMatrix = new MultiFormatWriter().encode(txt,BarcodeFormat.QR_CODE, width, height, hints); // bitMatrix = deleteWhite(bitMatrix); MatrixToImageWriter.writeToPath(bitMatrix, suffix, imageFile.toPath(), config); } catch (Exception e) {
e.printStackTrace(); } } /** * 生成带logo的二维码图片 * @param txt //二维码内容 * @param logoPath //logo绝对物理路径 * @param imgPath //二维码保存绝对物理路径 * @param imgName //二维码文件名称 * @param suffix //图片后缀名 * @throws Exception */ public static void generateQRImage(String txt, String logoPath, String imgPath, String imgName, String suffix) throws Exception{
File filePath = new File(imgPath); if(!filePath.exists()){
filePath.mkdirs(); } if(imgPath.endsWith("/")){
imgPath += imgName; }else{
imgPath += "/"+imgName; } Hashtable
hints = new Hashtable
(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); hints.put(EncodeHintType.ERROR_CORRECTION, level); hints.put(EncodeHintType.MARGIN, margin); //设置白边 BitMatrix bitMatrix = new MultiFormatWriter().encode(txt, BarcodeFormat.QR_CODE, width, height, hints); File qrcodeFile = new File(imgPath); writeToFile(bitMatrix, suffix, qrcodeFile, logoPath); } /** * * @param matrix 二维码矩阵相关 * @param format 二维码图片格式 * @param file 二维码图片文件 * @param logoPath logo路径 * @throws IOException */ public static void writeToFile(BitMatrix matrix,String format,File file,String logoPath) throws IOException {
BufferedImage image = toBufferedImage(matrix); Graphics2D gs = image.createGraphics(); int ratioWidth = image.getWidth()*2/10; int ratioHeight = image.getHeight()*2/10; //载入logo Image img = ImageIO.read(new File(logoPath)); int logoWidth = img.getWidth(null)>ratioWidth?ratioWidth:img.getWidth(null); int logoHeight = img.getHeight(null)>ratioHeight?ratioHeight:img.getHeight(null); int x = (image.getWidth() - logoWidth) / 2; int y = (image.getHeight() - logoHeight) / 2; gs.drawImage(img, x, y, logoWidth, logoHeight, null); gs.setColor(Color.black); gs.setBackground(Color.WHITE); gs.dispose(); img.flush(); if(!ImageIO.write(image, format, file)){
throw new IOException("Could not write an image of format " + format + " to " + file); } } public static BufferedImage toBufferedImage(BitMatrix matrix){
int width = matrix.getWidth(); int height = matrix.getHeight(); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); for(int x=0;x
hints = new Hashtable
(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); hints.put(EncodeHintType.ERROR_CORRECTION, level); hints.put(EncodeHintType.MARGIN, margin); //设置白边 BitMatrix bitMatrix = new MultiFormatWriter().encode(txt, BarcodeFormat.QR_CODE, width, height, hints); BufferedImage image = toBufferedImage(bitMatrix); Graphics2D gs = image.createGraphics(); int ratioWidth = image.getWidth()*2/10; int ratioHeight = image.getHeight()*2/10; //载入logo Image img = ImageIO.read(new File(logoPath)); int logoWidth = img.getWidth(null)>ratioWidth?ratioWidth:img.getWidth(null); int logoHeight = img.getHeight(null)>ratioHeight?ratioHeight:img.getHeight(null); int x = (image.getWidth() - logoWidth) / 2; int y = (image.getHeight() - logoHeight) / 2; gs.drawImage(img, x, y, logoWidth, logoHeight, null); gs.setColor(Color.black); gs.setBackground(Color.WHITE); gs.dispose(); return image; } }

转载于:https://www.cnblogs.com/chunming-an/p/10178039.html

你可能感兴趣的文章
pxc群集搭建
查看>>
JS中加载cssText延时
查看>>
常用的脚本编程知识点
查看>>
计算机网络术语总结4
查看>>
新手小白 python之路 Day3 (string 常用方法)
查看>>
soapUI的简单使用(webservice接口功能测试)
查看>>
框架 Hibernate
查看>>
python-while循环
查看>>
手机端上传图片及java后台接收和ajaxForm提交
查看>>
【MSDN 目录】C#编程指南、C#教程、ASP.NET参考、ASP.NET 4、.NET Framework类库
查看>>
jquery 怎么触发select的change事件
查看>>
angularjs指令(二)
查看>>
<气场>读书笔记
查看>>
领域驱动设计,构建简单的新闻系统,20分钟够吗?
查看>>
web安全问题分析与防御总结
查看>>
React 组件通信之 React context
查看>>
Linux下通过配置Crontab实现进程守护
查看>>
ios 打包上传Appstore 时报的错误 90101 90149
查看>>
Oracle推出轻量级Java微服务框架Helidon
查看>>
密码概述
查看>>