首页 > 其他分享 >vue生成二维码图片并且下载图片到本地

vue生成二维码图片并且下载图片到本地

时间:2023-07-06 14:14:58浏览次数:34  
标签:vue const 二维码 let qrcode new type 图片

一、安装生成二维码插件qrcode.js

npm install --save qrcodejs2

二、封装组件

<template>
    <div>
        <div id="qrcode"></div>
    </div>
</template>
 
<script>
    // 二维码
    import QRCode from 'qrcodejs2' // 引入qrcode
    export default {
        name: 'test',
        mounted() {
            this.qrcode();
        },
        props: {
            width: {
                type: Number,
                default () {
                    return 200
                }
            },
            height: {
                type: Number,
                default () {
                    return 200
                }
            },
            // 二维码地址
            url: {
                type: String,
                default () {
                    return ''
                }
            }
        },
        methods: {
            qrcode() {
                let qrcode = new QRCode('qrcode', {
                    width: this.width,
                    height: this.height,
                    text: this.url,
                    colorDark: "#000",
                    colorLight: "#fff",
                })
            },
        }
    }
</script>
<style scoped>
    #qrcode {
        display: inline-block;
    }
 
    #qrcode img {
        width: 132px;
        height: 132px;
        background-color: #fff;
        padding: 6px;
    }
</style>

三、应用于页面

<template>
    <el-dialog :title="dialogTit" :visible.sync="dialogShow" width="360px" :wrapperClosable="false"
        :append-to-body="true" :modal-append-to-body="false">
        <el-container>
            <el-main>
                <div class="qrcodeCon">
                    <qrcode id="qrcode" :url="form.w_type_urlstring"></qrcode>
                </div>
                <div class="qrcodeUrl">{{form.w_type_urlstring}}</div>
                <div class="qrcodeDownload">
                    <el-button @click="downloadClick">下载二维码</el-button>
                </div>
            </el-main>
        </el-container>
    </el-dialog>
</template>
 
<script>
    import qrcode from '@/components/Qrcode'
 
    export default {
        data() {
            return {
                // 对话框
                dialogTit: '二维码',
                dialogShow: false,
                form: {},
                formLabelWidth: '100px',
 
                // 二维码url
                url: '',
            };
        },
        components: {
            qrcode,
        },
        mounted: function() {
            var _this = this;
            _this.$nextTick(() => {
                _this.$on('handleQrcode', function(row) {
                    _this.dialogShow = true;
                    _this.form = row;
                });
            });
        },
        methods: {
            // 下载二维码
            downloadClick() {
                // 先找到canvas节点下的二维码图片
                const myCanvas = document.getElementById('qrcode').getElementsByTagName('canvas')
                const img = document.getElementById('qrcode').getElementsByTagName('img')
                // 创建一个a节点
                const a = document.createElement('a')
                // 设置a的href属性将canvas变成png格式
                const imgURL = myCanvas[0].toDataURL('image/jpg')
                const ua = navigator.userAgent
                if (ua.indexOf('Trident') !== -1 && ua.indexOf('Windows') !== -1) { // IE内核 并且  windows系统 情况下 才执行;
                    var bstr = atob(imgURL.split(',')[1])
                    var n = bstr.length
                    var u8arr = new Uint8Array(n)
                    while (n--) {
                        u8arr[n] = bstr.charCodeAt(n)
                    }
                    const blob = new Blob([u8arr])
                    window.navigator.msSaveOrOpenBlob(blob, '下载' + '.' + 'png')
                } else if (ua.indexOf('Firefox') > -1) { // 火狐兼容下载
                    const blob = this.base64ToBlob(imgURL); //new Blob([content]);
                    const evt = document.createEvent("HTMLEvents");
                    evt.initEvent("click", true, true); //initEvent 不加后两个参数在FF下会报错  事件类型,是否冒泡,是否阻止浏览器的默认行为
                    a.download = ' '; //下载图片名称,如果填内容识别不到,下载为未知文件,所以我这里就不填为空
                    a.href = URL.createObjectURL(blob);
                    a.dispatchEvent(new MouseEvent('click', {
                        bubbles: true,
                        cancelable: true,
                        view: window
                    }));
                } else { // 谷歌兼容下载
                    img.src = myCanvas[0].toDataURL('image/jpg')
                    a.href = img.src
                    // 设置下载文件的名字
                    a.download = this.form.title + this.form.w_type_title + '二维码';
                    // 点击
                    a.click();
 
                    this.$message({
                        message: '下载成功,已保存到桌面',
                        type: 'success'
                    });
                }
            },
 
            // base64转blob
            base64ToBlob(code) {
                let parts = code.split(';base64,');
                let contentType = parts[0].split(':')[1];
                let raw = window.atob(parts[1]);
                let rawLength = raw.length;
                let uInt8Array = new Uint8Array(rawLength);
                for (let i = 0; i < rawLength; ++i) {
                    uInt8Array[i] = raw.charCodeAt(i);
                }
                return new Blob([uInt8Array], {
                    type: contentType
                });
            },
        },
    };
