js 生成二维码并点击下载软件_js生成二维码并保存

js 生成二维码并点击下载软件_js生成二维码并保存html部分<h1>文字生成二维码及生成可下载的图片</h1><div><inputid=”input”value=”http://www.baidu.com”type=”text”><buttontype=”button”onclick=”refresh()”>生出来</button></div><divid=”output”><!–这里放canvas格式的二维

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

html 部分

<h1>
    文字生成二维码及生成可下载的图片
</h1>
<div><input id="input" value="http://www.baidu.com" type="text"><button type="button" onclick="refresh()">生出来</button></div>
<div id="output">
    <!--这里放canvas格式的二维码-->
</div>
<p>上面的码是使用canvas标签, 下面的是img标签</p>
<div id="img-qr">
    <!--放img格式的二维码-->
</div>
<p id="download">
    <!--放下载链接-->
</p>

js

我们先引入两个js文件

<script src="http://code.jquery.com/jquery-3.6.0.js"></script>
<script src="https://cdn.bootcss.com/jquery.qrcode/1.0/jquery.qrcode.min.js"></script>
 window.onload = function () { 
   
        refresh();
    };

    function refresh() { 
   
        let text = document.getElementById("input").value;
        creater(text);
    }

    /** * 生成二维码及下载链接的函数 * @param text 传入你要生成二维码的文字 */
    function creater(text = '') { 
   
        //http://www.baidu.com可以自定义任意你要的链接或者文字
        jQuery('#output').empty().qrcode(text);
        let drawing = document.getElementsByTagName('canvas')[0];
        //确定浏览器支持<canvas>元素
        if (drawing.getContext) { 
   
            let context = drawing.getContext('2d');
            console.log(context);
            //取得图像的数据 URI
            let imgURI = drawing.toDataURL("image/png");

            //显示img格式图片开始(不要此步可以跳过)
            let image = document.createElement("img");
            image.src = imgURI;
            $("#img-qr").empty().append(image);
            //显示img格式图片结束(不要此步可以跳过)

            //显示下载链接开始
            let a = document.createElement("a");
            a.href = imgURI;
            //设置下载链接的文字提示
            a.innerText = 'download qrcode';
            //设置下载的文件名
            a.download = 'test.png';
            document.body.appendChild(a);
            $("#download").empty().append(a);
            //显示下载链接结束
        }
    }

界面效果
在这里插入图片描述

如果想生成中文,并且出现了乱码,可以使用下面方法将input文字进行转换

function toUtf8(str) { 
   
            var out, i, len, c;
            out = "";
            len = str.length;
            for (i = 0; i < len; i++) { 
   
                c = str.charCodeAt(i);
                if ((c >= 0x0001) && (c <= 0x007F)) { 
   
                    out += str.charAt(i);
                } else if (c > 0x07FF) { 
   
                    out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
                    out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
                    out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
                } else { 
   
                    out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
                    out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
                }
            }
            return out;
        }
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/180755.html原文链接:https://javaforall.net

(0)
上一篇 2022年10月18日 上午8:16
下一篇 2022年10月18日 上午8:16


相关推荐

  • reading recovery_the common reader

    reading recovery_the common readerBookKeeperAutoRecovery背景版本:BookKeeper版本为4.12.0(Pulsar2.7.0的内置版本)Recovery主要针对的场景是:当集群中有部分bookie节点因异常原因宕机,此时我们恢复该节点上存储的数据。BookKeeper提供了两种数据恢复方式,一种是手动恢复(Manualrecovery),一种是自动恢复(AutoRecovery)。ManualRecovery如果集群中未开启AutoRecovery的功能,则用户可用手动恢复。手动恢复

    2025年8月4日
    6
  • LLM系列:ChatGPT提示词精选与实操指南

    LLM系列:ChatGPT提示词精选与实操指南

    2026年3月16日
    2
  • [HDU 2096] 小明A+B

    [HDU 2096] 小明A+B[HDU2096]小明A+B 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=20961#include&lt;iostream&gt;2#include&lt;cstdio&gt;3#include&lt;cmath&gt;4#include&lt;algori…

    2022年6月16日
    28
  • GridBagLayout 以及 GridBagConstraints 用法「建议收藏」

    GridBagLayout 以及 GridBagConstraints 用法「建议收藏」GridBagLayout是一个灵活的布局管理器,部件如果想加入其中需借助GridBagConstraints,其中有若干个参数,解释如下:gridx/gridy:组件的横纵坐标gridwidth:组件所占列数,也是组件的宽度gridheight:组件所占行数,也是组件的高度fill:当组件在其格内而不能撑满其格时,通过fill的值来设定填充方式,有四个值

    2025年10月16日
    6
  • redis集群客户端JedisCluster优化 – 管道(pipeline)模式支持

    redis集群客户端JedisCluster优化 – 管道(pipeline)模式支持redis集群客户端JedisCluster优化-管道(pipeline)模式支持Redis在3.0版正式引入了集群这个特性,扩展变得非常简单。然而当你开心的升级到3.0后,却发现有些很好用的功能现在工作不了了,比如我们今天要聊的pipeline功能。我们知道,普通的情况下,redisclient与server之间采用的是请求应答的模式,即:Client:command1

    2022年8月31日
    4
  • vue 获取当前路由的子路由

    vue 获取当前路由的子路由获取当前路由的子路由 routes varroutes children this router options routes varroute this route matched for vari 0 i route length 1 i routes routes children find e e name route length 1 i routes routes children find e

    2026年3月16日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号