使用qrcode绘制二维码

1、代码如下

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
  <script type='text/javascript' src='http://cdn.staticfile.org/jquery/2.1.1/jquery.min.js'></script>
  <script type="text/javascript" src="http://cdn.staticfile.org/jquery.qrcode/1.0/jquery.qrcode.min.js"></script>
</head>

<body>
  <div id="qrcode"></div>
</body>
<script>
  $('#qrcode').qrcode({ width: 200, height: 200, text: '二维码内容' });
</script>
<style>

</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在 Taro 中,可以使用 `canvas` 绘制二维码,并通过 `canvas` 转成图片,再通过 `Image` 组件展示图片。在预览时,可以使用 Taro 提供的 `previewImage` 方法,将图片预览到系统相册中。 以下是一个简单的代码示例: 1. 安装 qrcode 库 ``` npm install qrcode --save ``` 2. 创建一个组件,生成二维码 ``` import Taro, { Component } from '@tarojs/taro' import { Canvas, Image } from '@tarojs/components' import QRCode from 'qrcode' const QR_CODE_SIZE = 200 class QRCodePreview extends Component { state = { qrcode: '', } componentDidMount() { this.generateQRCode() } async generateQRCode() { const { url } = this.props const qrcode = await QRCode.toDataURL(url, { width: QR_CODE_SIZE }) this.setState({ qrcode }) } render() { const { qrcode } = this.state return ( <Canvas canvasId="qrcode" style={{ width: QR_CODE_SIZE, height: QR_CODE_SIZE }} /> <Image src={qrcode} mode="widthFix" onClick={() => Taro.previewImage({ urls: [qrcode] })} /> ) } } ``` 在 `componentDidMount` 生命周期中,调用 `QRCode.toDataURL` 方法生成二维码,并将结果存储到组件的状态中。在 `render` 方法中,使用 `Canvas` 组件渲染二维码,使用 `Image` 组件展示二维码图片,并在点击事件中调用 `Taro.previewImage` 方法预览图片。 3. 在使用该组件的页面中,传递需要生成二维码的链接 ``` import Taro, { Component } from '@tarojs/taro' import { View } from '@tarojs/components' import QRCodePreview from './QRCodePreview' class MyPage extends Component { render() { return ( <View> <QRCodePreview url="https://www.example.com" /> </View> ) } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱编程的小学究

愿你有所收获

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值