栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java 二维码及条形码处理

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java 二维码及条形码处理

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 < width; x++) {
			for (int y = 0; y < height; y++) {
				ok.setRGB(x, y, bar.getRGB(x, y) == -1 ? 0xffffffff : c.getRGB());
			}
			// if(x>5) break;
		}
		ImageIO.write(ok, "png", new File("d:/mymy.png"));

		// BufferedImage bar = ImageIO.read(new File(name));

		// 解压读取二维码
		System.out.println("end");
	}

}


	

解析二维码
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;

public class DemoTest {
	public static void main(String[] args) throws Exception {
		// 解析读取二维码
		 //解析图像
        BufferedImage i = ImageIO.read(new File("c:/my.png"));
        LuminanceSource source = new BufferedImageLuminanceSource(i);
        BinaryBitmap image = new BinaryBitmap(new HybridBinarizer(source));
        try{
            Result result = new MultiFormatReader().decode(image);
            String info = result.getText();
            System.out.println(info);
            if(info==null || "".equals(info)){
                System.out.println("没有二维码信息");
            }else if(info.contains("weixin")){  //qq mp
                System.out.println("有微信二维码,不允许");
            }else{
                System.out.println("有二维,可以使用");
            }
        }catch(Exception e){
            System.out.println("图像中没有二维码");
        }
	}

}

条形码
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.awt.Color;
import java.awt.Font;
import java.awt.RenderingHints;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
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:/bar.png";
		String content = "6923790798701";
		int width = 300;
		int height = 100;

		BitMatrix bm = new MultiFormatWriter().encode(content, BarcodeFormat.EAN_13, width, height);

		MatrixToImageWriter.writeToStream(bm, "png", new FileOutputStream(name));

		BufferedImage bar2 = ImageIO.read(new File(name));
		BufferedImage bar = new BufferedImage(bar2.getWidth(), bar2.getHeight() + 30, 1);

		Graphics2D g = bar.createGraphics();
		g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);

		g.setColor(Color.WHITE);
		g.fillRect(0, 0, bar2.getWidth(), bar2.getHeight() + 30);
		g.drawImage(bar2, 0, 15, bar2.getWidth(), bar2.getHeight(), null);
		g.fillRect(62, 100, 85, 15);
		g.fillRect(155, 100, 83, 15);
		g.setColor(Color.BLACK);
		g.setFont(new Font("", Font.PLAIN, 22));

		g.drawString(content.substring(0, 1), 40, 120);
		g.drawString(content.substring(1, 7), 68, 120);
		g.drawString(content.substring(7), 158, 120);
		g.dispose();

		ImageIO.write(bar, "png", new File("c:/bar2.png"));

	}

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/768463.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号