我不喜欢费话,直接上代码!这是一个工具类。 直接烤到项目中就可以用。生成二维码的图片后,就可以直接放到项目中去使用。 方便,简单。
//第一个类
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.InputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import jp.sourceforge.qrcode.QRCodeDecoder;
import jp.sourceforge.qrcode.exception.DecodingFailedException;
import com.swetake.util.Qrcode;
/**
* java 生成二维码
* 黑夜
*
**/
public class TwoDimensionCode {
private static String imgType = "png";//二维码图片格式
private static int size = 7;//二维码尺寸
private String codeType = "utf-8";
//设置二维码中间图片的宽高
private int imageWidth = 30;
private int imageHeight = 30;
/**
* 生成二维码(QRCode)图片
* @param content 存储内容
* @param imgPath 中间图片路径
* @param imgType 图片类型
* @param size 二维码尺寸
*
* @param outpath 输出二维码文件
*/
public BufferedImage encoderQRCode(String content, String imgPath, String imgType, int size,String outpath) {
try {
BufferedImage bufImg = this.qRCodeCommon(content, imgType, size);
//在二维码中间加入图片
BufferedImage _bufImg = createPhotoAtCenter(bufImg,imgPath);
File imgFile = new File(outpath);