qrcode开发指导
==========

生成并显示二维码，具体用法请参考[qrcode](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-components-basic-qrcode)。

#### 创建qrcode组件

在pages/index目录下的hml文件中创建一个qrcode组件。

<!-- xxx.hml-->
<div class="container">
  <qrcode value="Hello"></qrcode>
</div>

/\* xxx.css \*/
.container {
  width: 100%;
  height: 100%;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background-color: #F1F3F5;
}

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/04/v3/PuSHxPPJRNOVO6JloDk8gQ/zh-cn_image_0000002594828254.png?HW-CC-KV=V1&HW-CC-Date=20260612T073321Z&HW-CC-Expire=86400&HW-CC-Sign=1B9D572D043159F057E209047A199EBC351A08F36244A4847ABA31E7124EA334)

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/99/v3/Flnh4xIkTDaulDeOrxW0sA/note_3.0-zh-cn.png?HW-CC-KV=V1&HW-CC-Date=20260612T073321Z&HW-CC-Expire=86400&HW-CC-Sign=DC78002B86CDB21D3D70C641FF251CB19F66A117C5526685863C8A1B1DD4EA3A)

qrcode组件在创建的时候value的值为必填项。

#### 设置组件类型

通过设置qrcode的type属性来选择二维码类型，如定义qrcode为矩形二维码、圆形二维码。

<!-- xxx.hml-->
<div class="container">
  <select onchange="settype">
    <option for="{{bcol\_list}}" value="{{$item}}">{{$item}}</option>
  </select>
  <qrcode value="Hello" type="{{qr\_type}}"></qrcode>
</div>

/\* xxx.css \*/
.container {
  width: 100%;
  height: 100%;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background-color: #F1F3F5;
}
select{
  margin-top: 50px;
  margin-bottom: 50px;
}

// index.js
export default {
  data: {
    qr\_type: 'rect',
    bcol\_list: \['rect','circle'\]
  },
  settype(e) {
    this.qr\_type = e.newValue
  },
}

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/ec/v3/Cd6nbtm9TgONa1ecSK91LQ/zh-cn_image_0000002625187701.gif?HW-CC-KV=V1&HW-CC-Date=20260612T073321Z&HW-CC-Expire=86400&HW-CC-Sign=C592C0639B3AF35F6507C462660B513B8B6D5B5D65D6FF690E027143CC06E52F)

#### 设置样式

通过color和background-color样式为二维码设置显示颜色和背景颜色。

<!-- xxx.hml-->
<div class="container">
  <qrcode value="Hello" type="rect"></qrcode>
</div>

/\* xxx.css \*/
.container {
  width: 100%;
  height: 100%;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background-color: #F1F3F5;
}
qrcode{
  width: 300px;
  height: 300px;
 color: blue;  background-color: #ffffff;
}

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/8c/v3/20tPFyeBSOeQ0UzLKwJ3iw/zh-cn_image_0000002625107845.png?HW-CC-KV=V1&HW-CC-Date=20260612T073321Z&HW-CC-Expire=86400&HW-CC-Sign=90529158FBD406BDDEC22D061D4DFC5BCA5CA3F6A8EE9E9F5E5799A368327769)

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/c8/v3/DOeq4_kpT7aPKmqL5CWN4A/note_3.0-zh-cn.png?HW-CC-KV=V1&HW-CC-Date=20260612T073321Z&HW-CC-Expire=86400&HW-CC-Sign=464A98766FCAAD64ABB4A1B0F26482ABA4703BF4208B2110B3FE450C4F6357F2)

*   width和height不一致时，取二者较小值作为二维码的边长，且最终生成的二维码居中显示。
    
*   width和height只设置一个时，取设置的值作为二维码的边长。都不设置时，使用200px作为默认边长。
    

#### 场景示例

在本场景中将二维码与输入框绑定，通过改变输入框的内容改变二维码。

<!-- xxx.hml-->
<div class="container">
  <input style="margin-bottom: 100px;" onchange="change"></input>
  <qrcode value="{{textVal}}"></qrcode>
</div>

/\* xxx.css \*/
.container {
  width: 100%;
  height: 100%;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background-color: #F1F3F5;
}
qrcode{
  width: 400px;
  height: 400px;
}

// index.js
export default{
  data: {
    textVal: ''
  },
  change(e){
    this.textVal = e.value
  }
}

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/0d/v3/QdBNzOAHT4qhHDgVhB9o7w/zh-cn_image_0000002594668328.gif?HW-CC-KV=V1&HW-CC-Date=20260612T073321Z&HW-CC-Expire=86400&HW-CC-Sign=5FED908D2972ECAD2D5503B5B8D55C6614CD5CEABA4B08739B6C4D91D3E59AD1)