在线生成二维码工具

首先引入com.google.zxing的jar包,在pom中:
<!-- 二维码生成工具 -->
<dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.3.3</version> </dependency> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.3.3</version> </dependency>
附上二维码生成工具代码,有些方法自行封装。
1 package com.wty.its.kit; 2 3 import com.google.zxing.*; 4 import com.google.zxing.client.j2se.BufferedImageLuminanceSource; 5 import com.google.zxing.common.BitMatrix; 6 import com.google.zxing.common.HybridBinarizer; 7 import com.google.zxing.qrcode.QRCodeReader; 8 import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; 9 10 import javax.imageio.ImageIO; 11 import java.awt.*; 12 import java.awt.image.BufferedImage; 13 import java.io.*; 14 import java.util.HashMap; 15 import java.util.Map; 16 17 /** 18 * @author acorn 19 * @Title: QRCodeKit 20 * @ProjectName QRCodeKit 21 * @Description: TODO 二维码生成工具 22 * @date 2018/10/2315:06 23 */ 24 public class QRCodeKit { 25 26 // 默认编码 27 public static final String QRCODE_DEFAULT_CHARSET = "UTF-8"; 28 29 // 默认二维码宽度 30 public static final int QRCODE_DEFAULT_WIDTH = 400; 31 32 // 默认二维码高度 33 public static final int QRCODE_DEFAULT_HEIGHT = 400; 34 35 // 图片文件输出类型 默认PNG 36 public static final String DEFAULT_OUT_IMAGE_TYPE = "PNG"; 37 38 private static final int BLACK = 0xFF000000; 39 40 private static final int WHITE = 0xFFFFFFFF; 41 42 43 public static BufferedImage createQRCode(String data){ 44 return createQRCode(data,QRCODE_DEFAULT_WIDTH,QRCODE_DEFAULT_HEIGHT); 45 } 46 47 public static BufferedImage createQRCode(String data, int width, int height){ 48 return createQRCode(data, QRCODE_DEFAULT_CHARSET, width, height); 49 } 50 51 52 public static BufferedImage createQRCode(String data, String charset, int width, int height) { 53 Map<EncodeHintType,Object> hintMap = new HashMap(); 54 hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); 55 hintMap.put(EncodeHintType.CHARACTER_SET, charset); 56 57 return createQRCode(data, charset, hintMap, width, height); 58 } 59 60 /** 61 * 生成二维码 bufferedImage 62 * @param content 二维码内容 63 * @param charset 编码 64 * @param hintMap 纠错级别 65 * @param width 二维码宽度 66 * @param heigt 二维码高度 67 * @return 68 */ 69 private static BufferedImage createQRCode(String content, String charset, Map hintMap,int width,int heigt){ 70 BitMatrix bitMatrix; 71 72 try { 73 bitMatrix = new MultiFormatWriter().encode(new String(content.getBytes(charset),charset), BarcodeFormat.QR_CODE,width,heigt,hintMap); 74 return toBufferedImage(bitMatrix); 75 } catch (WriterException e) { 76 throw new RuntimeException(e.getMessage()); 77 } catch (UnsupportedEncodingException e) { 78 throw new RuntimeException(e.getMessage()); 79 } 80 81 } 82 83 /** 84 * 生成位矩阵图片 85 * @param bitMatrix 86 * @return 87 */ 88 private static BufferedImage toBufferedImage(BitMatrix bitMatrix) { 89 90 int width = bitMatrix.getWidth(); 91 int height = bitMatrix.getHeight(); 92 93 BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); 94 95 for (int x = 0; x < width; x++) { 96 for (int y = 0; y < height; y++) { 97 image.setRGB(x, y, bitMatrix.get(x, y) ? BLACK : WHITE); 98 } 99 } 100 101 return image; 102 } 103 104 105 /** 106 * 生成带logo的二维码 107 * @param data 108 * @param charset 109 * @param hint 110 * @param width 111 * @param height 112 * @param logoFile 113 * @return 114 */ 115 public static BufferedImage createQRCodeWithLogo(String data, String charset, Map<EncodeHintType, ?> hint, 116 int width, int height, File logoFile) { 117 try { 118 BufferedImage qrcode = createQRCode(data, charset, hint, width, height); 119 120 //获取画笔 121 Graphics2D g = qrcode.createGraphics(); 122 123 BufferedImage logo = ImageIO.read(logoFile); 124 125 126 // logo大小为 二维码的20%大小 127 int logoWidth = logo.getWidth(null) > qrcode.getWidth()*2 /10 ? (qrcode.getWidth()*2 /10) : logo.getWidth(null); 128 int logoHeight = logo.getHeight(null) > qrcode.getHeight()*2 /10 ? (qrcode.getHeight()*2 /10) : logo.getHeight(null); 129 130 131 132 int x = (qrcode.getWidth() - logoWidth) / 2; 133 int y = (qrcode.getHeight() - logoHeight) / 2; 134 135 136 g.drawImage(logo, x, y, logoWidth, logoHeight, null); 137 g.setStroke(new BasicStroke(3f)); 138 g.setColor(Color.WHITE); 139 g.drawRoundRect(x, y, logoWidth, logoHeight, 5 ,5); 140 141 142 g.dispose(); 143 logo.flush(); 144 qrcode.flush(); 145 146 return qrcode; 147 } catch (IOException e) { 148 throw new RuntimeException(e.getMessage(), e); 149 } catch (Exception e) { 150 throw new RuntimeException(e.getMessage(), e); 151 } 152 } 153 154 public static BufferedImage createQRCodeWithLogo(String data, String logoFilePath) { 155 return createQRCodeWithLogo(data, QRCODE_DEFAULT_WIDTH, QRCODE_DEFAULT_HEIGHT, new File(logoFilePath)); 156 } 157 158 159 public static BufferedImage createQRCodeWithLogo(String data, File logoFile) { 160 return createQRCodeWithLogo(data, QRCODE_DEFAULT_WIDTH, QRCODE_DEFAULT_HEIGHT, logoFile); 161 } 162 163 public static BufferedImage createQRCodeWithLogo(String data, int width, int height, File logoFile) { 164 return createQRCodeWithLogo(data, QRCODE_DEFAULT_CHARSET, width, height, logoFile); 165 } 166 167 public static BufferedImage createQRCodeWithLogo(String data, int width, int height, String logoFilePath) { 168 return createQRCodeWithLogo(data, QRCODE_DEFAULT_CHARSET, width, height, new File(logoFilePath)); 169 } 170 171 public static BufferedImage createQRCodeWithLogo(String data, String charset, int width, int height, File logoFile) { 172 Map hint = new HashMap(); 173 hint.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); 174 hint.put(EncodeHintType.CHARACTER_SET, charset); 175 176 return createQRCodeWithLogo(data, charset, hint, width, height, logoFile); 177 } 178 179 180 /** 181 * 输出二维码 182 * @param bufferedImage 183 * @param formatName 184 * @param fileOutputStream 185 */ 186 public static void genQRCode(BufferedImage bufferedImage,String formatName,FileOutputStream fileOutputStream){ 187 188 try { 189 ImageIO.write(bufferedImage,formatName,fileOutputStream); 190 191 } catch (IOException e) { 192 e.printStackTrace(); 193 } 194 195 } 196 197 /** 198 * 输出流 199 * @param bufferedImage 200 * @param fileOutputStream 201 */ 202 public static void genQRCode(BufferedImage bufferedImage,FileOutputStream fileOutputStream){ 203 genQRCode(bufferedImage,DEFAULT_OUT_IMAGE_TYPE,fileOutputStream); 204 } 205 206 /** 207 * 输出文件对象 208 * @param bufferedImage 209 * @param outFile 210 */ 211 public static void genQRCode(BufferedImage bufferedImage,File outFile){ 212 213 try { 214 215 if (!outFile.exists()){ 216 outFile.createNewFile(); 217 } 218 219 genQRCode(bufferedImage,new FileOutputStream(outFile)); 220 System.out.println("生成二维码路径:" + outFile.getAbsolutePath()); 221 } catch (FileNotFoundException e) { 222 e.printStackTrace(); 223 } catch (IOException e) { 224 e.printStackTrace(); 225 } 226 } 227 228 /** 229 * 文件名称输出 230 * @param bufferedImage 231 * @param outQrcodePath 232 */ 233 public static void genQRCode(BufferedImage bufferedImage,String outQrcodePath){ 234 genQRCode(bufferedImage,new File(outQrcodePath)); 235 } 236 237 /** 238 * 239 * @param content 二维码内容 240 * @param outQrcodePath 二维码文件输出路径 241 */ 242 public static void genQRCode(String content,String outQrcodePath){ 243 genQRCode(createQRCode(content),outQrcodePath); 244 } 245 246 public static void genQRCode(String content,int width,int height,String outQrcodePath){ 247 genQRCode(createQRCode(content,width,height),outQrcodePath); 248 } 249 250 /** 251 * 带logo 二维码 252 * @param content 二维码内容 253 * @param logoPath logo路径 254 * @param outQrcodePath 图片输出路径 255 */ 256 public static void genQRCodeWithLogo(String content,String logoPath,String outQrcodePath){ 257 genQRCode(createQRCodeWithLogo(content,logoPath),outQrcodePath); 258 } 259 260 /** 261 * 带logo 二维码 262 * @param content 二维码内容 263 * @param width 二维码宽度 264 * @param height 二维码高度 265 * @param logoPath logo 路径 266 * @param outQrcodePath 二维码图片输出路径 267 */ 268 public static void genQRCodeWithLogo(String content,int width,int height,String logoPath,String outQrcodePath){ 269 genQRCode(createQRCodeWithLogo(content,width,height,logoPath),outQrcodePath); 270 } 271 272 /** 273 * 读二维码内容 274 * @param inputStream 275 * @return 276 * @throws IOException 277 */ 278 public static String readQrCode(InputStream inputStream){ 279 //从输入流中获取字符串信息 280 BufferedImage image = null; 281 try { 282 image = ImageIO.read(inputStream); 283 } catch (IOException e) { 284 e.printStackTrace(); 285 } 286 //将图像转换为二进制位图源 287 LuminanceSource source = new BufferedImageLuminanceSource(image); 288 289 BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); 290 291 QRCodeReader reader = new QRCodeReader(); 292 Result result = null ; 293 try { 294 result = reader.decode(bitmap); 295 } catch (ReaderException e) { 296 e.printStackTrace(); 297 } 298 299 return result.getText(); 300 } 301 302 /** 303 * 根据二维码文件对象读取内容 304 * @param qrcodeFile 305 * @return 306 */ 307 public static String readQrCode(File qrcodeFile){ 308 String text = null; 309 310 311 try { 312 text = readQrCode(new FileInputStream(qrcodeFile)); 313 } catch (FileNotFoundException e) { 314 e.printStackTrace(); 315 } 316 317 return text; 318 } 319 320 /** 321 * 根据二维码文件路径读取内容 322 * @param qrcodeFilePath 323 * @return 324 */ 325 public static String readQrCode(String qrcodeFilePath){ 326 327 return readQrCode(new File(qrcodeFilePath)); 328 } 329 330 /** 331 * 创建目录 332 * 333 * @param descDirName 目录名,包含路径 334 * @return 如果创建成功,则返回true,否则返回false 335 */ 336 public static boolean createDirectory(String descDirName) { 337 String descDirNames = descDirName; 338 if (!descDirNames.endsWith(File.separator)) { 339 descDirNames = descDirNames + File.separator; 340 } 341 File descDir = new File(descDirNames); 342 if (descDir.exists()) { 343 System.out.println("目录 " + descDirNames + " 已存在!"); 344 return false; 345 } 346 // 创建目录 347 if (descDir.mkdirs()) { 348 System.out.println("目录 " + descDirNames + " 创建成功!"); 349 return true; 350 } else { 351 System.out.println("目录 " + descDirNames + " 创建失败!"); 352 return false; 353 } 354 355 } 356 357 public static void main(String[] args) { 358 359 // genQRCode("i love acorn...","F:\\ceshi.png"); 360 361 // genQRCode("i love acorn ...",500,500,"F:\\ceshi.png"); 362 363 // genQRCodeWithLogo("i love acorn ...","F:\\logo.jpg","F:\\ceshi.png"); 364 365 genQRCodeWithLogo("i love acorn ...",400,400,"F:\\logo.jpg","F:\\ceshi.png"); 366 367 String text = readQrCode("F:\\ceshi.png"); 368 System.out.println(text); 369 } 370 }
浙公网安备 33010602011771号