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

四、亲测效果展示:

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

1、所有文章未经授权禁止转载、摘编、复制或建立镜像,如有违反,追究法律责任。
2、本站文章部分来源注册用户发布或互联网收集而来,若有侵权,请邮件联系作者。
邮箱地址:wtao219@qq.com
THE END
分享
二维码
< <上一篇
下一篇>>