1. HTTP3W博客首页
  2. Java

Java生成二维码图片

一、新建二维码生成工具类

CreateQrImage.java
package com.wash.car.common.utils.qrcode;

import java.io.File;
import java.io.IOException;
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.common.BitMatrix;

/**
 * 二维码图片
 * 
 * @author 
 * 
 */
public class CreateQrImage {
	
	/**
	 * 生成二维码图片
	 * @param content 二维码
	 * @param baseId 图片名称
	 * @param outputFile 输出到文件
	 */
	public static void createBaseQrcodeImage(String content, int baseId, String outputFile){
		try {
			MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
			Map<EncodeHintType, String> hints = new HashMap<EncodeHintType, String>();
			hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
			BitMatrix bitMatrix = multiFormatWriter.encode(content,BarcodeFormat.QR_CODE, 200, 200, hints);
			String path = getImagePath()+outputFile;
			File file = new File(path);
			if (!file.exists()) {
				file.mkdirs();
			}
			File fileImage = new File(path, baseId + ".gif");
			MatrixToImageWriter.writeToFile(bitMatrix, "gif", fileImage);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 生成二维码图片
	 * @param content 二维码
	 * @param fileName 图片名称
	 * @param outputFile 输出到文件
	 */
	public static void createBaseQrcodeImage(String content, String fileName, String outputFile){
		try {
			MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
			Map<EncodeHintType, String> hints = new HashMap<EncodeHintType, String>();
			hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
			BitMatrix bitMatrix = multiFormatWriter.encode(content,BarcodeFormat.QR_CODE, 200, 200, hints);
			String path = getImagePath()+outputFile;
			File file = new File(path);
			if (!file.exists()) {
				file.mkdirs();
			}
			File fileImage = new File(path, fileName + ".gif");
			MatrixToImageWriter.writeToFile(bitMatrix, "gif", fileImage);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 删除二维码图片
	 * @param baseId 图片名称
	 * @param outputFile 输出到文件
	 */
	public static void baseDeleteImage(int baseId,String outputFile){
		try {
			File fileImage = new File(getImagePath()+outputFile, baseId + ".gif");
			if (fileImage.exists()) {
				fileImage.delete();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 删除二维码图片
	 * @param baseId 图片名称
	 * @param outputFile 输出到文件
	 */
	public static void baseDeleteImage(String fileName,String outputFile){
		try {
			File fileImage = new File(getImagePath()+outputFile, fileName + ".gif");
			if (fileImage.exists()) {
				fileImage.delete();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 生成二维码图片
	 * 
	 * @param content
	 */
	public static void createImage(String content, int deviceId) {
		try {
			MultiFormatWriter multiFormatWriter = new MultiFormatWriter();

			Map<EncodeHintType, String> hints = new HashMap<EncodeHintType, String>();
			hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
			BitMatrix bitMatrix = multiFormatWriter.encode(content,
					BarcodeFormat.QR_CODE, 200, 200, hints);

			String path = getImagePath()+"output";

			File file = new File(path);
			if (!file.exists()) {
				file.mkdirs();
			}

			File fileImage = new File(path, deviceId + ".gif");
			MatrixToImageWriter.writeToFile(bitMatrix, "gif", fileImage);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * 生成二维码图片
	 * 
	 * @param content
	 */
	public static void createImage(String content, String fileName) {
		try {
			MultiFormatWriter multiFormatWriter = new MultiFormatWriter();

			Map<EncodeHintType, String> hints = new HashMap<EncodeHintType, String>();
			hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
			BitMatrix bitMatrix = multiFormatWriter.encode(content,
					BarcodeFormat.QR_CODE, 200, 200, hints);

			String path = getImagePath()+"output";

			File file = new File(path);
			if (!file.exists()) {
				file.mkdirs();
			}

			File fileImage = new File(path, fileName + ".gif");
			MatrixToImageWriter.writeToFile(bitMatrix, "gif", fileImage);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	
	/**
	 * 图片路径
	 * @return
	 */
	public  static String getImagePath() {

		StringBuffer msg = new StringBuffer();
		String path = CreateQrImage.class.getProtectionDomain().getCodeSource()
				.getLocation().getFile();
		try {
			path = new File(path).getCanonicalPath();
		} catch (IOException e) {
			e.printStackTrace();
		}
		int pos = path.lastIndexOf("WEB-INF");
		if (pos >= 0) {
			path = path.substring(0, pos);
			msg.setLength(0);
			msg.append(path);
		}
		return msg.toString();
	}

	/**
	 * 删除二维码图片
	 * 
	 * @param content
	 */
	public static void deleteImage(int deviceId) {
		try {

			File fileImage = new File(getImagePath()+"output", deviceId + ".gif");

			if (fileImage.exists()) {
				fileImage.delete();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}

二、辅助工具类:

MatrixToImageWriter.java

package com.wash.car.common.utils.qrcode;

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;

public final 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);
		}
	}

}

三,导入工具包 zxing-core.jar

链接:https://pan.baidu.com/s/1TccMuVpOXOM9j1yQi7z1Vg
提取码:ht3w

四、亲测效果展示:

Java生成二维码图片

提示:如果需要源码包的小伙伴,请右边窗口qq或站内给我留言哦!!告诉我您的邮箱地址即可

1、原创文章,作者:诺米,如若转载,请注明出处:https://www.http3w.com/archives/641

2、本站内容若有雷同从属巧合,若侵犯了您的权益,请联系本站删除,E-mail: wtao219@qq.com

发表评论

您的电子邮箱地址不会被公开。

联系我们

254007489

在线咨询:点击这里给我发消息

邮件:wtao219@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息