</script>
 
<style scoped>
    .el-container {
        max-height: 600px;
    }
 
    .qrcodeCon {
        text-align: center;
    }
 
    .qrcodeUrl {
        padding: 30px 0;
    }
 
    .qrcodeDownload {
        text-align: center;
    }
</style>

 

标签:vue,const,二维码,let,qrcode,new,type,图片
From: https://www.cnblogs.com/Jishuyang/p/17531961.html

相关文章

  • 用vue-contextmenujs进行右键菜单的操作
    1.安装依赖npminstallvue-contextmenujs2.引用在main.js中importContextmenufrom"vue-contextmenujs"Vue.use(Contextmenu);3.使用示例我是在elementui表格中使用的<template>....<el-table.....    @row-contextmenu="onContextmenu"&......
  • vue实现接受后台文件流转文件并下载
    接受到的文件流是这样的 1,首先在请求时加上  responseType:'blob'request({url:"url",method:"get",responseType:'blob',//加上这一行params:params})  2,在获取到返回来的文件流后进行处理 //传的参数为图片downloadImage({file_......
  • 使用vue-super-flow的使用进行工作流的梳理
    1.安装依赖npminstallvue-super-flow2.在页面中引用<template><super-flow></super-flow></template><script>importSuperFlowfrom'vue-super-flow'import'vue-super-flow/lib/index.css'exportdefault{......
  • 前端Vue自定义顶部导航栏navBar 导航栏搜索框searchBar 导航栏右侧菜单按钮button
    前端Vue自定义顶部导航栏navBar导航栏搜索框searchBar导航栏右侧菜单按钮button,下载完整代码请访问uni-app插件市场地址:https://ext.dcloud.net.cn/plugin?id=13342效果图如下:cc-headerSearch使用方法<!--icon:右侧菜单图标@searchClick:搜索点击 @rigIconClick:右......
  • 前端Vue自定义带历史记录的搜索框组件searchBar 支持搜索输入框清空 搜索历史存储记录
    前端Vue自定义带历史记录的搜索框组件searchBar支持搜索输入框清空搜索历史存储记录清除,下载完整代码请访问uni-app插件市场地址:https://ext.dcloud.net.cn/plugin?id=13343效果图如下:cc-hisSearchBar使用方法//不同的业务功能历史记录设置不同存储keyconstkStora......
  • 解决微信H5的图片缓存问题
    一、缓存可以解决什么问题?他的缺点是什么?1、缓存可以解决什么问题:减少不必要的网络传输,节约宽带更快的加载页面减少服务器负载,避免服务器过载的情况出现2、缓存的缺点:占内存,有些缓存会被存到内存中页面更新不及时,使用过时的图片、样式文件3、总结来说:开发阶段需要频......
  • vuex的使用
    下载安装npmivuex搭建vuex环境1.创建文件:src/store/index.js//引入Vue核心库importVuefrom'vue'//引入VueximportVuexfrom'vuex'//应用Vuex插件Vue.use(Vuex)//准备actions对象——响应组件中用户的动作constactions={}//准备mutations对象——修改state......
  • 孪生网络:图片相似度对比神器
    本文来自公众号"AI大道理"—————— SiameseNetwork(孪生网络)很早就被发明了,它的作者是著名的卷积神经网络LeNet-5的作者LeCun。最早用来从相似图片数据集上学习图片表示的网络结构就是siamese网络。两幅图通过两个共享权重的CNN得到各自的表示,而各自表示的距离决定了......
  • vue(二)vue组件
    单文件组件在其他框架都鼓励把模板、逻辑和样式的代码区分成不同文件的时候,Vue却反其道行之。使用单文件组件,Vue把模板、相关脚本和CSS一起整合放在.vue结尾的一个单文件中。这些文件最终会通过JS打包工具(例如Webpack)处理,这意味着你可以使用构建时工具。你可以使用比如......
  • .NET 个人博客-给图片添加水印
    个人博客-给图片添加水印前言......