vue二维码生成且带文字图片下载

本文介绍了如何在Vue项目中集成qrcode-vue和html2canvas库,实现自定义二维码生成,并结合按钮实现点击下载二维码图片的功能。步骤包括安装依赖库、在Vue组件中引入并配置,以及编写下载方法。
摘要由CSDN通过智能技术生成

(一)web页面效果:

                                          

 (二)执行结果:

 

(三)vue代码实例:

1)安装qrcode-vue库:npm install --save qrcode-vue 

2)安装html2canvas库:npm install --save html2canvas

3)vue代码

<template>
<div>
  <div class="div-box">
     <div ref="content" style="width:fit-content;height:fit-content;text-align: center;">
        <qrcode-vue
            :size="qrcodeVue.size"
            :value="qrcodeVue.value"
            :bgColor="qrcodeVue.bgColor"
            :fgColor="qrcodeVue.fgColor">
        </qrcode-vue>
         <p>描述文字...</p>
     </div>
  </div> 
  <el-button type="primary" icon="el-icon-document" size="small" @click="downloadCode">下载二维码文字图片</el-button>
</div>      
</template>

<script>

  import qrcodeVue from 'qrcode-vue'
  import html2canvas from 'html2canvas'

  export default {
    name: 'Test',
    components: { qrcodeVue },
    data () {
      return {
         qrcodeVue: {
          size: 150,
          bgColor: '#67C23A', //背景色
          fgColor: '#FFFFFF', //前景色
          value: 'qrCode value', //二维码值
          logo: require('@/assets/logo.png') //logo图片
        }
      }
    },
    created () {
      
    },
    mounted () {
       
    },
    methods: {
     downloadCode(){
				let content = this.$refs.content
				let scrollHeight = content.scrollHeight
				let scrollWidth = content.scrollWidth
				html2canvas(content,{
						scale: window.devicePixelRatio*2,
						useCORS: true , //开启跨域配置,但和allowTaint不能共存
						width:scrollWidth,
						height:scrollHeight,
						windowWidth:scrollWidth,
						windowHeight:scrollHeight,
						x:0,
						y:window.pageYOffset
					}).then((canvas) => {
						this.operType = 'edit'
						let dataURL = canvas.toDataURL("image/jpg");
						let link = document.createElement("a");    
						link.href = dataURL;
						let filename = `二维码文件名.jpg`;  //文件名称
						link.setAttribute("download", filename);
						link.style.display = "none"; //a标签隐藏
						document.body.appendChild(link);
						link.click();
					})
			}
    }
  }
</script>

<style>
.div-box {
    width:fit-content;
    height:fit-content;
    padding: 2px;
    border: 1px dashed #f69c55;
    position: relative;
    text-align: center;
    margin-bottom: 5px;
}
</style>

好的,这里是一个 Vue 组件,可以生成文字和图片的二维码: ```html <template> <div> <canvas ref="qrcodeCanvas"></canvas> </div> </template> <script> import QRCode from "qrcode"; export default { name: "QrcodeWithImageAndText", props: { value: { type: String, required: true }, size: { type: Number, default: 100 }, text: { type: String, default: "" }, textColor: { type: String, default: "#000" }, textSize: { type: Number, default: 16 }, image: { type: String, default: "" }, imageWidth: { type: Number, default: 20 }, imageHeight: { type: Number, default: 20 } }, mounted() { this.generate(); }, methods: { async generate() { try { const canvas = this.$refs.qrcodeCanvas; const url = await QRCode.toDataURL(this.value, { width: this.size }); const img = new Image(); img.src = url; img.onload = () => { canvas.width = img.width; canvas.height = img.height + this.textSize + this.imageHeight; const ctx = canvas.getContext("2d"); ctx.drawImage(img, 0, 0); ctx.font = `${this.textSize}px sans-serif`; ctx.fillStyle = this.textColor; ctx.textAlign = "center"; ctx.fillText( this.text, img.width / 2, img.height + this.textSize + this.imageHeight ); const image = new Image(); image.src = this.image; image.onload = () => { ctx.drawImage( image, img.width / 2 - this.imageWidth / 2, img.height + this.textSize, this.imageWidth, this.imageHeight ); }; }; } catch (err) { console.error(err); } } } }; </script> ``` 这个组件和上一个组件也很相似,但是多了几个属性: - `image`:要添加到二维码下面的图片 URL。 - `imageWidth`:图片的宽度。 - `imageHeight`:图片的高度。 当组件挂载到页面上时,会自动调用 `generate` 方法生成有文本和图片的二维码,并将其绘制到一个 `canvas` 元素上。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值