Maven依赖
com.google.zxing
javase
3.4.1
二维码
生成二维码
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
public class DemoTest {
public static void main(String[] args) throws Exception {
// 二维码原理(将字符串信息通过一规则转换为图像)
// 生成二维码
String name = "d:/my.png";
String content = "wwww.hcitlife.show";
int width = 400;
int height = 400;
// BitMatrix bm = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height);
Map hints = new HashMap();
hints.put(EncodeHintType.MARGIN, 2);
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix bm = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
MatrixToImageWriter.writeToStream(bm, "png", new FileOutputStream(name));
// BufferedImage bar = ImageIO.read(new File(name));
// 解压读取二维码
}
}
结果:
import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import javax.imageio.ImageIO;
import java.awt.Color;
import java.awt.image.BufferedImage;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
public class DemoTest {
public static void main(String[] args) throws Exception {
// 二维码原理(将字符串信息通过一规则转换为图像)
// 生成二维码
String name = "d:/my.png";
String content = "www.hcitlife.show";
int width = 400;
int height = 400;
// BitMatrix bm = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE,
// width, height);
Map hints = new HashMap();
hints.put(EncodeHintType.MARGIN, 2);
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix bm = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
MatrixToImageWriter.writeToStream(bm, "png", new FileOutputStream(name));
BufferedImage bar = ImageIO.read(new File(name));
BufferedImage ok = new BufferedImage(width, height, 1);
ok.getGraphics().drawImage(bar, 0, 0, width, height, null);
// System.out.println(String.format("%x", Color.RED.getRGB()));
// int[][] px = new int[width][height];
Random rand = new Random();
Color c = new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255), rand.nextInt(255));
for (int x = 0; x
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?