Set Barcode Size
This documentation provides a guide on how to set barcode sizes using in the Aspose.BarCode Cloud SDK for Python. 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.
- generate_body: Generates a barcode via POST request with JSON or XML body.
- generate_multipart: 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:
image_height
: Specifies the height of the barcode image.image_width
: Specifies the width of the barcode image.resolution
: 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
import os
from aspose_barcode_cloud import (
ApiClient,
EncodeBarcodeType,
GenerateParams,
BarcodeImageParams,
BarcodeImageFormat,
EncodeData,
EncodeDataType,
Configuration,
)
from aspose_barcode_cloud.api.generate_api import GenerateApi
def make_configuration():
env_token = os.getenv("TEST_CONFIGURATION_ACCESS_TOKEN")
if env_token:
config = Configuration(access_token=env_token)
else:
config = Configuration(
client_id="Client Id from https://dashboard.aspose.cloud/applications",
client_secret="Client Secret from https://dashboard.aspose.cloud/applications",
)
return config
def main():
file_name = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "testdata", "qr.png"))
configuration = make_configuration()
api_client = ApiClient(configuration=configuration)
generate_api = GenerateApi(api_client=api_client)
generator_params = GenerateParams(
EncodeBarcodeType.QR,
EncodeData(EncodeDataType.STRINGDATA, "Aspose.BarCode.Cloud"),
BarcodeImageParams(
BarcodeImageFormat.PNG,
image_height=200,
image_width=200,
resolution=300,
),
)
response = generate_api.generate_body(generator_params)
with open(file_name, "wb") as file:
file.write(response.data)
print(f"File '{file_name}' generated.")
# Run the main function
if __name__ == "__main__":
main()
Result Image is:
Example 2: Generating a Barcode with POST Request (JSON Body)
import os
from aspose_barcode_cloud import (
ApiClient,
EncodeBarcodeType,
EncodeDataType,
EncodeData,
GenerateParams,
BarcodeImageParams,
BarcodeImageFormat,
GraphicsUnit,
Configuration,
)
from aspose_barcode_cloud.api.generate_api import GenerateApi
def make_configuration():
env_token = os.getenv("TEST_CONFIGURATION_ACCESS_TOKEN")
if env_token:
config = Configuration(access_token=env_token)
else:
config = Configuration(
client_id="Client Id from https://dashboard.aspose.cloud/applications",
client_secret="Client Secret from https://dashboard.aspose.cloud/applications",
)
return config
def main():
# Define the output file path
file_name = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "testdata", "pdf417.png"))
# Create API client and generate parameters
configuration = make_configuration()
api_client = ApiClient(configuration=configuration)
generate_api = GenerateApi(api_client=api_client)
generate_params = GenerateParams(
EncodeBarcodeType.PDF417,
EncodeData(data="Aspose.BarCode.Cloud"),
BarcodeImageParams(
image_format=BarcodeImageFormat.PNG, image_height=2, image_width=3, resolution=96, units=GraphicsUnit.INCH
),
)
# Generate barcode and save to file
response = generate_api.generate_body(generate_params)
with open(file_name, "wb") as file:
file.write(response.data)
print(f"File '{file_name}' generated.")
# Entry point for async execution
if __name__ == "__main__":
main()
Result Image is:
Example 3: Generating a Barcode with POST Request (Form URL Encoded)
import os
from aspose_barcode_cloud import ApiClient, EncodeBarcodeType, Configuration, GraphicsUnit
from aspose_barcode_cloud.api.generate_api import GenerateApi
def make_configuration():
env_token = os.getenv("TEST_CONFIGURATION_ACCESS_TOKEN")
if env_token:
config = Configuration(access_token=env_token)
else:
config = Configuration(
client_id="Client Id from https://dashboard.aspose.cloud/applications",
client_secret="Client Secret from https://dashboard.aspose.cloud/applications",
)
return config
def main():
config = make_configuration()
api_client = ApiClient(configuration=config)
api = GenerateApi(api_client=api_client)
file_name = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "testdata", "aztec.png"))
response = api.generate_multipart(
barcode_type=EncodeBarcodeType.AZTEC,
data="Aspose.BarCode.Cloud",
image_height=200,
image_width=200,
resolution=150,
units=GraphicsUnit.POINT,
)
with open(file_name, "wb") as file_stream:
file_stream.write(response.data)
print(f"File '{file_name}' generated.")
if __name__ == "__main__":
main()
Result Image is:
Conclusion
Setting the correct barcode dimensions ensures optimal readability and compatibility with various scanning devices. With Aspose.BarCode Cloud SDK for Python, 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 Python, you can ensure your barcodes are perfectly tailored for their intended use.