Set Barcode Size
This documentation provides a guide on how to set barcode sizes using in the Aspose.BarCode Cloud SDK for Java. 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 com.aspose.barcode.cloud.ApiClient;
import com.aspose.barcode.cloud.api.GenerateApi;
import com.aspose.barcode.cloud.model.EncodeBarcodeType;
import com.aspose.barcode.cloud.model.GraphicsUnit;
import com.aspose.barcode.cloud.requests.GenerateRequestWrapper;
import java.io.File;
public class GenerateGet {
public static void main(String[] args) {
String accessToken = System.getenv("TEST_CONFIGURATION_ACCESS_TOKEN");
ApiClient client;
if (accessToken != null && !accessToken.isEmpty()) {
client = new ApiClient(accessToken);
} else {
client =
new ApiClient(
"Client Id from https://dashboard.aspose.cloud/applications",
"Client Secret from https://dashboard.aspose.cloud/applications");
}
GenerateApi generateApi = new GenerateApi(client);
try {
GenerateRequestWrapper request =
new GenerateRequestWrapper(EncodeBarcodeType.QR, "Aspose.BarCode.Cloud");
request.imageHeight = 200f;
request.imageWidth = 200f;
request.resolution = 300f;
request.units = GraphicsUnit.PIXEL;
File barcodeImage = generateApi.generate(request);
System.out.println("Barcode image saved to file " + barcodeImage.getAbsolutePath());
} catch (Exception e) {
System.err.println("Error");
e.printStackTrace();
System.exit(1);
}
}
}
Result Image is:
Example 2: Generating a Barcode with POST Request (JSON Body)
import com.aspose.barcode.cloud.ApiClient;
import com.aspose.barcode.cloud.api.GenerateApi;
import com.aspose.barcode.cloud.model.BarcodeImageParams;
import com.aspose.barcode.cloud.model.EncodeBarcodeType;
import com.aspose.barcode.cloud.model.EncodeData;
import com.aspose.barcode.cloud.model.EncodeDataType;
import com.aspose.barcode.cloud.model.GenerateParams;
import com.aspose.barcode.cloud.model.GraphicsUnit;
import com.aspose.barcode.cloud.requests.GenerateBodyRequestWrapper;
import java.io.File;
public class GenerateBody {
public static void main(String[] args) {
String accessToken = System.getenv("TEST_CONFIGURATION_ACCESS_TOKEN");
ApiClient client;
if (accessToken != null && !accessToken.isEmpty()) {
client = new ApiClient(accessToken);
} else {
client =
new ApiClient(
"Client Id from https://dashboard.aspose.cloud/applications",
"Client Secret from https://dashboard.aspose.cloud/applications");
}
GenerateApi generateApi = new GenerateApi(client);
try {
EncodeData encodeData = new EncodeData("Aspose.BarCode.Cloud");
encodeData.setDataType(EncodeDataType.STRING_DATA);
BarcodeImageParams imageParams = new BarcodeImageParams();
imageParams.setImageHeight(2f);
imageParams.setImageWidth(3f);
imageParams.setResolution(96f);
imageParams.setUnits(GraphicsUnit.INCH);
GenerateParams generateParams =
new GenerateParams(EncodeBarcodeType.PDF417, encodeData);
generateParams.setBarcodeImageParams(imageParams);
GenerateBodyRequestWrapper request = new GenerateBodyRequestWrapper(generateParams);
File barcodeImage = generateApi.generateBody(request);
System.out.println("Barcode image saved to file " + barcodeImage.getAbsolutePath());
} catch (Exception e) {
System.err.println("Error");
e.printStackTrace();
System.exit(1);
}
}
}
Result Image is:
Example 3: Generating a Barcode with POST Request (Form URL Encoded)
import com.aspose.barcode.cloud.ApiClient;
import com.aspose.barcode.cloud.api.GenerateApi;
import com.aspose.barcode.cloud.model.EncodeBarcodeType;
import com.aspose.barcode.cloud.model.GraphicsUnit;
import com.aspose.barcode.cloud.requests.GenerateMultipartRequestWrapper;
import java.io.File;
public class GenerateMultipart {
public static void main(String[] args) {
String accessToken = System.getenv("TEST_CONFIGURATION_ACCESS_TOKEN");
ApiClient client;
if (accessToken != null && !accessToken.isEmpty()) {
client = new ApiClient(accessToken);
} else {
client =
new ApiClient(
"Client Id from https://dashboard.aspose.cloud/applications",
"Client Secret from https://dashboard.aspose.cloud/applications");
}
GenerateApi generateApi = new GenerateApi(client);
try {
GenerateMultipartRequestWrapper request =
new GenerateMultipartRequestWrapper(
EncodeBarcodeType.AZTEC, "Aspose.BarCode.Cloud");
request.imageHeight = 200f;
request.imageWidth = 200f;
request.resolution = 150f;
request.units = GraphicsUnit.POINT;
File barcodeImage = generateApi.generateMultipart(request);
System.out.println("Barcode image saved to file " + barcodeImage.getAbsolutePath());
} catch (Exception e) {
System.err.println("Error");
e.printStackTrace();
System.exit(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 Java, 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 Java, you can ensure your barcodes are perfectly tailored for their intended use.