Java生成二维码之Graphics2D自定义码眼形状
Java 2D API 提供了几个类来定义常见的几何对象,例如点、直线、曲线和矩形。这些几何类是 java.awt.geom包的一部分。通过熟练使用Graphics2D类,可以绘制出任意类型的图形。
官网教程地址:https://docs.oracle.com/javase/tutorial/2d/geometry/index.html
先看效果,由于审核原因,此二维码的左下角的码眼内框没有生成。
通过效果图可以看出目前已经实现了11种组合码眼的绘制,都是通过Java中Graphics2D绘制实现,下面我们针对每种码眼的形状绘制形成实例代码。我们按照效果图先后顺序以此说明生成方式。
- 绘制方形二维码码眼
代码实例:
package com.faea.test;import javax.swing.*;
import java.awt.*;/* 生成方形 @author liuchao* @date 2023/4/11*/
public class CodeCreateTestOne extends JPanel {@Overridepublic 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);}}
- 绘制圆形二维码码眼
实例代码:
package com.faea.test;import javax.swing.*;
import java.awt.*;/* 生成圆形(粗边) @author liuchao* @date 2023/4/11*/
public class CodeCreateTestTwo extends JPanel {@Overridepublic 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);}}
效果:
- 绘制圆角矩形二维码码眼
代码:
package com.faea.test;import javax.swing.*;
import java.awt.*;/* 生成圆角矩形 @author liuchao* @date 2023/4/11*/
public class CodeCreateTestThree extends JPanel {@Overridepublic 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);}}
效果:
- 绘制方形圆点二维码码眼
代码:
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 {@Overridepublic 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);}}
效果:
- 绘制方形小方点二维码码眼
代码:
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 {@Overridepublic 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);}}
- 绘制单边圆角二维码码眼
代码:
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 {@Overridepublic 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);}}
- 绘制眼睛形状二维码码眼
代码:
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 {@Overridepublic 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);}}
- 绘制气泡形状二维码码眼
代码实例:
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 {@Overridepublic 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);}}
以上就是各种码眼绘制的代码实例,通过码外眼+码内眼 不同形状组合可以形成是几十种不同样式的码眼,如果朋友们还有其他的样式可以留言沟通,小编也继续学习扩展下。