Java生成二维码之Graphics2D自定义码眼形状

有时候,不是因为你没有能力,也不是因为你缺少勇气,只是因为你付出的努力还太少,所以,成功便不会走向你。而你所需要做的,就是坚定你的梦想,你的目标,你的未来,然后以不达目的誓不罢休的那股劲,去付出你的努力,成功就会慢慢向你靠近。

导读:本篇文章讲解 Java生成二维码之Graphics2D自定义码眼形状,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

Java 2D API 提供了几个类来定义常见的几何对象,例如点、直线、曲线和矩形。这些几何类是 java.awt.geom包的一部分。通过熟练使用Graphics2D类,可以绘制出任意类型的图形。

官网教程地址:https://docs.oracle.com/javase/tutorial/2d/geometry/index.html

先看效果,由于审核原因,此二维码的左下角的码眼内框没有生成。

Java生成二维码之Graphics2D自定义码眼形状

 通过效果图可以看出目前已经实现了11种组合码眼的绘制,都是通过Java中Graphics2D绘制实现,下面我们针对每种码眼的形状绘制形成实例代码。我们按照效果图先后顺序以此说明生成方式。

  • 绘制方形二维码码眼

代码实例:

package com.faea.test;

import javax.swing.*;
import java.awt.*;

/**
 * 生成方形
 *
 * @author liuchao
 * @date 2023/4/11
 */
public class CodeCreateTestOne extends JPanel {
    @Override
    public void paintComponent(Graphics g) {
        Graphics2D graphics2D = (Graphics2D) g;
        graphics2D.setColor(Color.RED);
        //尺寸
        int multiple = 30;
        //X坐标
        int x = 50;
        //Y坐标
        int y = 50;
        BasicStroke stroke = new BasicStroke(multiple);
        graphics2D.setStroke(stroke);
        int offset = multiple / 2;
        graphics2D.drawRect(x + offset,
                y + offset, multiple, multiple);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Rounded Rectangle");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new CodeCreateTestOne());
        frame.setSize(400, 400);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

}

Java生成二维码之Graphics2D自定义码眼形状  

  • 绘制圆形二维码码眼

实例代码:

package com.faea.test;

import javax.swing.*;
import java.awt.*;

/**
 * 生成圆形(粗边)
 *
 * @author liuchao
 * @date 2023/4/11
 */
public class CodeCreateTestTwo extends JPanel {
    @Override
    public void paintComponent(Graphics g) {
        Graphics2D graphics2D = (Graphics2D) g;
        graphics2D.setColor(Color.RED);
        //尺寸
        int multiple = 30;
        //X坐标
        int x = 50;
        //Y坐标
        int y = 50;
        BasicStroke stroke = new BasicStroke(multiple);
        graphics2D.setStroke(stroke);

        int offset = multiple / 2;
        int size = multiple / 4;
        graphics2D.drawOval(x + offset, y + offset, size, size);


    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("circle_thick_edge");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new CodeCreateTestTwo());
        frame.setSize(200, 200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

}

效果:

Java生成二维码之Graphics2D自定义码眼形状

  • 绘制圆角矩形二维码码眼

代码:

package com.faea.test;

import javax.swing.*;
import java.awt.*;

/**
 * 生成圆角矩形
 *
 * @author liuchao
 * @date 2023/4/11
 */
public class CodeCreateTestThree extends JPanel {
    @Override
    public void paintComponent(Graphics g) {
        Graphics2D graphics2D = (Graphics2D) g;
        graphics2D.setColor(Color.RED);
        //尺寸
        int multiple = 30;
        //X坐标
        int x = 50;
        //Y坐标
        int y = 50;

        BasicStroke stroke = new BasicStroke(multiple / 4);
        graphics2D.setStroke(stroke);


        graphics2D.drawRoundRect(x, y, multiple,
                multiple, multiple / 2, multiple / 2);


    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("rounded_thick_edge");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new CodeCreateTestThree());
        frame.setSize(200, 200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

}

 效果:

Java生成二维码之Graphics2D自定义码眼形状

  • 绘制方形圆点二维码码眼

代码:

package com.faea.test;

import com.alibaba.fastjson.JSON;
import com.faea.qrcode.model.CodeEyeOutDrawModel;

import javax.swing.*;
import java.awt.*;

/**
 * 生成圆点
 *
 * @author liuchao
 * @date 2023/4/11
 */
public class CodeCreateTestFour extends JPanel {
    @Override
    public void paintComponent(Graphics g) {
        Graphics2D graphics2D = (Graphics2D) g;
        graphics2D.setColor(Color.RED);
        String json = "{'eyePosition':'LEFT_TOP','holdPointNum':7,'multiple':8,'pointList':[{'x':0,'y':0},{'x':1,'y':0},{'x':2,'y':0},{'x':3,'y':0},{'x':4,'y':0},{'x':5,'y':0},{'x':6,'y':0},{'x':0,'y':1},{'x':6,'y':1},{'x':0,'y':2},{'x':6,'y':2},{'x':0,'y':3},{'x':6,'y':3},{'x':0,'y':4},{'x':6,'y':4},{'x':0,'y':5},{'x':6,'y':5},{'x':0,'y':6},{'x':1,'y':6},{'x':2,'y':6},{'x':3,'y':6},{'x':4,'y':6},{'x':5,'y':6},{'x':6,'y':6}]}";
        CodeEyeOutDrawModel model = JSON.parseObject(json, CodeEyeOutDrawModel.class);
        int multiple = model.getMultiple();
        for (Point point : model.getPointList()) {
            int x = 20 + (int) point.getX() * model.getMultiple();
            int y = 20 + (int) point.getY() * model.getMultiple();
            graphics2D.fillOval(x, y, multiple, multiple);
        }

    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("round_dot");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new CodeCreateTestFour());
        frame.setSize(200, 200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

}

效果:

Java生成二维码之Graphics2D自定义码眼形状

  •  绘制方形小方点二维码码眼

代码:

package com.faea.test;

import com.alibaba.fastjson.JSON;
import com.faea.qrcode.model.CodeEyeOutDrawModel;

import javax.swing.*;
import java.awt.*;

/**
 * 生成方点
 *
 * @author liuchao
 * @date 2023/4/11
 */
public class CodeCreateTestFive extends JPanel {
    @Override
    public void paintComponent(Graphics g) {
        Graphics2D graphics2D = (Graphics2D) g;
        graphics2D.setColor(Color.RED);
        String json = "{'eyePosition':'LEFT_TOP','holdPointNum':7,'multiple':8,'pointList':[{'x':0,'y':0},{'x':1,'y':0},{'x':2,'y':0},{'x':3,'y':0},{'x':4,'y':0},{'x':5,'y':0},{'x':6,'y':0},{'x':0,'y':1},{'x':6,'y':1},{'x':0,'y':2},{'x':6,'y':2},{'x':0,'y':3},{'x':6,'y':3},{'x':0,'y':4},{'x':6,'y':4},{'x':0,'y':5},{'x':6,'y':5},{'x':0,'y':6},{'x':1,'y':6},{'x':2,'y':6},{'x':3,'y':6},{'x':4,'y':6},{'x':5,'y':6},{'x':6,'y':6}]}";
        CodeEyeOutDrawModel model = JSON.parseObject(json, CodeEyeOutDrawModel.class);
        int multiple = model.getMultiple();
        int offsetX = multiple / 8, offsetY = multiple / 8;
        int width = multiple - offsetX * 2, height = multiple - offsetY * 2;
        for (Point point : model.getPointList()) {
            int x = 20 + (int) point.getX() * model.getMultiple();
            int y = 20 + (int) point.getY() * model.getMultiple();
            graphics2D.fillRect(x + offsetX, y + offsetY, width, height);
        }

    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("square_dot");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new CodeCreateTestFive());
        frame.setSize(200, 200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

}

Java生成二维码之Graphics2D自定义码眼形状

