当前位置:   article > 正文

React 生成二维码_react生成二维码图片

react生成二维码图片

使用qrcode.react

安装依赖库

  1. npm install qrcode.react
  2. //
  3. yarn add qrcode.react

代码实现

  1. import React, { Component } from 'react';
  2. import { Modal} from 'antd';
  3. const QRCode = require('qrcode.react');
  4. class QRCodeModal extends Component {
  5. constructor(props) {
  6. super(props)
  7. this.state = {
  8. qrCodeStr: '20220119QRCODE'
  9. }
  10. }
  11. downloadQrCode = () => {
  12. const canvasImg = document.getElementById('qrCode');
  13. const img = new Image();
  14. img.src = canvasImg.toDataURL('image/png');
  15. const downLink = document.getElementById('down_link');
  16. downLink.href = img.src;
  17. downLink.download = this.state.qrCodeStr;
  18. }
  19. render() {
  20. return (
  21. <Modal
  22. title={i18n.t('charger.qrcode')}
  23. width={500}
  24. visible={this.props.qrCodeVisible}
  25. onOk={this.handleCancel}
  26. onCancel={this.handleCancel}
  27. footer={[]}>
  28. <div className='bm bm-f1 bm-pc bm-ac bm-ver' style={{marginTop: 30}}>
  29. <QRCode value={this.state.qrCodeStr} size={256} id="qrCode" />
  30. <a id="down_qacode"
  31. onClick={this.downloadQrCode}
  32. style={{
  33. fontSize: 16,
  34. marginTop: 20,
  35. width: 256,
  36. textAlign: 'center',
  37. paddingTop: 10,
  38. paddingBottom: 10,
  39. border: '1px solid'}}>
  40. 下载二维码
  41. </a>
  42. </div>
  43. </Modal>
  44. )}
  45. }
  46. export default QRCodeModal

效果如下:

点击下载:

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/215460
推荐阅读
相关标签
  

闽ICP备14008679号