yeyincai的专栏

二维码生成有很多sdk,,但本人认为zxing包最好用,以下就是二维码生成的类import java.awt.Color;import java.awt.Font;import java.awt.Graphics2D;import java.awt.Image;import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException;import javax.imageio.ImageIO; import org.apache.commons.lang3.StringUtils;import com.google.zxing.BarcodeFormat; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException;import com.google.zxing.common.BitMatrix;/** * @ClassName: GenerateQRCode * @Description: 生成二维码 * @author * @company * @date 2015-3-10 * @version V1.0 */ public final class QRCodeUtils {private static final QRCodeUtils instance = new QRCodeUtils();private QRCodeUtils() {}public static QRCodeUtils getInstance() {return instance;}private static final int BLACK = 0xff000000;private static final int WHITE = 0xFFFFFFFF;private static final int WIDTH = 400;//二维码宽private static final int HEIGHT = 400;//二维码高private static final int totalWidth = 400;//总宽private static final int totalHeight = 600;//总高int fontHeight=20,lineHeight=20;//字高,行高/*** @Title: generate* @Description: 生成二维码* @param assetsName*二维码图片名称* @param params*二维码信息* @param width*生成的图片的宽* @param height*生成的图片的高* @param path*二维码图片存放目录* @throws Exception* @return String 二维码图片名称* @author* @date 2012-11-9*/public String generate(String nick,String assetsName, String params, String path, String fileName) throws Exception {File file = new File(path);if (!file.exists()) {file.mkdirs();}//获取logo文件String head=path+"logo.png";// 二维码图片存放路径path = path.concat(fileName);file = new File(path);if (!file.exists()) {file.createNewFile();}BitMatrix bitMatrix = new MultiFormatWriter().encode(params,BarcodeFormat.QR_CODE, WIDTH, HEIGHT);//二维码的bufferedBufferedImage bufImgCode = toBufferedImage(bitMatrix);//构造新图像BufferedImage bufImg = new BufferedImage(totalWidth, totalHeight, BufferedImage.TYPE_INT_RGB); // 图片的大小Graphics2D gs = bufImg.createGraphics();gs.setBackground(Color.WHITE);gs.clearRect(0, 0, totalWidth, totalHeight);gs.setColor(Color.BLACK);//把二维码buffered画到gs里面if(bufImgCode!=null)gs.drawImage(bufImgCode, null, 0, 100);//headImage img = ImageIO.read(new File(head));//实例化一个Image对象。             gs.drawImage(img, 40, 30, null);if(StringUtils.isNotBlank(nick)){gs.setFont(new Font("微软雅黑", Font.BOLD, 20));gs.drawString(nick,160, 80);gs.setFont(new Font("微软雅黑", Font.BOLD, 16));gs.drawString("为您推荐最热门的手机业务",160, 110);}//把文字写到二维码下面if (StringUtils.isNotBlank(assetsName)) {gs.setFont(new Font("微软雅黑", Font.BOLD, 20));gs.drawString("欢迎扫一扫二维码",50, 500);gs.drawString("办理"+assetsName,50, 540);}ImageIO.write(bufImg, "png", file);return fileName;}public static void writeToFile(BitMatrix matrix, String format, File file)throws Exception {BufferedImage image = toBufferedImage(matrix);ImageIO.write(image, format, file);}public static BufferedImage toBufferedImage(BitMatrix matrix) {int width = matrix.getWidth();int height = matrix.getHeight();BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_ARGB);for (int x = 0; x < width; x++) {for (int y = 0; y < height; y++) {image.setRGB(x, y, matrix.get(x, y) == true ? BLACK : WHITE);}}return image;}public static void main(String[] args) {String path = "E:\\";try {QRCodeUtils.getInstance().generate("XXX","移动流量数据包50元","", path,"90006.png");System.out.println("生成二维码成功");} catch (WriterException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();} }}

享受每一刻的感觉,欣赏每一处的风景,这就是人生。

yeyincai的专栏

相关文章:

你感兴趣的文章:

标签云: