Set Barcode Size

This documentation provides a guide on how to set barcode sizes using in the Aspose.BarCode Cloud SDK for Dart. 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.
  • 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 'dart:io';
import 'dart:typed_data';

import 'package:aspose_barcode_cloud/aspose_barcode_cloud.dart';

Configuration makeConfiguration() {
  final envToken = Platform.environment['TEST_CONFIGURATION_ACCESS_TOKEN'];
  if (envToken != null) {
    return Configuration(accessToken: envToken);
  } else {
    return Configuration(
      clientId: "Client Id from https://dashboard.aspose.cloud/applications",
      clientSecret:
          "Client Secret from https://dashboard.aspose.cloud/applications",
    );
  }
}

Future<void> main() async {
  final configuration = makeConfiguration();
  final apiClient = ApiClient(configuration);
  final generateApi = GenerateApi(apiClient);

  final fileName = '${Directory.current.path}${Platform.pathSeparator}qr.png';

  final Uint8List response = await generateApi.generate(
    EncodeBarcodeType.QR,
    "Aspose.BarCode.Cloud",
    foregroundColor: "Black",
    backgroundColor: "White",
    resolution: 300,
    imageHeight: 200,
    imageWidth: 200,
  );

  final file = File(fileName);
  file.writeAsBytes(response);

  print("File '${file.path}' generated.");
}

Result Image is: Result image

Example 2: Generating a Barcode with POST Request (JSON Body)

import 'dart:io';
import 'dart:typed_data';

import 'package:aspose_barcode_cloud/aspose_barcode_cloud.dart';

Configuration makeConfiguration() {
  final envToken = Platform.environment['TEST_CONFIGURATION_ACCESS_TOKEN'];
  if (envToken != null) {
    return Configuration(accessToken: envToken);
  } else {
    return Configuration(
      clientId: "Client Id from https://dashboard.aspose.cloud/applications",
      clientSecret:
          "Client Secret from https://dashboard.aspose.cloud/applications",
    );
  }
}

Future<void> main() async {
  final configuration = makeConfiguration();
  final apiClient = ApiClient(configuration);
  final generateApi = GenerateApi(apiClient);

  final file = File(
    "${Directory.current.path}${Platform.pathSeparator}Pdf417.png",
  );

  final generateParams = GenerateParams(
    EncodeBarcodeType.Pdf417,
    EncodeData("Aspose.BarCode.Cloud", EncodeDataType.StringData),
    BarcodeImageParams()
      ..imageFormat = BarcodeImageFormat.Png
      ..imageHeight = 2
      ..imageWidth = 3
      ..resolution = 96
      ..units = GraphicsUnit.Inch,
  );

  final Uint8List response = await generateApi.generateBody(generateParams);

  file.writeAsBytes(response);

  print("File '${file.path}' generated.");
}

Result Image is: Result image

Example 3: Generating a Barcode with POST Request (Form URL Encoded)

import 'dart:io';
import 'dart:typed_data';

import 'package:aspose_barcode_cloud/aspose_barcode_cloud.dart';

Configuration makeConfiguration() {
  final envToken = Platform.environment['TEST_CONFIGURATION_ACCESS_TOKEN'];
  if (envToken != null) {
    return Configuration(accessToken: envToken);
  } else {
    return Configuration(
      clientId: "Client Id from https://dashboard.aspose.cloud/applications",
      clientSecret:
          "Client Secret from https://dashboard.aspose.cloud/applications",
    );
  }
}

Future<void> main() async {
  final configuration = makeConfiguration();
  final apiClient = ApiClient(configuration);
  final generateApi = GenerateApi(apiClient);

  final fileName =
      '${Directory.current.path}${Platform.pathSeparator}aztec.png';

  final Uint8List response = await generateApi.generateMultipart(
      EncodeBarcodeType.Aztec, "Aspose.BarCode.Cloud",
      imageHeight: 200,
      imageWidth: 200,
      resolution: 150,
      units: GraphicsUnit.Point);

  await File(fileName).writeAsBytes(response);

  print("File '$fileName' generated.");
}

Result Image is: Result image

Conclusion

Setting the correct barcode dimensions ensures optimal readability and compatibility with various scanning devices. With Aspose.BarCode Cloud SDK for Dart, 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 Dart, you can ensure your barcodes are perfectly tailored for their intended use.