Java 生成二维码
来源:互联网 发布:济宁网络问政平台首页 编辑:程序博客网 时间:2024/06/16 18:54
Java生成二维码工具
使用google条码解析库zxing.
功能: 为给定字符串生成二维码
1. 返回BufferImage格式
2. 写入到指定文件中
3. 写入到OutputStream中
代码如下:
package org.ramer.common.utils;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import java.io.OutputStream;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.WriterException;import com.google.zxing.common.BitMatrix;import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;/** * The QR code util. * * @author RAMER * @date Apr 19, 2017 */public class QRcodeUtil{ private static final int BLACK = 0xFF000000; private static final int WHITE = 0xFFFFFFFF; /** * Generate QR code img. * * @param content the content * @param width the width * @param height the height * @return the buffered image */ public static BufferedImage generateQRcodeImg(String content, Integer width, Integer height) { BitMatrix bitMatrix = null; Hashtable<EncodeHintType, Object> hints = new Hashtable<>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); hints.put(EncodeHintType.MARGIN, 0); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); try { bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints); } catch (WriterException e) { e.printStackTrace(); return null; } int w = bitMatrix.getWidth(); int h = bitMatrix.getHeight(); BufferedImage bufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_BGR); for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { bufferedImage.setRGB(x, y, bitMatrix.get(x, y) ? BLACK : WHITE); } } return bufferedImage; } /** * Generate QR code file. * * @param content the content * @param width the width * @param height the height * @param format the format,eg: gif,jpeg * @param file the file * @return the file null ,if exec error. */ public static File generateQRcodeFile(String content, Integer width, Integer height, String format, File file) { if (!file.exists()) { try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); return null; } } BufferedImage bufferedImage = generateQRcodeImg(content, width, height); try { ImageIO.write(bufferedImage, format, file); } catch (IOException e) { e.printStackTrace(); return null; } return file; } /** * Write to stream. * * @param content the content * @param width the width * @param height the height * @param format the format,eg: gif,jpeg * @param outputStream the output stream */ public static void writeToStream(String content, Integer width, Integer height, String format,OutputStream outputStream) { BufferedImage bufferedImage = generateQRcodeImg(content, width, height); try { ImageIO.write(bufferedImage, format, outputStream); } catch (IOException e) { e.printStackTrace(); } }}
测试:
public class QRcodeUtilTest{ @Test public void testGenerateQRcodeFile() { File file = new File("Z:/Desktop/qrcode-ramer.jpeg"); QRcodeUtil.generateQRcodeFile("ramer", 80, 80, "jpeg", file); } @Test public void testWriteToStream() { try (FileOutputStream outputStream = new FileOutputStream( new File("Z:/Desktop/qrcode2-ramer.jpeg"))) { QRcodeUtil.writeToStream("ramer", 80, 80, "jpeg", outputStream); } catch (IOException e) { e.printStackTrace(); } }}
1 0
- java二维码 生成二维码
- Java生成二维码解析二维码
- java 生成二维码,解析二维码
- Java生成PDF417二维码
- Java 生成二维码
- java生成二维码
- java生成二维码
- java生成二维码
- JAVA 生成二维码 代码
- Java 生成二维码
- java 二维码生成
- java二维码生成
- JAVA生成解析二维码
- Java 生成二维码
- java 生成二维码
- java生成二维码
- Java生成二维码
- java生成二维码图片
- 音视频开发术语介绍
- 位运算常用技巧
- VM虚拟机 系统出现鼠标定位不准确、双鼠标问题
- windows下"8080"端口被占用解决办法
- Video Analysis相关领域介绍之Action Recognition(行为识别)
- Java 生成二维码
- java 虚拟机 体系结构
- 爬虫-伪装和防Ban
- C语言文件权限/数学函数总结
- Zjnu Stadium(并查集)
- Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
- 关于Android 设置应用 ID applicationId
- ORM和JPA概念
- 【精】Android高效加载大图、多图解决方案,有效避免程序OOM