Set Barcode Size
This documentation provides a guide on how to set barcode sizes using in the Aspose.BarCode Cloud SDK for Node.js. This guide includes examples and descriptions for configuring the height, width, and resolution of barcode images.
Overview
The GenerateApi
interface offers several methods for generating barcodes through different types of HTTP requests:
- generate: Generates a barcode via GET request.
- generateBody: Generates a barcode via POST request with JSON or XML body.
- generateMultipart: Generates a barcode via POST request with multipart form data.
Key Parameters for Setting Barcode Size
To control the size of a barcode, the following properties in the request objects can be configured:
imageHeight
: Specifies the height of the barcode image.imageWidth
: Specifies the width of the barcode image.- ``: Defines the resolution (DPI) of the barcode image.
units
: Defines the measurement units (e.g., pixel, inch, millimeter).
Example Usage
Example 1: Generating a Barcode with GET Request
const fs = require('fs');
const path = require('path');
const Barcode = require('aspose-barcode-cloud-node');
function makeConfiguration() {
const envToken = process.env['TEST_CONFIGURATION_ACCESS_TOKEN'];
if (!envToken) {
return new Barcode.Configuration(
'Client Id from https://dashboard.aspose.cloud/applications',
'Client Secret from https://dashboard.aspose.cloud/applications',
null,
null
);
} else {
return new Barcode.Configuration(
null,
null,
null,
envToken
);
}
}
const config = makeConfiguration();
async function generateBarcode(api, fileName) {
const request = new Barcode.GenerateRequestWrapper(Barcode.EncodeBarcodeType.Qr, "Aspose.BarCode.Cloud");
request.imageHeight = 200;
request.imageWidth = 200;
request.resolution = 300;
request.units = Barcode.GraphicsUnit.Pixel;
const generated = await api.generate(request);
fs.writeFileSync(fileName, generated.body);
}
const genApi = new Barcode.GenerateApi(config);
const fileName = path.resolve('testdata','Qr.png');
generateBarcode(genApi, fileName)
.then(() => {
console.log('File \'' + fileName + '\' generated.');
})
.catch(err => {
console.error("Error: " + JSON.stringify(err, null, 2));
process.exitCode = 1;
});
Result Image is:
Example 2: Generating a Barcode with POST Request (JSON Body)
const fs = require('fs');
const path = require('path');
const Barcode = require('aspose-barcode-cloud-node');
function makeConfiguration() {
const envToken = process.env['TEST_CONFIGURATION_ACCESS_TOKEN'];
if (!envToken) {
return new Barcode.Configuration(
'Client Id from https://dashboard.aspose.cloud/applications',
'Client Secret from https://dashboard.aspose.cloud/applications',
null,
null
);
} else {
return new Barcode.Configuration(
null,
null,
null,
envToken
);
}
}
const config = makeConfiguration();
async function generateBarcode(api, fileName) {
const imageParams = new Barcode.BarcodeImageParams();
imageParams.imageHeight = 2;
imageParams.imageWidth = 3;
imageParams.resolution = 96;
imageParams.units = Barcode.GraphicsUnit.Inch;
const encodeData = new Barcode.EncodeData();
encodeData.data = 'Aspose.BarCode.Cloud';
encodeData.dataType = Barcode.EncodeDataType.StringData;
const generateParams = new Barcode.GenerateParams();
generateParams.barcodeType = Barcode.EncodeBarcodeType.Pdf417;
generateParams.encodeData = encodeData;
generateParams.barcodeImageParams = imageParams;
const request = new Barcode.GenerateBodyRequestWrapper(generateParams);
const generated = await api.generateBody(request);
fs.writeFileSync(fileName, generated.body);
}
const genApi = new Barcode.GenerateApi(config);
const fileName = path.resolve('testdata','Pdf417.png');
generateBarcode(genApi, fileName)
.then(() => {
console.log('File \'' + fileName + '\' generated.');
})
.catch(err => {
console.error("Error: " + JSON.stringify(err, null, 2));
process.exitCode = 1;
});
Result Image is:
Example 3: Generating a Barcode with POST Request (Form URL Encoded)
const fs = require('fs');
const path = require('path');
const Barcode = require('aspose-barcode-cloud-node');
function makeConfiguration() {
const envToken = process.env['TEST_CONFIGURATION_ACCESS_TOKEN'];
if (!envToken) {
return new Barcode.Configuration(
'Client Id from https://dashboard.aspose.cloud/applications',
'Client Secret from https://dashboard.aspose.cloud/applications',
null,
null
);
} else {
return new Barcode.Configuration(
null,
null,
null,
envToken
);
}
}
const config = makeConfiguration();
async function generateBarcode(api, fileName) {
const request = new Barcode.GenerateMultipartRequestWrapper(Barcode.EncodeBarcodeType.Aztec, 'Aspose.BarCode.Cloud');
request.imageHeight = 200;
request.imageWidth = 200;
request.resolution = 150;
request.units = Barcode.GraphicsUnit.Point;
const generated = await api.generateMultipart(request);
fs.writeFileSync(fileName, generated.body);
}
const genApi = new Barcode.GenerateApi(config);
const fileName = path.resolve('testdata','aztec.png');
generateBarcode(genApi, fileName)
.then(() => {
console.log('File \'' + fileName + '\' generated.');
})
.catch(err => {
console.error("Error: " + JSON.stringify(err, null, 2));
process.exitCode = 1;
});
Result Image is:
Conclusion
Setting the correct barcode dimensions ensures optimal readability and compatibility with various scanning devices. With Aspose.BarCode Cloud SDK for Node.js, you can customize the barcode’s height, width, and resolution to meet specific requirements, whether for product labeling, logistics, or QR code marketing.
Proper barcode sizing improves scanning accuracy and ensures a better user experience. By setting the right dimensions using Aspose.BarCode Cloud SDK for Node.js, you can ensure your barcodes are perfectly tailored for their intended use.