java生成圆形二维码logo

自定义生成二维码,可以根据自己的喜欢在二维码中添加图片。有些代码是参考网上某位大神的,如有相同之处,请给我留言,我加上您的名字或者不让参考发表,则可删除。
jar提取地址:
链接: https://pan.baidu.com/s/1nXiTbQXvNQUUYXsvLbSeyg 提取码: pdnd


import com.swetake.util.Qrcode;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 *  二维码生成工具类
 */
public class QRCodeUtil {
    //生成二维码图片
    public static void getQrcode(String content,String imgPath) throws IOException {

        int width=140;
        int height=140;
        //创建二维码
        Qrcode  qrcode=new Qrcode();
        //设置二维码的纠错值
        qrcode.setQrcodeErrorCorrect('M');
        //设置二维码编码方式
        qrcode.setQrcodeEncodeMode('B');
        //设置二维码的版本,也叫信息容量
        qrcode.setQrcodeVersion(7);
        System.out.println(content);
        //将信息转化为字节数组
        byte[] contentByte=content.getBytes("gb2312");
        //相当于创建一个画布
        BufferedImage bufferedImage=new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
        //得到画笔
        Graphics2D gs=bufferedImage.createGraphics();
        //设置背景颜色
        gs.setBackground(Color.white);
        //画出一个矩形区域
        gs.clearRect(0,0,width,height);
        //设置图形颜色
        gs.setColor(Color.red);
        // 设置偏移量 不设置可能导致解析出错
        int pixoff = 2;
        //输出二维码图片
        if(contentByte.length>0 && contentByte.length<120){
            boolean[][] codeOut=qrcode.calQrcode(contentByte);
            //循环输出,形成矩阵
            for(int i=0;i<codeOut.length;i++){
                for(int j=0;j<codeOut.length;j++){
                    if(codeOut[j][i]){  //如果方阵下面有内容就画出一个小矩形
                        gs.fillRect(j*3+pixoff,i*3+pixoff,3,3);
                    }
                }
            }
        } else {
            System.err.println("QRCode content bytes length = " + contentByte.length + " not in [ 0,120 ]." );
        }

        gs.dispose();
        bufferedImage.flush();
        File file=new File(imgPath);
        //生产二维码图片进行输出
        ImageIO.write(bufferedImage,"png",file);
    }
    //生成带有logo的二维码图片
    public static void getLogoQrcode(String content,String imgPath,int width,int height,String logoPath) throws IOException {
        //创建二维码
        Qrcode  qrcode=new Qrcode();
        //设置二维码的纠错值
        qrcode.setQrcodeErrorCorrect('M');
        //设置二维码编码方式
        qrcode.setQrcodeEncodeMode('B');
        //设置二维码的版本,也叫信息容量
        qrcode.setQrcodeVersion(7);
        System.out.println(content);
        //将信息转化为字节数组
        byte[] contentByte=content.getBytes("gb2312");
        //相当于创建一个画布
        BufferedImage bufferedImage=new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
        //得到画笔
        Graphics2D gs=bufferedImage.createGraphics();
        //设置背景颜色
        gs.setBackground(Color.white);
        //画出一个矩形区域
        gs.clearRect(0,0,width,height);
        //设置图形颜色
        gs.setColor(Color.black);
        // 设置偏移量 不设置可能导致解析出错
        int pixoff = 2;
        //输出二维码图片
        if(contentByte.length>0 && contentByte.length<120){
            boolean[][] codeOut=qrcode.calQrcode(contentByte);
            //循环输出,形成矩阵
            for(int i=0;i<codeOut.length;i++){
                for(int j=0;j<codeOut.length;j++){
                    if(codeOut[j][i]){  //如果方阵下面有内容就画出一个小矩形
                       gs.fillRect(j*3+pixoff,i*3+pixoff,3,3);
                    }
                }
            }
        } else {
            System.err.println("QRCode content bytes length = " + contentByte.length + " not in [ 0,120 ]." );
        }
        //绘制logo图像
        BufferedImage img = ImageIO.read(new File(logoPath));
        //Image img=ImageIO.read(new File(logoPath));
        gs.drawImage(convertCircular(img),50,50,40,40,null);
        gs.dispose();
        bufferedImage.flush();
        File file=new File(imgPath);
        //生产二维码图片进行输出
        ImageIO.write(bufferedImage,"png",file);
    }
    /**
     * 传入的图像必须是正方形的 才会 圆形  如果是长方形的比例则会变成椭圆的
     * @param bi1 用户头像地址
     * @return
     * @throws IOException
     */
    public static BufferedImage convertCircular(BufferedImage bi1) throws IOException{
        //这种是黑色底的
       // BufferedImage bi2 = new BufferedImage(bi1.getWidth(),bi1.getHeight(),BufferedImage.TYPE_INT_RGB);

        //透明底的图片
        BufferedImage bi2 = new BufferedImage(bi1.getWidth(),bi1.getHeight(),BufferedImage.TYPE_4BYTE_ABGR);
        Ellipse2D.Double shape = new Ellipse2D.Double(0,0,bi1.getWidth(),bi1.getHeight());
        Graphics2D g2 = bi2.createGraphics();
        g2.setClip(shape);
        // 使用 setRenderingHint 设置抗锯齿
        g2.drawImage(bi1,0,0,null);
        //设置颜色
        g2.setBackground(Color.green);
        g2.dispose();
        return bi2;
    }
    public static void fixqrcode(String content,String imgPath,String logoPath) throws Exception{
        int width=140;
        int height=140;
        getLogoQrcode(content,imgPath,width,height,logoPath);
    }
    public static void main(String[] args) throws Exception{
        //输入二维码的信息
        String content="my name is wangjinsheng";
        String time=System.currentTimeMillis()+"";
        //存放二维码的地址
        String imgPath="F:\\tmp\\"+time+".png";
        //产生不带有logo的二维码
        getQrcode(content,imgPath);
        //logo图片的地址
        String logoPath="C:\\Users\\Administrator\\Desktop\\temp\\fuwu3.jpg";
        String imgLogoPath="F:\\tmp\\"+time+"12.png";
        //生成带有logo的二维码
        fixqrcode(content,imgLogoPath,logoPath);
        System.out.println("it's great,success");
    }
}

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java生成圆形二维码的步骤如下: 1. 首先,你需要导入相关的库文件。你可以使用zxing库来生成二维码。在项目的`pom.xml`文件中添加以下依赖项: ``` <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.4.1</version> </dependency> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.4.1</version> </dependency> ``` 2. 创建一个Java类,用于生成圆形二维码。你可以使用以下代码示例: ```java import com.google.zxing.BarcodeFormat; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; import javax.imageio.ImageIO; import java.awt.*; import java.awt.geom.Ellipse2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; public class CircleQRCodeGenerator { public static void main(String[] args) { int size = 300; // 二维码尺寸 String content = "Your Content"; // 二维码内容 try { // 创建QRCodeWriter对象 QRCodeWriter qrCodeWriter = new QRCodeWriter(); // 生成二维码的BitMatrix对象 BitMatrix bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, size, size); // 创建一个BufferedImage对象,并设置其维度和类型 BufferedImage bufferedImage = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB); Graphics2D graphics = (Graphics2D) bufferedImage.getGraphics(); // 将二维码绘制到BufferedImage对象上 graphics.setColor(Color.WHITE); graphics.fillRect(0, 0, size, size); graphics.setColor(Color.BLACK); for (int i = 0; i < size; i++) { for (int j = 0; #### 引用[.reference_title] - *1* [基于Java Web的智能二维码门禁管理系统(本科毕业论文+毕业设计)](https://download.csdn.net/download/qq_35831906/88227131)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值