Qrcode生成二维码工具类

Qrcode生成二维码工具类

package com.qrcode.util;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.ObjectOutputStream;

import javax.imageio.ImageIO;

import com.swetake.util.Qrcode;

/**
 * 生成二维码工具类
 * @author pengfei.xiong
 */
public class QrcodeUtil {
    /**
     * 二维码生成核心方法
     * @param content 二维码存储的内容  
     * @param imgPath 二维码图片路径
     * @param logoPath 中间logo的图片路径
     * @throws Exception 异常
     */
     public static void  getQrocodeImg(String content,String imgPath,String logoPath) 

throws Exception{
         //实例化qecode对象
         Qrcode qrcode = new Qrcode();
         //设置排错率15%空间 错误信息
         qrcode.setQrcodeErrorCorrect('M');
         //二维码存储内容的编码集
         qrcode.setQrcodeEncodeMode('B');
         //设置二维码的版本 1-40 值越大尺寸越大,可存储的信息越大
         qrcode.setQrcodeVersion(15);

         //准备绘制二维码 画画条件:画板 画笔
         int width = 235;     //宽
         int height = 235;   //高
         //创建画板
         BufferedImage image = new BufferedImage(width, height, 

BufferedImage.TYPE_INT_RGB);
         //绘制工具
         Graphics2D gs =image.createGraphics();

         //设置背景颜色
         gs.setBackground(Color.white);
         //开始绘制矩形
         gs.clearRect(0, 0, width, height);
         //设置前景色
         gs.setColor(Color.black);

         //接受内容
        // byte[] codeOut = objectToByte(content);
         byte[] codeOut;
         codeOut = content.getBytes("utf-8");


         //通过byte数组获取boolean 类型二维码
         boolean[][] code = qrcode.calQrcode(codeOut);

         //遍历这个二维数组
         for (int i = 0; i < code.length; i++) {
            for (int j = 0; j < code.length; j++) {
                //如果是真的就涂黑
                if (code[i][j]) {
                    gs.fillRect(j*3+2, i*3+2, 3, 3);
                }
            }
        }


         //加入中间logo
         Image img= ImageIO.read(new File(logoPath));
         //80,80是距离gs两个边的距离,55,55是中间logo的大小
         gs.drawImage(img,80,80,75,75,null);

        //释放资源
         image.flush();
         gs.dispose();
         image.flush();

         //写入指定路径
         ImageIO.write(image, "png", new File(imgPath));
         System.out.println("二维码生成成功!");
     }

     public static void main(String[] args) {
        try {
            /*Student stu = new Student("test", 11);
            stu.setId(10);*/
            getQrocodeImg("http://baidu.com", 

"D:/Qrcode/qrcode.png","src/com/qrcode/util/images/p1.jpg");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

     /**
      * Object转换为byte[]
      * @param object
      * @return byte[]数组
      */
     public static byte[] objectToByte(java.lang.Object object){
         byte[] bytes = null;
         ByteArrayOutputStream bo = new ByteArrayOutputStream();
         try {
             ObjectOutputStream oo = new ObjectOutputStream(bo);
            oo.writeObject(object);
            bytes = bo.toByteArray();
            bo.close();
            oo.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bytes;

     }
}

这个工具类生成的二维码目前只能存储字符串和网页路径,要存更多内容和对象的话,自己改下,也希望能大神能贴出更好的!需要导入的包有Qrcode_swetake.jar,自行百度找一个
找不到的话这是jar链接 : http://download.csdn.net/download/xpf_user/10029120

转载于:https://www.cnblogs.com/xpf1009/p/9227312.html

Java生成二维码工具类的源码是一段用Java语言编写的程序代码,用于生成二维码图片。下面是一个简单的Java生成二维码工具类的源码示例: ```java import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import com.google.zxing.BarcodeFormat; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; public class QRCodeGenerator { public static void main(String[] args) { String text = "https://www.example.com"; // 要生成二维码的内容 int width = 300; // 二维码图片的宽度 int height = 300; // 二维码图片的高度 String format = "png"; // 二维码图片的格式 try { BufferedImage image = generateQRCode(text, width, height); saveImage(image, format, "qrcode.png"); System.out.println("二维码生成。"); } catch (Exception e) { e.printStackTrace(); } } // 生成二维码图片 public static BufferedImage generateQRCode(String text, int width, int height) throws Exception { QRCodeWriter qrCodeWriter = new QRCodeWriter(); BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height); int matrixWidth = bitMatrix.getWidth(); BufferedImage image = new BufferedImage(matrixWidth, matrixWidth, BufferedImage.TYPE_INT_RGB); image.createGraphics(); Graphics2D graphics = (Graphics2D) image.getGraphics(); graphics.setColor(Color.WHITE); graphics.fillRect(0, 0, matrixWidth, matrixWidth); graphics.setColor(Color.BLACK); for (int i = 0; i < matrixWidth; i++) { for (int j = 0; j < matrixWidth; j++) { if (bitMatrix.get(i, j)) { graphics.fillRect(i, j, 1, 1); } } } return image; } // 保存二维码图片 public static void saveImage(BufferedImage image, String format, String filePath) throws IOException { ImageIO.write(image, format, new File(filePath)); } } ``` 这个工具类使用了Google的ZXing库来生成二维码。主要包含两个方法:`generateQRCode()`用于生成二维码图片,`saveImage()`用于保存二维码图片到文件。 使用时,只需要指定要生成二维码的内容、图片的宽度和高度,然后调用`generateQRCode()`方法获取生成二维码图片,最后保存到文件即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值