图像处理 /scanQRCode

# img.scanQRCode

> 本接口应在服务器端调用,详细说明参见[服务端API](https://developers.weixin.qq.com/miniprogram/dev/framework/server-ability/backend-api.html)。

> 本接口支持[云调用](https://developers.weixin.qq.com/miniprogram/dev/wxcloud/guide/openapi/openapi.html)。需开发者工具版本 >= `1.02.1904090`(最新[稳定版下载](https://developers.weixin.qq.com/miniprogram/dev/devtools/stable.html)),`wx-server-sdk` >= `0.4.0`

本接口提供基于小程序的条码/二维码识别的API。

调用方式:

- [HTTPS 调用](https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/img/img.scanQRCode.html#method-http)
- [云调用](https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/img/img.scanQRCode.html#method-cloud)



## HTTPS 调用

### 请求地址

```text
POST https://api.weixin.qq.com/cv/img/qrcode?img_url=ENCODE_URL&access_token=ACCESS_TOCKEN
```

### 请求参数

| 属性                                  | 类型     | 默认值 | 必填 | 说明                                                         |
| :------------------------------------ | :------- | :----- | :--- | :----------------------------------------------------------- |
| access_token / cloudbase_access_token | string   |        | 是   | [接口调用凭证](https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html) |
| img_url                               | string   |        | 是   | 要检测的图片 url,传这个则不用传 img 参数。                  |
| img                                   | FormData |        | 是   | form-data 中媒体文件标识,有filename、filelength、content-type等信息,传这个则不用传 img_url。 |

### 返回值

### Object

返回的 JSON 数据包

| 属性    | 类型   | 说明     |
| :------ | :----- | :------- |
| errcode | string | 错误码   |
| errmsg  | string | 错误信息 |

### 使用说明

**图片说明** 文件大小限制:小于2M

**二维码说明** 支持条码、二维码、DataMatrix和PDF417的识别。 二维码、DataMatrix会返回位置坐标,条码和PDF417暂不返回位置坐标。

### 请求数据示例

示例1:

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

示例2:

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

### 返回数据示例

```text
{
    "errcode": 0,
    "errmsg": "ok",
    "code_results": [
        {
            "type_name": "QR_CODE",
            "data": "http://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
                }
            }
        },
        {
            "type_name": "QR_CODE",
            "data": "https://mp.weixin.qq.com",
            "pos": {
                "left_top": {
                    "x": 185,
                    "y": 142
                },
                "right_top": {
                    "x": 396,
                    "y": 142
                },
                "right_bottom": {
                    "x": 396,
                    "y": 353
                },
                "left_bottom": {
                    "x": 185,
                    "y": 353
                }
            }
        },
        {
            "type_name": "EAN_13",
            "data": "5906789678957"
        },
        {
            "type_name": "CODE_128",
            "data": "50090500019191"
        }
    ],
    "img_size": {
        "w": 1000,
        "h": 900
    }
}
```



## 云调用

> [云调用](https://developers.weixin.qq.com/miniprogram/dev/wxcloud/guide/openapi/openapi.html)是微信云开发提供的在云函数中调用微信开放接口的能力,需要在云函数中通过 `wx-server-sdk` 使用。

### 接口方法

```js
openapi.img.scanQRCode
```

> 需在 `config.json` 中配置 `img.scanQRCode` API 的权限,[详情](https://developers.weixin.qq.com/miniprogram/dev/wxcloud/guide/openapi/openapi.html#usage-3)

### 请求参数

| 属性   | 类型     | 默认值 | 必填 | 说明                                                         |
| :----- | :------- | :----- | :--- | :----------------------------------------------------------- |
| imgUrl | string   |        | 是   | 要检测的图片 url,传这个则不用传 img 参数。                  |
| img    | FormData |        | 是   | form-data 中媒体文件标识,有filename、filelength、content-type等信息,传这个则不用传 img_url。 |

**img 的结构**

| 属性        | 类型   | 默认值 | 必填 | 说明                     |
| :---------- | :----- | :----- | :--- | :----------------------- |
| contentType | string |        | 是   | 数据类型,传入 MIME Type |
| value       | Buffer |        | 是   | 文件 Buffer              |

### 返回值

### Object

返回的 JSON 数据包

| 属性    | 类型   | 说明     |
| :------ | :----- | :------- |
| errCode | string | 错误码   |
| errMsg  | string | 错误信息 |

### 异常

### Object

抛出的异常

| 属性    | 类型   | 说明     |
| :------ | :----- | :------- |
| errCode | string | 错误码   |
| errMsg  | string | 错误信息 |

**errCode 的合法值**

| 值   | 说明 | 最低版本 |
| :--- | :--- | :------- |
|      |      |          |

### 使用说明

**图片说明** 文件大小限制:小于2M

**二维码说明** 支持条码、二维码、DataMatrix和PDF417的识别。 二维码、DataMatrix会返回位置坐标,条码和PDF417暂不返回位置坐标。

### 返回数据示例

```json
{
    "errcode": 0,
    "errmsg": "ok",
    "code_results": [
        {
            "type_name": "QR_CODE",
            "data": "http://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
                }
            }
        },
        {
            "type_name": "QR_CODE",
            "data": "https://mp.weixin.qq.com",
            "pos": {
                "left_top": {
                    "x": 185,
                    "y": 142
                },
                "right_top": {
                    "x": 396,
                    "y": 142
                },
                "right_bottom": {
                    "x": 396,
                    "y": 353
                },
                "left_bottom": {
                    "x": 185,
                    "y": 353
                }
            }
        },
        {
            "type_name": "EAN_13",
            "data": "5906789678957"
        },
        {
            "type_name": "CODE_128",
            "data": "50090500019191"
        }
    ],
    "img_size": {
        "w": 1000,
        "h": 900
    }
}
```