java生成二维码使用QRCode和ZXing两种方式

QRCode是日本人开发的ZXing是google开发的

QRCode开发需要包http://download.csdn.net/detail/xiaokui_wingfly/7957815

ZXing开发需要包http://download.csdn.net/detail/u010457960/5301392

QRCode方式:

package cn.utils;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.imageio.ImageIO;

import jp.sourceforge.qrcode.QRCodeDecoder;
import jp.sourceforge.qrcode.data.QRCodeImage;
import jp.sourceforge.qrcode.exception.DecodingFailedException;

import com.swetake.util.Qrcode;

public class QRCodeUtils {
	/**
	 * 编码字符串内容到目标File对象中
	 *
	 * @param encodeddata 编码的内容
	 * @param destFile	生成file文件  1381090722   5029067275903
	 * @throws IOException
	 */
	public static void qrCodeEncode(String encodeddata, File destFile) throws IOException {
		Qrcode qrcode = new Qrcode();
		qrcode.setQrcodeErrorCorrect('M');	// 纠错级别(L 7%、M 15%、Q 25%、H 30%)和版本有关
		qrcode.setQrcodeEncodeMode('B');
		qrcode.setQrcodeVersion(7);		// 设置Qrcode包的版本

		byte[] d = encodeddata.getBytes("GBK");	// 字符集
		BufferedImage bi = new BufferedImage(139, 139, BufferedImage.TYPE_INT_RGB);
		// createGraphics	// 创建图层
		Graphics2D g = bi.createGraphics();

		g.setBackground(Color.WHITE);	// 设置背景颜色(白色)
		g.clearRect(0, 0, 139, 139);	// 矩形 X、Y、width、height
		g.setColor(Color.BLACK);	// 设置图像颜色(黑色)

		if (d.length > 0 && d.length < 123) {
			boolean[][] b = qrcode.calQrcode(d);
			for (int i = 0; i < b.length; i++) {
				for (int j = 0; j < b.length; j++) {
					if (b[j][i]) {
						g.fillRect(j * 3 + 2, i * 3 + 2, 3, 3);
					}
				}
			}
		}

//		Image img = ImageIO.read(new File("D:/tt.png"));  logo
//		g.drawImage(img, 25, 55,60,50, null);

		g.dispose(); // 释放此图形的上下文以及它使用的所有系统资源。调用 dispose 之后,就不能再使用 Graphics 对象
		bi.flush(); // 刷新此 Image 对象正在使用的所有可重构的资源

		ImageIO.write(bi, "png", destFile);
		System.out.println("Input Encoded data is:" + encodeddata);
	}

	/**
	 * 解析二维码,返回解析内容
	 *
	 * @param imageFile
	 * @return
	 */
	public static String qrCodeDecode(File imageFile) {
		String decodedData = null;
		QRCodeDecoder decoder = new QRCodeDecoder();
		BufferedImage image = null;
		try {
			image = ImageIO.read(imageFile);
		} catch (IOException e) {
			System.out.println("Error: " + e.getMessage());
		}

		try {
			decodedData = new String(decoder.decode(new J2SEImage(image)), "GBK");
			System.out.println("Output Decoded Data is:" + decodedData);
		} catch (DecodingFailedException dfe) {
			System.out.println("Error: " + dfe.getMessage());
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return decodedData;
	}

	public static void main(String[] args) {
		String FilePath = "d:/qrcode.png";
		File qrFile = new File(FilePath);

		// 二维码内容
		String encodeddata = "Hello X-rapido";
		try {
			QRCodeUtils.qrCodeEncode(encodeddata, qrFile);
		} catch (IOException e) {
			e.printStackTrace();
		}

		// 解码
		String reText = QRCodeUtils.qrCodeDecode(qrFile);
		System.out.println(reText);
	}
}

class J2SEImage implements QRCodeImage {
	BufferedImage image;

	public J2SEImage(BufferedImage image) {
		this.image = image;
	}

	public int getWidth() {
		return image.getWidth();
	}

	public int getHeight() {
		return image.getHeight();
	}

	public int getPixel(int x, int y) {
		return image.getRGB(x, y);
	}
}

Zxing方式

package cn.utils.code;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

import javax.imageio.ImageIO;

import com.google.zxing.common.BitMatrix;

/**
 * 二维码的生成需要借助MatrixToImageWriter类,该类是由Google提供的,可以将该类直接拷贝到源码中使用
 */
public class MatrixToImageWriter {
	private static final int BLACK = 0xFF000000;
	private static final int WHITE = 0xFFFFFFFF;

	private MatrixToImageWriter() {
	}

	public static BufferedImage toBufferedImage(BitMatrix matrix) {
		int width = matrix.getWidth();
		int height = matrix.getHeight();
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		for (int x = 0; x < width; x++) {
			for (int y = 0; y < height; y++) {
				image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
			}
		}
		return image;
	}

	public static void writeToFile(BitMatrix matrix, String format, File file) throws IOException {
		BufferedImage image = toBufferedImage(matrix);
		if (!ImageIO.write(image, format, file)) {
			throw new IOException("Could not write an image of format " + format + " to " + file);
		}
	}

