npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tc-qrcode

v1.1.0

Published

简单好用的生成和解析二维码的js库

Downloads

150

Readme

tc-qrcode

英文 | 在线使用 | 更新日志 | 反馈 | Gitee


1. 特性

  1. 调用单个api生成和解析二维码
  2. 使用Promise api, 支持async/await
  3. 使用 typescript 开发
  4. 解析二维码支持解析文件,base64,url,image
  5. 支持video和canvas的截屏
  6. 解析二维码支持绑定一个type为file的input元素
  7. 生成二维码支持返回 base64 and image

注:该库编解码功能分别封装自 aralejs/qrcodecozmo/jsQR

2. 快速使用

2.1 npm 安装

npm i tc-qrcode
import qrcode from 'tc-qrcode';

qrcode.decodeFromUrl('https://cdn.jsdelivr.net/gh/theajack/qrcode/helper/demo-qrcode.png')
    .then(result=>{
        console.log(result);
    })

2.2 cdn

<script src="https://cdn.jsdelivr.net/npm/tc-qrcode/tc-qrcode.min.js"></script>
<script>
    TCQrcode.decodeFromUrl('https://cdn.jsdelivr.net/gh/theajack/qrcode/helper/demo-qrcode.png')
        .then(function (result) {
            console.log(result);
        })
</script>

3 api

请参考 index.d.ts

注:

编码的api都支持类型为 IEncodeOption 输入参数,如果传入的是字符串,则以下参数都传入默认值. 返回值也都是经过Promise 包裹的

interface IEncodeOption {
    text: string;
    width?: number; // 默认值 256
    height?: number; // 默认值 256
    typeNumber?: number; // 默认值 4
    colorDark?: string; // 默认值 '#000000'
    colorLight?: string; // 默认值 '#ffffff'
    correctLevel?: 1 | 0 | 3 | 2; // 默认值 2
}

3.1 decodeFromUrl

从url中解析二维码,可以是一个在线的图片地址或者blob url

function decodeFromUrl(url: string): Promise<string>;
import {decodeFromUrl} from 'tc-qrcode';
decodeFromUrl('xxx').then(result=>{});

3.2 decodeFromFile

从file对象中解析二维码

function decodeFromFile(file: File): Promise<string>;
import {decodeFromFile} from 'tc-qrcode';
decodeFromFile(file).then(result=>{});

3.3 decodeFromBase64

从base64的图中解析二维码

function decodeFromBase64(base64Str: string): Promise<string>;
import {decodeFromBase64} from 'tc-qrcode';
decodeFromBase64(base64).then(result=>{});

3.4 decodeFromImage

从image对象中解析二维码

function decodeFromImage(image: HTMLImageElement): Promise<string>;
import {decodeFromImage} from 'tc-qrcode';
decodeFromImage(image).then(result=>{});

3.5 decodeFromVideo

从video对象中截图并解析二维码

function decodeFromVideo(video: HTMLVideoElement): Promise<string>;
import {decodeFromVideo} from 'tc-qrcode';
decodeFromVideo(video).then(result=>{});

3.6 decodeFromCanvas

从canvas对象中截图并解析二维码

function decodeFromCanvas(canvas: HTMLCanvasElement): Promise<string>;
import {decodeFromCanvas} from 'tc-qrcode';
decodeFromCanvas(canvas).then(result=>{});

3.7 decodeBindInput

绑定一个type为file的input元素作为输入源,持续的解析二维码

这个方法不会返回 string 对象,而是使用一个回调函数来接收返回值

function decodeBindInput(input: HTMLInputElement, onResult: (result: string) => void): void;
import {decodeBindInput} from 'tc-qrcode';
decodeBindInput(input, (result)=>{

});

3.8 encodeAsBase64

将内容编码为base64的图片

function encodeAsBase64(content: string | IEncodeOption): string;
import {encodeAsBase64} from 'tc-qrcode';
const base64 = encodeAsBase64('xxxx');

// 或使用参数
const base64 = encodeAsBase64({
    text: 'xxx',
    width: 256, // 默认值 256
    height: 256, // 默认值 256
    typeNumber: 4; // 默认值 4
    colorDark: '#000000'; // 默认值 '#000000'
    colorLight: '#ffffff'; // 默认值 '#ffffff'
    correctLevel: 2; // 默认值 2
});

3.9 encodeAsImage

将内容编码为base64之后生成一个image元素

function encodeAsImage(content: string | IEncodeOption): HTMLImageElement;
import {encodeAsImage} from 'tc-qrcode';
const image = encodeAsImage('xxxx'); // 参数与3.8一致

3.10 version

获取版本号

import qrcode from 'tc-qrcode';

qrcode.version;

3.11 Encoder

暴露出编码使用库 aralejs/qrcode

import qrcode from 'tc-qrcode';

qrcode.Encoder;

3.12 Dncoder

暴露出解码使用库 cozmo/jsQR

import qrcode from 'tc-qrcode';

qrcode.Decoder;