# 二维码/条码识别

[调试诊断](https://developers.weixin.qq.com/console/devtools/debug?utm_source=api_tools)

> 接口应在服务器端调用，不可在前端（小程序、网页、APP等）直接调用，具体可参考[接口调用指南](/doc/oplatform/developers/dev/guide.md)。

接口英文名：imgQrcode

识别图片中的二维码、条码、DataMatrix和PDF417

## 1. 调用方式

### HTTPS 调用

```bash
POST https://api.weixin.qq.com/cv/img/qrcode?access_token=ACCESS_TOKEN&img_url=https://example.com/img.jpg
```

### 云调用

- 调用方法：img.scanQRCode

- 出入参和 HTTPS 调用相同，调用方式可查看 [云调用](/doc/oplatform/developers/dev/cloudCall.md) 说明文档。

### 第三方调用

- 本接口支持第三方平台代商家调用。

- 该接口所属的权限集 id 为：117

- 服务商获得其中之一权限集授权后，可通过使用 [authorizer_access_token](/doc/oplatform/developers/dev/AuthorizerAccessToken.md) 代商家进行调用，具体可查看 [第三方调用](/doc/oplatform/Third-party_Platforms/2.0/Before_Develop/call_interface.md) 说明文档。

## 2. 请求参数

### 查询参数 `Query String Parameters`

```json
[
  {
    "参数名": "access_token",
    "类型": "string",
    "必填": "是",
    "示例": "ACCESS_TOKEN",
    "说明": "接口调用凭证，可使用 [access_token](/doc/subscription/api/base/api_getaccesstoken)、[authorizer_access_token](/doc/oplatform/openApi/ticket-token/api_getauthorizeraccesstoken.md)"
  },
  {
    "参数名": "img_url",
    "类型": "string",
    "必填": "否",
    "示例": "https://example.com/img.jpg",
    "说明": "图片URL地址"
  }
]
```

### 请求体 `Request Payload`

```json
[
  {
    "参数名": "img",
    "类型": "formdata",
    "必填": "否",
    "说明": "图片文件，限制小于 2 M"
  }
]
```

## 3. 返回参数

### 返回体 `Response Payload`

```json
[
  {
    "参数名": "errcode",
    "类型": "number",
    "说明": "错误码"
  },
  {
    "参数名": "errmsg",
    "类型": "string",
    "说明": "错误信息"
  },
  {
    "参数名": "code_results",
    "类型": "objarray",
    "说明": "处理结果"
  },
  {
    "参数名": "img_size",
    "类型": "object",
    "说明": "图片大小"
  }
]
```

">

### Res.code_results(Array) `Object Payload`

处理结果

```json
[
  {
    "参数名": "type_name",
    "类型": "string",
    "说明": "码的类型"
  },
  {
    "参数名": "data",
    "类型": "string",
    "说明": "码的信息"
  },
  {
    "参数名": "pos",
    "类型": "object",
    "说明": "码的坐标"
  }
]
```

### Res.img_size `Object Payload`

图片大小

```json
[
  {
    "参数名": "w",
    "类型": "number",
    "说明": "宽度"
  },
  {
    "参数名": "h",
    "类型": "number",
    "说明": "高度"
  }
]
```

__pos">

### Res.code_results(Array).pos `Object Payload`

码的坐标

```json
[
  {
    "参数名": "left_top",
    "类型": "object",
    "说明": "左上角位置"
  },
  {
    "参数名": "right_top",
    "类型": "object",
    "说明": "右上角位置"
  },
  {
    "参数名": "right_bottom",
    "类型": "object",
    "说明": "右下角位置"
  },
  {
    "参数名": "left_bottom",
    "类型": "object",
    "说明": "左下角位置"
  }
]
```

__pos__left_top">

### Res.code_results(Array).pos.left_top `Object Payload`

左上角位置

```json
[
  {
    "参数名": "x",
    "类型": "number",
    "说明": "x坐标"
  },
  {
    "参数名": "y",
    "类型": "number",
    "说明": "y坐标"
  }
]
```

__pos__right_top">

### Res.code_results(Array).pos.right_top `Object Payload`

右上角位置

```json
[
  {
    "参数名": "x",
    "类型": "number",
    "说明": "x坐标"
  },
  {
    "参数名": "y",
    "类型": "number",
    "说明": "y坐标"
  }
]
```

__pos__right_bottom">

### Res.code_results(Array).pos.right_bottom `Object Payload`

右下角位置

```json
[
  {
    "参数名": "x",
    "类型": "number",
    "说明": "x坐标"
  },
  {
    "参数名": "y",
    "类型": "number",
    "说明": "y坐标"
  }
]
```

__pos__left_bottom">

### Res.code_results(Array).pos.left_bottom `Object Payload`

左下角位置

```json
[
  {
    "参数名": "x",
    "类型": "number",
    "说明": "x坐标"
  },
  {
    "参数名": "y",
    "类型": "number",
    "说明": "y坐标"
  }
]
```

## 4. 注意事项

1. 图片支持使用img参数实时上传，也支持使用img_url参数传送图片地址，由微信后台下载图片进行识别
2. 文件需小于2MB
3. 支持条码、二维码、DataMatrix和PDF417的识别。
4. 二维码、DataMatrix会返回位置坐标，条码和PDF417暂不返回位置坐标。

## 5. 代码示例

### 5.1 文件链接请求

请求示例

``` json
curl 'https://api.weixin.qq.com/cv/img/qrcode?img_url=ENCODE_URL&access_token=ACCESS_TOCKEN'
```

返回示例

``` json
{
  "errcode": 0,
  "errmsg": "ok",
  "code_results": [
    {
      "type_name": "QR_CODE",
      "data": "https://www.qq.com",
      "pos": {
        "left_top": {
          "x": 585,
          "y": 378
        },
        "right_top": {
          "x": 828,
          "y": 378
        },
        "right_bottom": {
          "x": 828,
          "y": 618
        },
        "left_bottom": {
          "x": 585,
          "y": 618
        }
      }
    }
  ],
  "img_size": {
    "w": 1000,
    "h": 900
  }
}
```

### 5.2 文件上传请求

请求示例

``` bash
curl -F 'img=@test.jpg' 'https://api.weixin.qq.com/cv/img/qrcode?access_token=ACCESS_TOCKEN'
```

返回示例

``` json
{
  "errcode": 0,
  "errmsg": "ok",
  "code_results": [
    {
      "type_name": "QR_CODE",
      "data": "https://www.qq.com",
      "pos": {
        "left_top": {
          "x": 585,
          "y": 378
        },
        "right_top": {
          "x": 828,
          "y": 378
        },
        "right_bottom": {
          "x": 828,
          "y": 618
        },
        "left_bottom": {
          "x": 585,
          "y": 618
        }
      }
    }
  ],
  "img_size": {
    "w": 1000,
    "h": 900
  }
}
```

## 6. 错误码

以下是本接口的错误码列表，其他错误码可参考 [通用错误码](/doc/oplatform/developers/errCode/README.md)；调用接口遇到报错，可使用官方提供的 [API 诊断工具](https://developers.weixin.qq.com/console/devtools/debug?utm_source=api_errcode) 辅助定位和分析问题。

```json
[
  {
    "错误码": "-1",
    "错误描述": "system error",
    "解决方案": "系统错误"
  },
  {
    "错误码": "40001",
    "错误描述": "invalid credential  access_token isinvalid or not latest",
    "解决方案": "获取 access_token 时 AppSecret 错误，或者 access_token 无效。请开发者认真比对 AppSecret 的正确性，或查看是否正在为恰当的公众号调用接口"
  },
  {
    "错误码": "101000",
    "错误描述": "invalid image url",
    "解决方案": "图片URL错误"
  },
  {
    "错误码": "101002",
    "错误描述": "invalid image data",
    "解决方案": "图片数据无效"
  }
]
```

## 7. 适用范围

本接口在不同账号类型下的可调用情况：

```json
[
  {
    "小程序": "✔",
    "公众号": "仅认证",
    "服务号": "仅认证"
  }
]
```

- ✔：该账号可调用此接口。
- 仅认证：表示仅允许企业主体已认证账号调用，未认证或不支持认证的账号无法调用。
- 其他未明确声明的账号类型，如无特殊说明，均不可调用此接口。