	public static void writeToStream(BitMatrix matrix, String format, OutputStream stream) throws IOException {
		BufferedImage image = toBufferedImage(matrix);
		if (!ImageIO.write(image, format, stream)) {
			throw new IOException("Could not write an image of format " + format);
		}
	}
}

编写main方法

package cn.utils.code;

import java.io.File;
import java.util.Hashtable;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;

public class Main {
	public static void main(String[] args) throws Exception {
		String text = "NiuYueYue I Love You!"; // 二维码内容
		int width = 300; // 二维码图片宽度
		int height = 300; // 二维码图片高度
		String format = "gif";// 二维码的图片格式

		Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
		hints.put(EncodeHintType.CHARACTER_SET, "utf-8");	// 内容所使用字符集编码

		BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
		// 生成二维码
		File outputFile = new File("d:" + File.separator + "new.gif");
		MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);
	}
}

两种方式都可以进行创建二维码并解析,一般项目中很少使用自己写二维码,在网站上提供了很多在线二维码生成器,其中高级设计模式中有很多功能

推荐一个网站 草料二维码 http://cli.im/text

时间: 2025-01-18 10:53:10

java生成二维码使用QRCode和ZXing两种方式的相关文章

java生成二维码(需导入第三方ZXing.jar包)

//这个类是用来解析,通过图片解析该图片的网页链接是什么 package util; import java.awt.Graphics2D;import java.awt.geom.AffineTransform;import java.awt.image.BufferedImage; import com.google.zxing.LuminanceSource; public class BufferedImageLuminanceSource extends LuminanceSource

java 生成 二维码 和jquery 生成二维码

生成二维码 Java 生成二维码: 思路为拿到jar 包知道里面的方法使用 Step one : 在https://github.com/zxing中下载(点击网页中名为 zxing 的a标签,跳转到源码页面,点击release 查看所有发布的源码,下载zip压缩文件 Step two:  解压文件后打开文件夹,将core包和javase包 中的com包拷贝到一java项目src目录下.右键导出 jar file  得到一个二维码开发的jar包 Step three: 进行二维码制作 impor

java生成二维码(带logo)

之前写过一篇不带logo的二维码实现方式,采用QRCode和ZXing两种方式 http://blog.csdn.net/xiaokui_wingfly/article/details/39476185 这里介绍一下ZXing的带logo实现方式,具体实现参考一下代码,测试使用ZXingCodeUtil的main方法. 视频链接地址稍后更新,视频地址中包含图片二维码流输出方式 LogoConfig logo背景配置类 ZXingConfig 二维码配置信息 BufferedImageLumina

vue2.0 自定义 生成二维码(QRCode)组件

1.自定义 生成二维码组件 QRCode.vue <!-- 生成二维码 组件 --> <template> <canvas class="qrcode-canvas" :class="{show: show}" :style="{height: size + 'px', width: size + 'px'}" :height="size" :width="size" ref=

java实现二维码的生成和解析:QRCode、zxing 两种方式

第一种:QRCode.jar,使用QRCode生成和解析二维码 1.导入jar包  2.代码 (1)QRCodeUtil .java 1 import com.swetake.util.Qrcode; 2 import jp.sourceforge.qrcode.QRCodeDecoder; 3 4 import javax.imageio.ImageIO; 5 import java.awt.*; 6 import java.awt.image.BufferedImage; 7 import

java生成二维码

具体代码如下,作为一个新手,期待与你一起交流: 1 import java.awt.Color; 2 import java.awt.Graphics2D; 3 import java.awt.image.BufferedImage; 4 import java.io.File; 5 6 import javax.imageio.ImageIO; 7 8 import com.swetake.util.Qrcode; 9 public class QRCodeEncoderHandler { 1

java生成二维码的三个工具

1.  使用SwetakeQRCode在Java项目中生成二维码 http://swetake.com/qr/ 下载地址 或着http://sourceforge.jp/projects/qrcode/downloads/28391/qrcode.zip 这个是日本人写的,生成的是我们常见的方形的二维码 可以用中文 如:5677777ghjjjjj  有朋友问我要这个图片生成的代码,我就在网上搜索然后整理了一个类,首先要把SwetakeQRCode的jar包qrcode.jar放在工程的编译路径

JAVA生成二维码图片代码

首先需要导入 QRCode.jar 包 下载地址看这里   http://pan.baidu.com/s/1o6qRFqM import java.awt.Color;import java.awt.Graphics2D;import java.awt.Image;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import java.io.UnsupportedEncoding

java生成二维码的几种方式

1: 使用SwetakeQRCode在Java项目中生成二维码 http://swetake.com/qr/ 下载地址 或着http://sourceforge.jp/projects/qrcode/downloads/28391/qrcode.zip 这个是日本人写的,生成的是我们常见的方形的二维码 可以用中文 如:5677777ghjjjjj 2: 使用BarCode4j生成条形码和二维码 BarCode4j网址:http://sourceforge.NET/projects/barcode