Skip to content

QRCode 二维码 ​

把文本编码为可扫描的二维码,支持纠错等级、颜色、尺寸、中心图标和过期状态。

何时使用 ​

  • 展示登录链接、邀请链接、下载地址或支付信息等可扫描内容。
  • 需要在页面上明确展示加载中、已过期和已扫描状态时。
  • 只是展示短文本时用 Text 文本;需要条形码或其他编码格式时选择对应的专用组件。

在线体验 ​

示例包含基础二维码、中心图标、过期/加载/已扫描状态,以及颜色、尺寸和纠错等级。

QRCode 二维码 · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

基本用法 ​

下面代码放在应用的页面文件中;完整的 main 和窗口初始化见 打开自绘窗口。

go
import "github.com/tangoui/tango/ui"

var inviteURL = "https://example.com/invite/abc"

func page(c *ui.Context) {
    ui.QRCode(c, inviteURL).Size(160).A11yLabel("邀请链接二维码")
}

QRCode(c, value) 默认创建 160 逻辑像素、带边框的二维码,纠错级别从 M 开始。默认会在二维码版本不变时尽量提高纠错级别,可用 BoostLevel(false) 固定为指定级别。二维码内容由 value 决定;页面需要重新生成不同内容时,直接传入新的值即可。

常见用法 ​

以下片段省略了 import。

纠错等级与中心图标 ​

Level 接受 L、M、Q、H 四级纠错。二维码加入中心图标或处于可能被遮挡的环境时建议使用 H,同时不要让图标盖住过多模块。

go
var logo image.Image = image.NewRGBA(image.Rect(0, 0, 32, 32))

func logoCode(c *ui.Context) {
    ui.QRCode(c, inviteURL).Level("H").Icon(logo).IconSize(36)
}

自定义颜色、背景和尺寸 ​

Color 修改模块颜色,Bg 修改二维码背景,Bordered(false) 去掉外框。自定义颜色时要保持前景和背景有足够对比度,避免影响扫描。

go
func styledCode(c *ui.Context) {
    ui.Row(c, func() {
        ui.QRCode(c, inviteURL).Size(120).Color(c.Theme.Success.Base)
        ui.QRCode(c, inviteURL).Size(120).
            Color(theme.Hex("#1677ff")).Bg(theme.Hex("#f5f5f5")).Bordered(false)
    }, layout.Gap(16))
}

过期、加载与已扫描状态 ​

Status 支持 active(默认)、expired、loading 和 scanned。过期状态显示刷新动作,Refreshed() 在用户点击刷新时返回 true,此时由应用获取新的内容并重新渲染。

go
var qrStatus = "active"

func statusCode(c *ui.Context) {
    code := ui.QRCode(c, inviteURL).Status(qrStatus)
    if code.Refreshed() {
        inviteURL = "https://example.com/invite/new"
        qrStatus = "active"
    }
}

读取编码矩阵 ​

Matrix() 返回编码后的 *QRMatrix,编码失败时为 nil。通常只需要把组件画出来;需要做诊断或自定义导出时才读取矩阵。

go
func inspectCode(c *ui.Context) {
    code := ui.QRCode(c, inviteURL)
    if code.Matrix() == nil {
        ui.Text(c, "二维码内容无法编码").Color(c.Theme.Error.Base)
    }
}

主题与样式 ​

Ant Design 使用白色二维码底板和边框;Element Plus 沿用同一组件并使用 Element 圆角令牌;shadcn 保持高对比度底板。深色主题默认仍使用黑色模块和白色底板,以保证常见扫码器的识别率。

ConfigProvider.Defaults("QRCode", ...) 支持 "size"(像素)、"errorLevel"(L / M / Q / H)和 "bordered";实例上的 Size、Level、Bordered 优先。

注意事项 ​

  • 内容安全:二维码只编码传入的字符串;不要把密码、访问令牌等敏感信息直接放入可被拍摄的二维码。
  • 可扫描性:优先保持深色模块、浅色背景和足够留白;使用中心图标时使用 H 级纠错并控制图标尺寸。
  • 状态放在哪:过期时间、当前 URL、刷新状态和请求状态由应用保存;不要在渲染函数中生成随机值,否则二维码会不断变化。
  • 无障碍:组件默认以图像语义使用编码值作为名称;对 URL 或复杂内容应使用 A11yLabel 提供用户可理解的名称。状态会暴露忙碌、过期和已扫描描述。
  • RTL:二维码本身不镜像,周围布局和状态文字遵循 RTL;不要对二维码图像做水平翻转。

示例代码 ​

以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。

QRCode 二维码

gallery 演示 QRCode 的 Render 函数,源码 page_data_qrcode.go#L12。

加载中…

QRCode 纠错提升、留白与无边框

gallery 演示 QRCodeAlign 的 Render 函数,源码 page_b2h.go#L171。

加载中…

API ​

签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。

入口函数 ​

ui.QRCode ​

go
func QRCode(c *Context, value string) *QRCodeW

QRCode adds a QR code for value (default 160px, level M, bordered). An empty value draws nothing (Ant renders null), keeping the box size.

返回 *QRCodeW,可链式调用的方法见 QRCodeW 方法。

QRCodeW 方法 ​

ui.QRCodeW:QRCodeW renders a QR code (Ant Design QRCode / Element has none; the Element flavor keeps the same component with Element tokens).

方法说明签名
A11yLabelA11yLabel sets the accessible name (default: the encoded value).A11yLabel(s string) *QRCodeW
BgBg sets the background color.Bg(col theme.Color) *QRCodeW
BoostLevelBoostLevel(false) keeps the requested error-correction level; by default (Ant boostLevel true) the level is raised as far as the code keeps the same version (size).BoostLevel(on bool) *QRCodeW
BorderedBordered toggles the outer border.Bordered(on bool) *QRCodeW
ColorColor sets the module color.Color(col theme.Color) *QRCodeW
IconIcon draws img in the center (use level "H" so the code stays readable).Icon(img image.Image) *QRCodeW
IconSizeIconSize sets the center icon size (default 40).IconSize(px float32) *QRCodeW
LevelLevel sets the error-correction level: "L", "M", "Q" or "H".Level(l string) *QRCodeW
MarginSizeMarginSize adds a quiet zone of n modules around the code (Ant marginSize, default 0).MarginSize(n int) *QRCodeW
MatrixMatrix returns the encoded matrix (nil if encoding failed).Matrix() *QRMatrix
RefreshedRefreshed reports a click on the "点击刷新" action of an expired code.Refreshed() bool
SizeSize sets the outer size in px.Size(px float32) *QRCodeW
StatusStatus sets "active" (default), "expired", "loading" or "scanned".Status(s string) *QRCodeW

相关链接 ​

以 MIT 许可发布