在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:metafloor/bwip-js开源软件地址:https://github.com/metafloor/bwip-js开源编程语言:JavaScript 60.0%开源软件介绍:// Barcode Writer in Pure JavaScriptbwip-js is a translation to native JavaScript of the amazing code provided in Barcode Writer in Pure PostScript. The translated code can run on any modern browser or JavaScript-based server framework. The software has encoding modules for over 100 different barcode types and standards. All linear and two-dimensional barcodes in common use (and many uncommon ones) are available. An exhaustive list of supported barcode types can be found at the end of this document. Barcode images are generated as png (node-js) or to a canvas (browser). Status
Supported PlatformsLinks
InstallationYou can download the latest npm module using:
Or the latest code from github:
(The bwip-js master branch and the npm version are kept sync'd.) Online Barcode GeneratorAn online barcode generator demonstrates all of the features of bwip-js. The app is also available in the root bwip-js directory. See Demo Apps. Online Barcode APIA bwip-js barcode service is available online, ready to serve up barcode images on demand. You can generate barcodes from anywhere on the web. Embed the URLs in your HTML documents or retrieve the barcode images directly from your non-JavaScript server. (JavaScript-based servers should use the bwip-js code directly - it will be a lot more performant.) For details on how to use this service, see Online Barcode API. Working With bwip-js MethodsMost of the public methods of the bwip-js export use an options object. Only two values are required:
All remaining options are optional, though you may find some quite useful. The options values can be divided into two parts, bwip-js specific options and BWIPP options. The bwip-js options are:
For the BWIPP specific options, you will need to consult the BWIPP documentation to determine what options are available for each barcode type. Note that bwip-js normalizes the BWIPP
Barcodes have the concept of module width (and height if a two-dimensional barcode). For linear barcodes, the module width is the width of the narrowest bar, and all other bar widths are a multiple of it. For 2D symbols, module width and height are the dimensions of the square or rectangle that defines the symbol's layout grid. For a barcode to be "in spec", the individual module dimensions must be consistent throughout the
symbol. With high resolution printing, you can add/subtract a dot to adjust the size of individual
modules so the overall image meets the requested width or height, while still keeping the module
size within spec. This is the intention behind BWIPP's bwip-js is designed for web usage, with a target display resolution of 72dpi. (All of BWIPP's internals are calculated in points and bwip-js just maps 1pt to 1px.) At that low resolution, it is not possible to add or subtract pixels without causing the symbol to go out of spec. Imagine a fairly common module width of 2px. If you add or subtract a pixel, you have changed the size by 50%. Typical barcode specs require module sizes to be within 5-10 pecent of nominal. For this reason, bwip-js uses a constant module size so the resulting image is as
large as possible, without exceeding the requested With bwip-js, the When you specify Browser UsageTo use within a browser, add the following to the head of your page:
While developing your project, you may want to use If you are using The scripts adds a single try {
// The return value is the canvas element
let canvas = bwipjs.toCanvas('mycanvas', {
bcid: 'code128', // Barcode type
text: '0123456789', // Text to encode
scale: 3, // 3x scaling factor
height: 10, // Bar height, in millimeters
includetext: true, // Show human-readable text
textxalign: 'center', // Always good to set this
});
} catch (e) {
// `e` may be a string or Error object
} The
On return from If you would prefer to display the barcode using an let canvas = document.createElement('canvas');
try {
bwipjs.toCanvas(canvas, options);
document.getElementById('my-img').src = canvas.toDataURL('image/png');
} catch (e) {
// `e` may be a string or Error object
} Browser ES6 Module UsageThe ESM provides the same API as the standard browser module using: import bwipjs from 'bwip-js';
// ... identical bwipjs.toCanvas() interface as above ... The ESM also facilitates bundler tree shaking by providing the individual encoders as named exports.
Each exported encoder functions identically to The exported names are the same as the import { gs1_128 } from 'bwip-js';
try {
gs1_128('my-canvas', options);
} catch (e) {
// `e` may be a string or Error object
} React UsageThe following is a minimal example of bwip-js in a React app.
It is based on the default import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import bwipjs from 'bwip-js';
class App extends Component {
componentDidMount() {
try {
// The return value is the canvas element
let canvas = bwipjs.toCanvas('mycanvas', {
bcid: 'code128', // Barcode type
text: '0123456789', // Text to encode
scale: 3, // 3x scaling factor
height: 10, // Bar height, in millimeters
includetext: true, // Show human-readable text
textxalign: 'center', // Always good to set this
});
} catch (e) {
// `e` may be a string or Error object
}
}
render() {
return (
<div className="App">
<div className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h2>Welcome to React</h2>
</div>
<canvas id="mycanvas"></canvas>
</div>
);
}
}
export default App; See the Browser Usage section for details on the See the ES6 Browser Module Usage section for details on importing encoders directly. Node.js Request HandlerThe online barcode API is implemented as a Node.js application. See the Online Barcode API for details on how the URL query parameters must be structured. A working, minimal example of how to use the request handler can be found in
// Simple HTTP server that renders barcode images using bwip-js.
const http = require('http');
const bwipjs = require('bwip-js');
// This shows how to load the Inconsolata font, supplied with the bwip-js distribution.
// The path to your fonts will be different.
//bwipjs.loadFont('Inconsolata', 100,
// require('fs').readFileSync('./fonts/Inconsolata.otf', 'binary'));
http.createServer(function(req, res) {
// If the url does not begin /?bcid= then 404. Otherwise, we end up
// returning 400 on requests like favicon.ico.
if (req.url.indexOf('/?bcid=') != 0) {
res.writeHead(404, { 'Content-Type':'text/plain' });
res.end('BWIPJS: Unknown request format.', 'utf8');
} else {
bwipjs.request(req, res); // Executes asynchronously
}
}).listen(3030); If you run the above code on your local machine, you can test with the following URL:
The bwip-js request handler only operates on the URL query parameters and ignores all path information. Your application is free to structure the URL path as needed to implement the desired HTTP request routing. Node.js Image GeneratorYou can use bwip-js to generate PNG images directly. const bwipjs = require('bwip-js');
bwipjs.toBuffer({
bcid: 'code128', // Barcode type
text: '0123456789', // Text to encode
scale: 3, // 3x scaling factor
height: 10, // Bar height, in millimeters
includetext: true, // Show human-readable text
textxalign: 'center', // Always good to set this
}, function (err, png) {
if (err) {
// `err` may be a string or Error object
} else {
// `png` is a Buffer
// png.length : PNG file length
// png.readUInt32BE(16) : PNG image width
// png.readUInt32BE(20) : PNG image height
}
}); If you would prefer to work with Promises, omit the callback function and
bwipjs.toBuffer({
bcid: 'code128', // Barcode type
text: '0123456789', // Text to encode
scale: 3, // 3x scaling factor
height: 10, // Bar height, in millimeters
includetext: true, // Show human-readable text
textxalign: 'center', // Always good to set this
})
.then(png => {
// `png` is a Buffer as in the example above
})
.catch(err => {
// `err` may be a string or Error object
}); Node.js ES6 Module UsageThe ESM provides the same API as import bwipjs from 'bwip-js';
// ... identical to the examples above ... The ESM also facilitates bundler tree-shaking by providing the individual encoders as named exports. Each exported encoder functions identically to The exported names are the same as the import { gs1_128 } from 'bwip-js';
try {
let buf = await gs1_128(options);
} catch (e) {
// `e` may be a string or Error object
} When named encoders are imported, the Electron ExampleThere have been some changes to the Electron bundler, and it may pull in either the nodejs or browser module, depending on your version of Electron. The example below assumes the nodejs module. If you try this example and get the error This is an example <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
</head>
<body>
Node.js <script>document.write(process.versions.node)</script>,
Chromium <script>document.write(process.versions.chrome)</script>,
Electron <script>document.write(process.versions.electron)</script>,
bwip-js <script>document.write(bwipjs.VERSION)</script>,
and BWIPP <script>document.write(bwipjs.BWIPP.VERSION)</script>.
<br><br><img id="myimg">
<pre id="output"></pre>
</body>
<script>
var bwipjs = require('bwip-js');
bwipjs.toBuffer({ bcid:'qrcode', text:'0123456789' }, function (err, png) {
if (err) {
document.getElementById('output').textContent = err;
} else {
document.getElementById('myimg').src = 'data:image/png;base64,' +
png.toString('base64');
}
});
</script>
</html> Command Line Interfacebwip-js can be used as a command line tool when installed globally:
To use a custom font with the command line utility, use the
For example:
The above demonstrates how to maniplulate the font metrics so the characters appear tall and narrow. Demo HTML App
ExamplesThere are example html and node apps provided with the project including how to write your own drawing interface, generating SVG barcode images, and adding scalable barcodes to a pdfkit document. See the examples README for more details. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论