laitimes

qrcode: A command-line tool for QR codes implemented in Rust

author:Not bald programmer
qrcode: A command-line tool for QR codes implemented in Rust

QRtool is a command-line tool written in Rust for encoding and decoding QR codes. This article will go into detail on how to install and use QRtool, as well as some advanced features.

Install the QRtool

QRtool can be installed in a variety of ways: from source, via a package manager, or by downloading a pre-compiled binary.

Install from source

You can use Cargo to install QRtool:

cargo install qrtool           

Install via the package manager

Depending on the operating system, it can be installed using the appropriate package manager:

  • Homebrew (works with any operating system)
brew install sorairolake/tap/qrtool           
  • Nix (for any operating system)
nix-env -iA nixpkgs.qrtool           
  • Arch Linux
pacman -S qrtool           
  • openSUSE
zypper install qrtool           

Install from binaries

Precompiled binaries for Linux, macOS, and Windows can be downloaded from the release page.

Use QRtool

The basic use of QRtool is very simple, and here are some examples of commonly used commands:

Encode a string as a QR code

qrtool encode "QR code" > output.png           

This will generate a QR code image in PNG format and save it in a output.png file.

Decode the QR code from the image

qrtool decode output.png           

The output will be the string contained in the QR code:

QR code           

Advanced usage

QRtool also supports advanced features such as generating QR codes in SVG format, customizing QR code colors and formats, and generating micro QR codes.

Generate a QR code in SVG format

Use the -t option to change the format of the generated image to support png (default), svg, or UTF-8 strings output to the terminal.

qrtool encode -o output.svg -t svg "QR code"           

Customize the foreground and background color of your QR code

You can change the foreground and background color of the generated image using the --foreground and --background options, which accept CSS color strings such as brown, #a52a2a或rgb (165 42 42). The default foreground color is black and the background color is white.

qrtool encode --foreground brown --background lightslategray "QR code" > output.png           

Generate micro QR codes

Use the --variant option to change the variation of the QR code, supporting both normal (default) and micro (micro QR codes).

qrtool encode -v 3 --variant micro "QR code" > output.png           

Supported input image formats

QRtool supports decoding QR codes from a wide range of image formats, including:

  • BMP
  • DDS
  • Swatch
  • GIF
  • Radiance RGBE
  • ICO
  • JPEG
  • OpenEXR
  • PNG
  • PNM
  • QOI
  • SVG
  • TGA
  • TIFF
  • WebP

It is important to note that if you want to support decoding QR codes from SVG images, you must enable the decode-from-svg feature at compile time. SVG images are rasterized before scanning.

qrtool decode input.webp
# 或者
qrtool decode -t webp input.webp           

Generate a shell autocomplete script

Use the --generate-completion option to generate an autocomplete script for the shell. Supported shells include:

  • bash
  • elvish
  • fish
  • nushell
  • powershell
  • zsh

Example:

qrtool --generate-completion bash > qrtool.bash           

Integration with other programs

The QRtool can be read by stdin and output via stdout, allowing it to integrate seamlessly with other programs.

Optimize the output image

The images generated by QRtool are not optimized. For example, the output PNG image is always stored in 32-bit RGBA format. If you wish to reduce the image size or optimize your image, you can use an optimizer such as Oxipng or SVGcleaner.

Optimize the output of PNG images:

qrtool encode "QR code" | oxipng - > output.png           

Optimize the output of SVG images:

qrtool encode -t svg "QR code" | svgcleaner -c - > output.svg           

Read and write unsupported image formats

If you want to save the encoded image in an image format other than PNG or SVG, or decode an unsupported image format, you can use a conversion tool such as ImageMagick.

Read Cargo.toml from stdin and save the encoded result as a JPEG XL image:

cat Cargo.toml | qrtool encode | magick png:- output.jxl           

Decode this image and print the result:

magick output.jxl png:- | qrtool decode | bat -l toml           

conclusion

QRtool is a powerful and flexible tool for a wide range of QR code encoding and decoding tasks, from simple string encoding to advanced image format processing. Hopefully, this article will help you better understand and use QR code related operations.

Read on