What you need to create a barcode
Creating a barcode means using a library or tool that converts data (usually numbers or text) into a visual pattern of bars and spaces that a scanner can read. You do not need specialized hardware — a barcode library handles the conversion, and you output the result as an image file or display it on screen.
The most common approach is to pick a barcode format (Code128, QR code, UPC, or EAN are standard choices), install a library for your programming language, pass your data to that library, and save or display the image it returns. The whole process takes a few lines of code.
Your choice of library depends on what language you are writing in and what barcode format you need. Python, JavaScript, Java, C#, and PHP all have well-maintained options. If you are building a web application, you may want a library that generates barcodes on the server side and sends them to the browser as images.
Key Takeaways
- A barcode library converts your data into an image that scanners can read, and most languages have at least one solid option available.
- Code128 works for most text and numbers, while QR codes hold more data and work on smartphones, and UPC/EAN are standard for retail products.
- The basic workflow is: install the library, create a barcode object with your data, set any options you need, and save or display the image.
- Testing your barcode with an actual scanner before deploying it to production catches format and data errors early.
Choosing a barcode format for your use case
Code128 is the most flexible choice for most applications. It encodes numbers, uppercase letters, lowercase letters, and common symbols in a compact format. A typical Code128 barcode is about 2 inches wide and readable by any standard barcode scanner. Use Code128 if you are tracking inventory, labeling packages, or encoding order numbers.
QR codes hold much more data — up to 4,296 characters — and can be scanned by any smartphone camera without a special app. They are larger on the page but work well when you need to encode a URL, contact information, or a long product identifier. QR codes are now standard in retail and logistics.
UPC and EAN are the formats you see on retail product packaging. UPC-A is 12 digits and used in North America; EAN-13 is 13 digits and used internationally. You only use these if you are creating barcodes for products that will be sold in stores or scanned by retail systems. They require specific data formats and check digits.
Start with Code128 unless you have a specific reason to use something else. It is simple, widely supported, and works for almost any internal tracking system.
Creating a barcode in Python
Python's python-barcode library is the most straightforward option. Install it with pip install python-barcode in your terminal or command prompt.
Here is the basic workflow:
- Import the barcode module: import barcode
- Create a barcode object with your format and data: barcode_instance = barcode.get_barcode_class('code128')('123456789')
- Save it as an image file: barcode_instance.save('my_barcode') — this creates a PNG file named my_barcode.png
If you want to customize the output — change the image format, add text below the barcode, or adjust the size — the library accepts those options when you save. For example, barcode_instance.save('my_barcode', options={'module_height': 15}) makes the bars taller.
For a web application, you can generate the barcode in memory and return it directly to the browser without saving a file to disk. This is useful when you are creating barcodes on demand for thousands of products.
Creating a barcode in JavaScript
The jsbarcode library works in the browser and on Node.js. For browser use, include it via a CDN: <script src="https://cdn.jsdelivr.net/npm/jsbarcode@3.11.5/dist/JsBarcode.all.min.js"></script>
Then create an SVG or canvas element in your HTML and pass it to JsBarcode:
- Add an SVG element: <svg id="barcode"></svg>
- Call JsBarcode in your script: JsBarcode("#barcode", "123456789", {format: "code128"})
The barcode renders instantly in the browser. You can print it, save it as an image using the browser's print-to-PDF feature, or let users download it directly. This approach is ideal for e-commerce sites, shipping labels, or any system where customers need to generate their own barcodes.
If you are using Node.js on the server side, install jsbarcode with npm and use it the same way you would in Python — generate the image and send it to the client.
Creating a barcode in other languages
Java developers can use the barbecue library or ZXing (Zebra Crossing), which is also used for QR code generation. ZXing is more actively maintained and supports more formats.
C# developers have BarcodeLib, which is straightforward and works in both .NET Framework and .NET Core. Install it via NuGet and use it similarly to the Python examples above.
PHP developers can use TCPDF (which includes barcode generation) or the standalone barcode library. Both are installed via Composer and integrate easily into web applications.
The pattern is the same across all languages: install the library, instantiate a barcode object with your data and format, and output the result as an image. Documentation for each library shows language-specific syntax, but the concept does not change.
Testing and troubleshooting your barcode
Before you deploy a barcode to production, test it with an actual scanner. Barcode scanners are inexpensive (under $50 for a basic USB model) and catch problems that the human eye misses. Scan the barcode and verify that the scanner reads the exact data you encoded — no extra characters, no missing digits.
Common issues include incorrect data format (for example, UPC requires exactly 12 digits), wrong check digit calculation (most libraries handle this automatically, but some formats require you to calculate it), and image resolution too low for the scanner to read. If a barcode does not scan, regenerate it and check your library's documentation for the specific format you chose.
If you are generating barcodes dynamically in a web application, test with different data lengths and special characters to make sure the library handles edge cases. A barcode that works for "12345" might fail for "ABC-123-XYZ" depending on the format.
Print a test barcode at the size you plan to use in production. Barcodes that are too small or printed at low resolution will not scan reliably. Most libraries let you adjust the module width (the thickness of the bars) to control the final size.
Displaying barcodes in a web application
If you are building a system where users see barcodes on screen or in printed documents, generate them on the server and embed them as images in your HTML. For Python Flask applications, generate the barcode, save it temporarily, and serve it as a static file. For Node.js Express, generate it in memory and send it directly with the correct content type.
For high-volume systems, cache generated barcodes so you do not regenerate the same barcode every time a user views a page. Store the image file or the barcode data in a database, and only regenerate when the underlying data changes.
If you are printing barcodes, use a print stylesheet to hide unnecessary page elements and set the barcode to a fixed size. Test the print output in your browser before sending it to a physical printer — screen resolution and print resolution are different, and a barcode that looks fine on screen might be unreadable when printed.
Frequently Asked Questions
Can I create a barcode without installing a library?
You can use an online barcode generator to create one-off barcodes, but for any system that generates barcodes programmatically, you need a library. Writing the barcode encoding logic yourself is possible but error-prone — the libraries handle check digits, format validation, and image generation correctly.
What is the difference between a barcode and a QR code?
A barcode (like Code128) is a one-dimensional pattern of vertical bars that encodes data in a line. A QR code is two-dimensional and holds much more data in a square grid. QR codes can be scanned by smartphones, while traditional barcodes require a dedicated scanner. Choose based on how much data you need to encode and how your users will scan it.
Do I need to pay for a barcode library?
Most popular barcode libraries are open source and free. python-barcode, jsbarcode, and ZXing are all free and maintained by the community. Some commercial libraries exist, but you rarely need them unless you have very specific requirements.
How do I make sure my barcode is readable by all scanners?
Use a standard format like Code128 or QR code, test with an actual scanner before deployment, and print or display the barcode at a reasonable size. Most scanners support the common formats, so as long as your barcode is generated correctly and large enough to read, it will work across different devices.
Can I encode special characters or Unicode in a barcode?
It depends on the format. Code128 supports numbers, uppercase and lowercase letters, and common symbols. If you need to encode Unicode characters or languages other than English, QR codes are your best option — they support UTF-8 and can encode any text a smartphone camera can read.