0
# QRCode Terminal
1
2
QRCode Terminal generates and displays QR codes directly in the terminal using ASCII/ANSI characters. It provides both a Node.js library API and a command-line interface for creating QR codes from text input, supporting different error correction levels and output formats (normal and compact).
3
4
## Package Information
5
6
- **Package Name**: qrcode-terminal
7
- **Package Type**: npm
8
- **Language**: JavaScript
9
- **Installation**: `npm install qrcode-terminal`
10
11
## Core Imports
12
13
```javascript
14
const qrcode = require('qrcode-terminal');
15
```
16
17
## Basic Usage
18
19
```javascript
20
const qrcode = require('qrcode-terminal');
21
22
// Generate QR code and print to console
23
qrcode.generate('https://example.com');
24
25
// Generate QR code with callback
26
qrcode.generate('Hello World', function(qrcode) {
27
console.log(qrcode);
28
});
29
30
// Generate compact QR code
31
qrcode.generate('Hello World', { small: true });
32
33
// Set error correction level
34
qrcode.setErrorLevel('H');
35
qrcode.generate('Hello World');
36
```
37
38
## CLI Usage
39
40
Install globally for command-line usage:
41
42
```bash
43
npm install -g qrcode-terminal
44
```
45
46
Generate QR codes from the command line:
47
48
```bash
49
# Direct argument
50
qrcode-terminal "Hello World"
51
52
# Piped input
53
echo "Hello World" | qrcode-terminal
54
55
# Help and version
56
qrcode-terminal --help
57
qrcode-terminal --version
58
```
59
60
## Capabilities
61
62
### QR Code Generation
63
64
Generate QR codes for terminal display with customizable options and output formats.
65
66
```javascript { .api }
67
/**
68
* Generate QR code and display in terminal or pass to callback
69
* @param input - Text to encode in QR code
70
* @param opts - Optional configuration object
71
* @param cb - Optional callback function to receive QR code string
72
*/
73
function generate(input, opts, cb);
74
75
/**
76
* Configuration options for QR code generation
77
*/
78
interface GenerateOptions {
79
/** Generate compact QR code using Unicode block characters */
80
small?: boolean;
81
}
82
```
83
84
The `generate` function supports multiple calling patterns:
85
- `generate(input)` - Print to console
86
- `generate(input, callback)` - Pass result to callback
87
- `generate(input, options)` - Print with options
88
- `generate(input, options, callback)` - Pass result with options to callback
89
90
### Error Correction Level Configuration
91
92
Set the error correction level for QR code generation. Higher levels provide better error recovery but result in denser QR codes.
93
94
```javascript { .api }
95
/**
96
* Set error correction level for subsequent QR code generation
97
* @param error - Error correction level ('L', 'M', 'Q', or 'H')
98
*/
99
function setErrorLevel(error);
100
```
101
102
### Error Correction Levels
103
104
Built-in error correction level constants available from the library.
105
106
```javascript { .api }
107
/**
108
* Default error correction level (Low - ~7% recovery)
109
*/
110
error: 1;
111
```
112
113
**Available Error Correction Levels:**
114
- `'L'` (Low): ~7% error recovery capability (default)
115
- `'M'` (Medium): ~15% error recovery capability
116
- `'Q'` (Quartile): ~25% error recovery capability
117
- `'H'` (High): ~30% error recovery capability
118
119
## CLI Interface
120
121
Command-line interface for generating QR codes from terminal input.
122
123
```bash { .api }
124
# Generate QR code from argument
125
qrcode-terminal <message>
126
127
# Generate QR code from piped input
128
echo <message> | qrcode-terminal
129
130
# Display help information
131
qrcode-terminal --help
132
qrcode-terminal -h
133
134
# Display version number
135
qrcode-terminal --version
136
qrcode-terminal -v
137
```
138
139
## Usage Examples
140
141
### Basic QR Code Generation
142
143
```javascript
144
const qrcode = require('qrcode-terminal');
145
146
// Simple QR code generation
147
qrcode.generate('Visit https://github.com');
148
```
149
150
### QR Code with Callback
151
152
```javascript
153
const qrcode = require('qrcode-terminal');
154
155
qrcode.generate('Hello World', function(qrString) {
156
// Process the QR code string
157
console.log('Generated QR code:');
158
console.log(qrString);
159
160
// Save to file, send over network, etc.
161
});
162
```
163
164
### Compact QR Code Format
165
166
```javascript
167
const qrcode = require('qrcode-terminal');
168
169
// Generate smaller QR code using Unicode block characters
170
qrcode.generate('https://example.com', { small: true }, function(qrString) {
171
console.log('Compact QR code:');
172
console.log(qrString);
173
});
174
```
175
176
### Error Correction Level Configuration
177
178
```javascript
179
const qrcode = require('qrcode-terminal');
180
181
// Set high error correction for better recovery
182
qrcode.setErrorLevel('H');
183
qrcode.generate('Important data that needs high reliability');
184
185
// Set low error correction for simpler QR codes
186
qrcode.setErrorLevel('L');
187
qrcode.generate('Simple message');
188
```
189
190
### Command Line Usage
191
192
```bash
193
# Generate QR code for a URL
194
qrcode-terminal "https://github.com/gtanner/qrcode-terminal"
195
196
# Generate QR code from command output
197
echo "Hello from the terminal" | qrcode-terminal
198
199
# Generate QR code for file contents
200
cat message.txt | qrcode-terminal
201
```