您当前的位置: 首页 >  Java

梁云亮

暂无认证

  • 2浏览

    0关注

    1211博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Java 二维码及条形码处理

梁云亮 发布时间:2022-03-15 14:45:51 ,浏览量:2

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             
关注
打赏
1665409997
查看更多评论
0.0430s