ZXING QR Coder
目录
ZXING QR Coder
工具类
ZXING QR Coder
ZXING 生成和读取二维码package com.example.java11boot.im;import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;
import com.google.zxing.qrcode.QRCodeWriter;import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;public class Ttest2 {public static void main(String[] args) {}public static void make() throws Exception {Map<EncodeHintType, String> hints = new HashMap<>();hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");hints.put(EncodeHintType.MARGIN, "1");// 外边距int width = 200;int height = 200;String content = "Hello World By 中国";QRCodeWriter qrCodeWriter = new QRCodeWriter();BitMatrix bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);Path path = Paths.get("D:/hello.PNG");MatrixToImageWriter.writeToPath(bitMatrix,"PNG", path);}public static void read () throws Exception {QRCodeReader qrCodeReader = new QRCodeReader();BufferedImage bufferedImage = ImageIO.read(new File("D:/hello.PNG"));LuminanceSource luminanceSource = new BufferedImageLuminanceSource(bufferedImage);BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(luminanceSource));Map<DecodeHintType, String> hints2 = new HashMap<>();hints2.put(DecodeHintType.CHARACTER_SET, "UTF-8");Result result = qrCodeReader.decode(binaryBitmap, hints2);System.out.println(result.getText());}
}=========================================================================================@GetMapping("/")public void qr(HttpServletResponse httpServletResponse) throws Exception {Map<EncodeHintType, String> hints = new HashMap<>();hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");hints.put(EncodeHintType.MARGIN, "1");// 外边距int width = 200;int height = 200;String content = "Hello World By 中国";QRCodeWriter qrCodeWriter = new QRCodeWriter();BitMatrix bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);ServletOutputStream outputStream = httpServletResponse.getOutputStream();MatrixToImageWriter.writeToStream(bitMatrix,"PNG", outputStream);outputStream.flush();outputStream.close();}=========================================================================================<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>
工具类
package com.example.demo2;import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;
import com.google.zxing.qrcode.QRCodeWriter;
import org.apache.commons.lang3.RandomStringUtils;import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;public class QRUtil {/*** 生成二维码** @param content 二维码内容* @param width 长度* @param height 高度* @param filePath 保留图片的文件夹路径* @return 二维码的文件名(绝对路径)* @throws Exception*/public static String generator(String content, Integer width, Integer height, String filePath) throws Exception {Map<EncodeHintType, String> hints = new HashMap<>();hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");hints.put(EncodeHintType.MARGIN, "1");// 外边距width = height == null ? 200 : width;height = height == null ? 200 : height;QRCodeWriter qrCodeWriter = new QRCodeWriter();BitMatrix bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);File file = new File(filePath);if (!file.exists()) {file.mkdirs();}String fp = filePath + File.separator + RandomStringUtils.randomAlphabetic(8) + ".PNG";Path path = Paths.get(fp);MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);return fp;}/*** 读取二维码** @param file 文件路径* @return 二维码内容* @throws Exception*/public static String read(File file) throws Exception {QRCodeReader qrCodeReader = new QRCodeReader();BufferedImage bufferedImage = ImageIO.read(file);LuminanceSource luminanceSource = new BufferedImageLuminanceSource(bufferedImage);BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(luminanceSource));Map<DecodeHintType, String> hints2 = new HashMap<>();hints2.put(DecodeHintType.CHARACTER_SET, "UTF-8");Result result = qrCodeReader.decode(binaryBitmap, hints2);return result.getText();}/*** 读取二维码** @param url 文件路径* @return 二维码内容* @throws Exception*/public static String read(URL url) throws Exception {QRCodeReader qrCodeReader = new QRCodeReader();BufferedImage bufferedImage = ImageIO.read(url);LuminanceSource luminanceSource = new BufferedImageLuminanceSource(bufferedImage);BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(luminanceSource));Map<DecodeHintType, String> hints2 = new HashMap<>();hints2.put(DecodeHintType.CHARACTER_SET, "UTF-8");Result result = qrCodeReader.decode(binaryBitmap, hints2);return result.getText();}
}
=============================带图========================
package com.example.springbootdemo;import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
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;import javax.imageio.ImageIO;
import java.awt.AlphaComposite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;public class QrCode {private final String DIR = "D:/z/";private final String ext = ".png";private final String LOGO = "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png";private final String CONTENT = "some content here";private final int WIDTH = 300;private final int HEIGHT = 300;public void generate() {Map<EncodeHintType, Object> hints = new HashMap<>();hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");hints.put(EncodeHintType.MARGIN, "0");// 外边距QRCodeWriter writer = new QRCodeWriter();BitMatrix bitMatrix = null;ByteArrayOutputStream os = new ByteArrayOutputStream();try {bitMatrix = writer.encode(CONTENT, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);// Load QR imageBufferedImage qrImage = MatrixToImageWriter.toBufferedImage(bitMatrix);// Load logo imageBufferedImage bufferedImage = ImageIO.read(new File("D:/1.png"));Image image = bufferedImage.getScaledInstance(75, 75, Image.SCALE_SMOOTH);BufferedImage overly =new BufferedImage(75, 75, BufferedImage.TYPE_INT_RGB);Graphics graphics = overly.getGraphics();graphics.drawImage(image, 0, 0, null);graphics.dispose();// Calculate the delta height and width between QR code and logoint deltaHeight = qrImage.getHeight() - overly.getHeight();int deltaWidth = qrImage.getWidth() - overly.getWidth();// Initialize combined imageBufferedImage combined = new BufferedImage(qrImage.getHeight(), qrImage.getWidth(), BufferedImage.TYPE_INT_ARGB);Graphics2D g = (Graphics2D) combined.getGraphics();// Write QR code to new image at position 0/0g.drawImage(qrImage, 0, 0, null);g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));// Write logo into combine image at position (deltaWidth / 2) and// (deltaHeight / 2). Background: Left/Right and Top/Bottom must be// the same space for the logo to be centeredg.drawImage(overly, (int) Math.round(deltaWidth / 2), (int) Math.round(deltaHeight / 2), null);// Write combined image as PNG to OutputStreamImageIO.write(combined, "png", os);// Store ImageFiles.copy( new ByteArrayInputStream(os.toByteArray()), Paths.get("D:/z/123." +ext), StandardCopyOption.REPLACE_EXISTING);} catch (WriterException e) {e.printStackTrace();//LOG.error("WriterException occured", e);} catch (IOException e) {e.printStackTrace();//LOG.error("IOException occured", e);}}private BufferedImage getOverly(String LOGO) throws IOException {File file = new File("D:/1.png");Image logo = ImageIO.read(new File("D:/1.png"));Image image = logo.getScaledInstance(80, 80, Image.SCALE_SMOOTH);BufferedImage tag = new BufferedImage(80, 80, BufferedImage.TYPE_INT_RGB);Graphics g = tag.getGraphics();g.drawImage(image, 0, 0, null); // 绘制缩小后的图g.dispose();return ImageIO.read(file);}private String generateRandoTitle(Random random, int length) {return random.ints(48, 122).filter(i -> (i < 57 || i > 65) && (i < 90 || i > 97)).mapToObj(i -> (char) i).limit(length).collect(StringBuilder::new, StringBuilder::append, StringBuilder::append).toString();}public static void main(String[] args) {QrCode qrCode = new QrCode();qrCode.generate();}
}
ZXING QR Coder相关推荐
- zxing qr区域判断_如何在Java中使用Zxing和JFreeSVG创建QR Code SVG?
zxing qr区域判断 在本文中,我们将研究如何使用Zxing QR代码生成库和JFreeSVG库在Java中创建QR Code SVG图像. QR码生成 下面的代码使用Zxing库创建一个表示QR ...
- zxing Qr二维码(二维码颜色、logo)
自己封装的二维码生成器,使用方便,案例代码如下 public static void main(String[] args) throws Exception {// logoBufferedImag ...
- 如何在Java中使用Zxing和JFreeSVG创建QR Code SVG?
在本文中,我们将研究如何使用Zxing QR代码生成库和JFreeSVG库在Java中创建QR Code SVG图像. QR码生成 下面的代码使用Zxing库创建一个表示QR Code的java.aw ...
- 【Android QR Code】开源项目:ZXing(一)导入项目
维基百科:QR Code http://en.wikipedia.org/wiki/QR_code 开源项目:ZXing http://code.google.com/p/zxing/ 1.下载源代码 ...
- Zxing和QR CODE 生成与解析二维码实例(普通篇)
首先下载对应的jar包,本实例用的是Zxing2.2jar 下载地址:http://download.csdn.net/detail/gao36951/8161861 Zxing是Google提供的关 ...
- Zxing和QR CODE 生成与解析二维码实例(带logo篇)
上一篇介绍了普通的二位码的生成与解析,本篇来介绍两种工具类生成带Logo的二维码的实例 下载jar包地址:http://download.csdn.net/detail/gao36951/816186 ...
- Android 系列 5.7使用Google ZXing条形码扫描器扫描条形码或QR码
5.7使用Google ZXing条形码扫描器扫描条形码或QR码 问题 您希望应用程序能够扫描条形码或QR码("QR"原本代表"快速反应"). 解 使用Inte ...
- Android 系列 5 7使用Google ZXing条形码扫描器扫描条形码或QR码
分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 5.7使 ...
- zxing源码分析——QR码部分
zxing源码分析--QR码部分 2013-07-10 17:16:03| 分类: 默认分类 | 标签: |字号大中小 订阅 Java代码结构: zxing源码的结构还是比较清晰的,有关Q ...
最新文章
- R语言使用anova函数进行方差分析比较两个回归分析模型的差异、从而决定是否删除某些预测变量(Comparing nested models using the anova function)
- 前端代码规范(es6,eslint,vue)
- JavaScript 仿ios滑动选择器
- 实战:向GitHub提交代码时触发Jenkins自动构建
- 程序员的工作,四种情景的处理
- css——常用选择器
- rabbitmq 常用的一些命令
- Javascript之把网页加入收藏夹功能
- cryptoJs 前端用法
- 修复YYC松鼠短视频系统我的收藏页面 没有返回按钮的bug
- 1. 线性回归/非线性回归代码
- 《A fast parallel algorithm for thinning digital patterns》论文算法python代码实现
- 计算机中正斜杠/与反斜杠\的区别
- 结构化思维的训练方式
- 日语学习(简单语法-2)
- 如何解除excel只读文件
- Not Shading
- lms算法的verilog实现_最小均方算法(LMS Algorithm)理论及DSP实现
- linux进入黑洞路由,BGP路由黑洞解决办法介绍
- iNode安装禁用了u盘停服务也没用