Skip to content

qrcode โ€‹

QR Code๏ผˆQuick Response Code๏ผŒไบŒ็ปด็ ๏ผ‰ๆ˜ฏไธ€็งไบŒ็ปดๆกๅฝข็ 

ไบŒ็ปดๆกๅฝข็ ็”Ÿๆˆๅ™จ

https://github.com/soldair/node-qrcode

install โ€‹

bash
npm intall qrcode

usage โ€‹

js
const QRCode = require("qrcode");

// ็”Ÿๆˆ็ปˆ็ซฏ ASCII ไบŒ็ปด็ 
QRCode.toString(
  "Hello, QR Code!",
  { type: "terminal" },
  function (err, string) {
    if (err) throw err;
    console.log(string); // ๅœจ็ปˆ็ซฏ่พ“ๅ‡บไบŒ็ปด็ ็š„ASCIIๅฝขๅผ
  }
);

// ็”Ÿๆˆ SVG ๆ ผๅผ็š„ไบŒ็ปด็ 
QRCode.toString("https://example.com", { type: "svg" }, function (err, svg) {
  if (err) throw err;
  console.log(svg); // ่พ“ๅ‡บไบŒ็ปด็ ็š„ SVG ๆ ผๅผๅญ—็ฌฆไธฒ
});
js
const QRCode = require("qrcode");

// ็”Ÿๆˆๅนถไฟๅญ˜ PNG ๆ–‡ไปถ
QRCode.toFile("./qrcode.png", "https://example.com", function (err) {
  if (err) throw err;
  console.log("QR Code saved as qrcode.png");
});

// ็”Ÿๆˆๅนถไฟๅญ˜ SVG ๆ–‡ไปถ
QRCode.toFile("./qrcode.svg", "https://example.com", function (err) {
  if (err) throw err;
  console.log("QR Code saved as qrcode.svg");
});
js
const QRCode = require("qrcode");

QRCode.toDataURL("Hello, QR Code!", function (err, url) {
  if (err) throw err;
  console.log(url); // ่พ“ๅ‡บไบŒ็ปด็ ็š„ Base64 ๆ•ฐๆฎ URL
});
js
const QRCode = require("qrcode");

QRCode.toBuffer("Hello, QR Code!", function (err, buffer) {
  if (err) throw err;
  console.log(buffer); // ่พ“ๅ‡บไบŒ็ปด็ ๅ›พๅƒ็š„ Buffer
});
js
const QRCode = require("qrcode");

QRCode.toCanvas(canvasElement, "https://example.com", function (error) {
  if (error) console.error(error);
  console.log("QR Code rendered on canvas");
});

่‡ชๅฎšไน‰ options โ€‹

ไพ‹ๅฆ‚่ฎพ็ฝฎไบŒ็ปด็ ็š„ๅฎฝๅบฆใ€่พน่ทใ€้ขœ่‰ฒ็ญ‰

js
const options = {
  width: 300,
  margin: 4,
  color: {
    dark: "#0000ff", // ่“่‰ฒๅ‰ๆ™ฏ่‰ฒ
    light: "#ffffff", // ็™ฝ่‰ฒ่ƒŒๆ™ฏ
  },
  scale: 5, // ็ผฉๆ”พๆฏ”ไพ‹
};

WARNING

ไธๆ”ฏๆŒ่‡ชๅฎšไน‰ options toString๏ผš็”Ÿๆˆ ASCII ๆˆ– SVG ๅญ—็ฌฆไธฒๅฝขๅผ็š„ไบŒ็ปด็ ๏ผŒไธๆ”ฏๆŒ่‡ชๅฎšไน‰้ขœ่‰ฒใ€ๅฎฝๅบฆ็ญ‰ใ€‚

example โ€‹

js
import { useEffect, useRef } from "react";
import QRCode from "qrcode";

const App = () => {
  const codeRef = useRef(null);

  useEffect(() => {
    if (codeRef.current) {
      QRCode.toCanvas(
        codeRef.current,
        "ms-150.github.io/blog/",
        { width: 300, height: 300 },
        function (error: unknown) {
          if (error) {
            console.error("Failed to generate QR code:", error);
          }
        }
      );
    }
  }, [codeRef]);
  return (
    <div>
      <canvas ref={codeRef}></canvas>
    </div>
  );
};

export default App;
js
<template>
  <div>
    <h1>QR Code ็คบไพ‹</h1>
    <canvas ref="qrCanvas"></canvas>
  </div>
</template>

<script>
import QRCode from 'qrcode';

export default {
  data() {
    return {
      value: 'https://example.com' // ไฝ ๆƒณ่ฆ็ผ–็ ็š„ๅ†…ๅฎน
    };
  },
  mounted() {
    QRCode.toCanvas(this.$refs.qrCanvas, this.value, (error) => {
      if (error) console.error(error);
    });
  }
};
</script>

Released under the MIT License.