  • 绘制单边圆角二维码码眼

代码:

package com.faea.test;

import com.alibaba.fastjson.JSON;
import com.faea.qrcode.model.CodeEyeOutDrawModel;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Path2D;

/**
 * 生成方点
 *
 * @author liuchao
 * @date 2023/4/11
 */
public class CodeCreateTestSix extends JPanel {
    @Override
    public void paintComponent(Graphics g) {
        Graphics2D graphics2D = (Graphics2D) g;
        graphics2D.setColor(Color.RED);

        String json = "{'eyePosition':'LEFT_TOP','holdPointNum':7,'multiple':8,'pointList':[{'x':0,'y':0},{'x':1,'y':0},{'x':2,'y':0},{'x':3,'y':0},{'x':4,'y':0},{'x':5,'y':0},{'x':6,'y':0},{'x':0,'y':1},{'x':6,'y':1},{'x':0,'y':2},{'x':6,'y':2},{'x':0,'y':3},{'x':6,'y':3},{'x':0,'y':4},{'x':6,'y':4},{'x':0,'y':5},{'x':6,'y':5},{'x':0,'y':6},{'x':1,'y':6},{'x':2,'y':6},{'x':3,'y':6},{'x':4,'y':6},{'x':5,'y':6},{'x':6,'y':6}]}";
        CodeEyeOutDrawModel model = JSON.parseObject(json, CodeEyeOutDrawModel.class);
        BasicStroke stroke = new BasicStroke(model.getMultiple());
        graphics2D.setStroke(stroke);

        int x = 20 + (int) model.getPointList().get(0).getX() * model.getMultiple();
        int y = 20 + (int) model.getPointList().get(0).getY() * model.getMultiple();
        int width = model.getHoldPointNum() * model.getMultiple() - model.getMultiple() / 2;
        int offset = model.getMultiple() / 4;
        x += offset;
        y += offset;
        int height = width;
        int arcWidth = width / 2;
        int arcHeight = arcWidth;
        Path2D path = new Path2D.Double();
        path.moveTo(x, y + arcHeight);
        path.quadTo(x, y, x + arcWidth, y);
        path.lineTo(x + width, y);
        path.lineTo(x + width, y + height);
        path.lineTo(x, y + height);
        path.closePath();
        graphics2D.draw(path);

    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("single_sided_fillet");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new CodeCreateTestSix());
        frame.setSize(200, 200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

}

 Java生成二维码之Graphics2D自定义码眼形状

  • 绘制眼睛形状二维码码眼

代码:

package com.faea.test;

import com.alibaba.fastjson.JSON;
import com.faea.qrcode.model.CodeEyeOutDrawModel;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Path2D;

/**
 * 生成眼睛形状
 *
 * @author liuchao
 * @date 2023/4/11
 */
public class CodeCreateTestSeven extends JPanel {
    @Override
    public void paintComponent(Graphics g) {
        Graphics2D graphics2D = (Graphics2D) g;
        graphics2D.setColor(Color.RED);

        String json = "{'eyePosition':'LEFT_TOP','holdPointNum':7,'multiple':8,'pointList':[{'x':0,'y':0},{'x':1,'y':0},{'x':2,'y':0},{'x':3,'y':0},{'x':4,'y':0},{'x':5,'y':0},{'x':6,'y':0},{'x':0,'y':1},{'x':6,'y':1},{'x':0,'y':2},{'x':6,'y':2},{'x':0,'y':3},{'x':6,'y':3},{'x':0,'y':4},{'x':6,'y':4},{'x':0,'y':5},{'x':6,'y':5},{'x':0,'y':6},{'x':1,'y':6},{'x':2,'y':6},{'x':3,'y':6},{'x':4,'y':6},{'x':5,'y':6},{'x':6,'y':6}]}";
        CodeEyeOutDrawModel model = JSON.parseObject(json, CodeEyeOutDrawModel.class);
        BasicStroke stroke = new BasicStroke(model.getMultiple());
        graphics2D.setStroke(stroke);

        int x = 20 + (int) model.getPointList().get(0).getX() * model.getMultiple();
        int y = 20 + (int) model.getPointList().get(0).getY() * model.getMultiple();
        int width = model.getHoldPointNum() * model.getMultiple() - model.getMultiple() / 2;
        int offset = model.getMultiple() / 4;
        x += offset;
        y += offset;
        int height = width;
        int arcWidth = width / 2;
        int arcHeight = arcWidth;

        Path2D path = new Path2D.Double();
        path.moveTo(x, y + height - arcHeight);
        path.lineTo(x, y);
        path.lineTo(x + width - arcWidth, y);
        path.quadTo(x + width, y, x + width, y + arcHeight);
        path.lineTo(x + width, y + height);
        path.lineTo(x + arcWidth, y + height);
        path.quadTo(x, y + height, x, y + height - arcHeight);
        path.closePath();
        graphics2D.draw(path);

    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("eye_shape");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new CodeCreateTestSeven());
        frame.setSize(200, 200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

}

