控制台指南

最佳实践

开发者指南

数据湖存储

API 文档

SDK 文档

文档捉虫大赛火热进行中,好礼多多> HOT

简介

本文档提供关于图片二维码相关的 API 概览以及 SDK 示例代码。

API 说明
二维码识别 二维码识别功能可识别图片中有效二维码的位置及内容,输出图像中二维码包含的文本信息(每个二维码对应的 URL 或文本),并可对识别出的二维码添加马赛克
二维码生成 二维码生成功能可根据用户指定的文本信息(URL 或文本),生成对应的二维码或条形码

二维码识别

功能说明

数据万象二维码识别功能可识别图片中有效二维码的位置及内容,输出图像中二维码包含的文本信息(每个二维码对应的 URL 或文本),并可对识别出的二维码添加马赛克。

方法原型

func (s *CIService) GetQRcode(ctx context.Context, name string, cover int, opt *ObjectGetOptions, id ...string) (*GetQRcodeResult, *Response, error)

请求示例1:上传时识别

opt := &cos.ObjectPutOptions{
    nil,
    &cos.ObjectPutHeaderOptions{
        XOptionHeader: &http.Header{},
    },
}
pic := &cos.PicOperations{
    IsPicInfo: 1,
    Rules: []cos.PicOperationsRules{
        {
            FileId: "format.jpg",
            Rule:   "QRcode/cover/1",
        },
    },
}
opt.XOptionHeader.Add("Pic-Operations", cos.EncodePicOperations(pic))
name := "test.jpg"
filepath := "./QRcode.jpg"
res, _, err := c.CI.PutFromFile(context.Background(), name, filepath, opt)

请求示例2:下载时识别

name := "test.jpg"
res, _, err := c.CI.GetQRcode(context.Background(), name, 1, nil)

参数说明

参数名称 参数描述 类型 必填
cover 二维码覆盖功能,将对识别出的二维码覆盖上马赛克。取值为0或1。0表示不开启二维码覆盖,1表示开启二维码覆盖,默认值0 int

结果说明

下载时识别的结果返回如下

type GetQRcodeResult struct {
    CodeStatus  int        
    QRcodeInfo  *QRcodeInfo
    ResultImage string
}
type QRcodeInfo struct {
    CodeUrl      string    
    CodeLocation *CodeLocation
}
type CodeLocation struct {
    Point []string
}

参数名称 参数描述 类型
CodeStatus 二维码识别结果。0表示未识别到二维码,1表示识别到二维码 int
QRcodeInfo 二维码识别结果,可能有多个 Struct
ResultImage 处理后的图片 base64数据,请求参数 cover 为1时返回 string
CodeUrl 二维码的内容。可能识别不到内容 string
CodeLocation 图中识别到的二维码位置坐标 Struct
Point 二维码坐标点(X坐标,Y坐标) Array

二维码生成

功能说明

数据万象二维码生成功能可根据用户指定的文本信息(URL 或文本),生成对应的二维码或条形码。

方法原型

func (s *CIService) GenerateQRcode(ctx context.Context, opt *GenerateQRcodeOptions) (*GenerateQRcodeResult, *Response, error)

func (s *CIService) GenerateQRcodeToFile(ctx context.Context, filePath string, opt *GenerateQRcodeOptions) (*GenerateQRcodeResult, *Response, error)

请求示例

opt := &cos.GenerateQRcodeOptions{
    QRcodeContent: "<https://www.example.com>"
    Mode:          0,
    Width:         200,
}
// Case1 生成二维码
res, _, err := c.CI.GenerateQRcode(context.Background(), opt)

// Case2 生成二维码,并保存成文件
filepath := "example.jpg"
res, _, err := c.CI.GenerateQRcodeToFile(context.Background(), filepath, opt)

参数说明

type GenerateQRcodeOptions struct {
    QRcodeContent string
    Mode          int 
    Width         int
}

参数名称 参数描述 类型
QRcodeContent 可识别的二维码文本信息 string
Mode 生成的二维码类型,可选值:0或1。0为二维码,1为条形码,默认值为0 int
Width 指定生成的二维码或条形码的宽度,高度会进行等比压缩 int

结果说明

type GenerateQRcodeResult struct {
    ResultImage string
}

参数名称 参数描述 类型
ResultImage 二维码图片 base64 数据 string
目录