How to generate QR codes in Python, PHP, Java, Node and .NET
Every major language has a free, open-source library that generates QR codes locally: qrcode or segno in Python, chillerlan/php-qrcode or endroid/qr-code in PHP, qrcode in Node, ZXing in Java and QRCoder in .NET. Generate in your own code rather than calling a free online QR API, which receives every value you encode.
Why not call a free QR code API
Many old tutorials build an image URL such as https://chart.googleapis.com/chart?cht=qr&chs=150x150&chl=... and drop it into an img tag. That is Google's Image Charts API, which Google deprecated back in 2012. A deprecated service can stop answering at any time, and every code on your site breaks with it.
Other free QR APIs work the same way: your server or your visitor's browser sends the content to someone else's server to be drawn. For a public link that may not matter. For ticket IDs, customer records, Wi-Fi passwords or one-time login links, it is a leak you do not need. The libraries below run in your own process, cost nothing, and work offline.
Python: qrcode and segno
qrcode is the most familiar Python library. Install it with Pillow support: pip install "qrcode[pil]"
import qrcode
img = qrcode.make('https://example.com')
img.save('qr.png')
For more control, create qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_H, box_size=10, border=4), then call qr.add_data(...), qr.make(fit=True) and qr.make_image(fill_color='black', back_color='white'). It also has SVG image factories in qrcode.image.svg.
segno has no dependencies and writes SVG, PNG, EPS and PDF directly, which makes it the easy answer to generating a QR code as a PDF in Python. Install it with pip install segno.
import segno
qr = segno.make_qr('https://example.com', error='h')
qr.save('qr.svg', scale=10) or qr.save('qr.pdf', scale=10)
Use make_qr rather than make if you always want a standard QR code: make may choose a Micro QR code for very short content, and some phone cameras do not read those.
PHP: chillerlan/php-qrcode and endroid/qr-code
chillerlan/php-qrcode is installed with composer require chillerlan/php-qrcode. The shortest use renders a data URI you can put straight into an img tag:
use chillerlan\QRCode\QRCode;
echo '<img src="' . (new QRCode)->render('https://example.com') . '" alt="QR code">';
Options let you choose SVG or raster output, the error correction level and colours, and its examples include a QR code with a logo in the centre.
endroid/qr-code (composer require endroid/qr-code) uses a builder: you set the data, size, margin, error correction, an optional logo path and a writer such as PNG or SVG, then save the result or return it as a data URI. Its builder API has changed between major versions, so copy the example from the README for the version Composer installs.
For line barcodes in PHP, such as a Code 128 barcode generator, picqer/php-barcode-generator outputs SVG, PNG and HTML.
Node, Java and .NET
Node: npm i qrcode, then const QRCode = require('qrcode'); and await QRCode.toFile('qr.png', 'https://example.com', { errorCorrectionLevel: 'H', width: 600 }); For SVG, const svg = await QRCode.toString('https://example.com', { type: 'svg' }); and toDataURL gives a data URI for web pages.
Java: ZXing's core and javase modules (com.google.zxing) are the usual Java QR code generator. new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, 400, 400) returns a BitMatrix, and MatrixToImageWriter.writeToPath(matrix, "PNG", path) saves it. ZXing's other writers, such as Code128Writer, draw line barcodes the same way.
.NET: dotnet add package QRCoder, then:
using var generator = new QRCodeGenerator();
var data = generator.CreateQrCode("https://example.com", QRCodeGenerator.ECCLevel.Q);
byte[] png = new PngByteQRCode(data).GetGraphic(20);
QRCoder also has an SvgQRCode renderer. ZXing.Net is the .NET port of ZXing if you also need to read barcodes.
Error correction, logos and SVG
- Error correction has four levels: L (about 7% of the code can be damaged), M (15%), Q (25%) and H (30%). Higher levels make a denser code.
- Use M for plain codes on screens and clean prints. Use Q or H for codes that get scuffed, and H when you place a logo in the middle.
- Keep a logo small, around a fifth of the code's width, and always scan the result.
- Keep the quiet zone: a clear border of at least four modules. Most libraries add it by default.
- Prefer SVG for print and for web pages. It stays sharp at any size and is usually smaller than PNG.
- Dark code on a light background. Inverted and low-contrast colours fail on many phone cameras.
Reading QR codes and barcodes in code
Python: OpenCV's QRCodeDetector has a detectAndDecode method for QR codes, and the zxing-cpp package (import zxingcpp) reads QR codes and most barcodes with read_barcodes. pyzbar is another option, which needs the zbar library installed.
Java: ZXing's MultiFormatReader decodes a BufferedImage once it is wrapped in a BufferedImageLuminanceSource and a HybridBinarizer.
PHP cannot open a camera, because it runs on the server. A PHP QR code scanner with a camera on mobile is really a web page that scans in the browser with JavaScript, then posts the decoded text to your PHP script. Our guide to scanning QR codes in JavaScript shows the browser side with BarcodeDetector and zxing-wasm, which also reads uploaded images before any upload is needed.
When you do not need code at all
If you need one code, or a batch from a spreadsheet, a browser tool is quicker than writing a script. NeoReader's QR code generator runs in the browser, test-scans each design as you edit, and downloads PNG, SVG or PDF with no watermark. The bulk QR code generator turns a CSV into a ZIP of named PNG files. Both make static codes, so the content is stored in the code itself and never expires.