 Java生成二维码之Graphics2D自定义码眼形状

  • 绘制气泡形状二维码码眼

代码实例:

package com.faea.test;

import com.alibaba.fastjson.JSON;
import com.faea.qrcode.constants.CodeEyePositionEnum;
import com.faea.qrcode.model.CodeEyeOutDrawModel;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Path2D;

/**
 * 生成气泡形状
 *
 * @author liuchao
 * @date 2023/4/11
 */
public class CodeCreateTestNine extends JPanel {
    @Override
    public void paintComponent(Graphics g) {
        Graphics2D graphics2D = (Graphics2D) g;
        graphics2D.setColor(Color.RED);

        String json = "{'eyePosition':'LEFT_TOP','holdPointNum':7,'multiple':8,'pointList':[{'x':0,'y':0},{'x':1,'y':0},{'x':2,'y':0},{'x':3,'y':0},{'x':4,'y':0},{'x':5,'y':0},{'x':6,'y':0},{'x':0,'y':1},{'x':6,'y':1},{'x':0,'y':2},{'x':6,'y':2},{'x':0,'y':3},{'x':6,'y':3},{'x':0,'y':4},{'x':6,'y':4},{'x':0,'y':5},{'x':6,'y':5},{'x':0,'y':6},{'x':1,'y':6},{'x':2,'y':6},{'x':3,'y':6},{'x':4,'y':6},{'x':5,'y':6},{'x':6,'y':6}]}";
        CodeEyeOutDrawModel model = JSON.parseObject(json, CodeEyeOutDrawModel.class);
        BasicStroke stroke = new BasicStroke(model.getMultiple());
        graphics2D.setStroke(stroke);

        int x = 20 + (int) model.getPointList().get(0).getX() * model.getMultiple();
        int y = 20 + (int) model.getPointList().get(0).getY() * model.getMultiple();
        int width = model.getHoldPointNum() * model.getMultiple() - model.getMultiple() / 2;
        int offset = model.getMultiple() / 4;
        x += offset;
        y += offset;
        int height = width;
        int arcWidth = width / 2;
        int arcHeight = arcWidth;

        Path2D path = new Path2D.Double();
        path.moveTo(x, y + arcHeight);
        //左上角
        if (CodeEyePositionEnum.LEFT_TOP.equals(model.getEyePosition())) {
            path.quadTo(x, y, x + arcWidth, y);
            path.lineTo(x + width, y);
            path.lineTo(x + width, y + height - arcHeight);
            path.quadTo(x + width, y + height, x + width - arcWidth, y + height);
            path.lineTo(x + arcWidth, y + height);
            path.quadTo(x, y + height, x, y + height - arcHeight);
            //右上角
        } else if (CodeEyePositionEnum.RIGHT_TOP.equals(model.getEyePosition())) {
            path.lineTo(x, y);
            path.lineTo(x + width - arcWidth, y);
            path.quadTo(x + width, y, x + width, y + arcHeight);
            path.lineTo(x + width, y + height - arcHeight);
            path.quadTo(x + width, y + height, x + width - arcWidth, y + height);
            path.lineTo(x + arcWidth, y + height);
            path.quadTo(x, y + height, x, y + height - arcHeight);

            //左下角
        } else {
            path.quadTo(x, y, x + arcWidth, y);
            path.lineTo(x + width - arcWidth, y);
            path.quadTo(x + width, y, x + width, y + arcHeight);
            path.lineTo(x + width, y + height);
            path.lineTo(x + arcWidth, y + height);
            path.quadTo(x, y + height, x, y + height - arcHeight);

        }
        path.closePath();
        graphics2D.draw(path);

    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("air_bubble");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new CodeCreateTestNine());
        frame.setSize(200, 200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

}

Java生成二维码之Graphics2D自定义码眼形状

 以上就是各种码眼绘制的代码实例,通过码外眼+码内眼 不同形状组合可以形成是几十种不同样式的码眼,如果朋友们还有其他的样式可以留言沟通,小编也继续学习扩展下。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/144586.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!