Set Barcode Text
Contents
[
Hide
]
This documentation covers how to set barcode text in the Aspose.BarCode Cloud SDK for Java. The Barcode API supports multiple methods to generate barcodes in different formats using GET and POST requests through the GenerateApi
. Barcodes can be generated by specifying various parameters such as barcode type, data to encode, image format and display options. The data encoding process involves defining the type of data and the actual content.
GenerateApi
The GenerateApi
interface includes methods for generating barcodes:
- GET Request: Uses parameters in the URL route and query string.
- POST Request (JSON/XML): Parameters are provided in the request body.
- POST Request (Multipart Form): Parameters are sent as URL-encoded form data.
Methods
- 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.
Setting Barcode Text
Properties
dataType
: Specifies the type of data encoding (can be string, base64, or hex). If not set, thedata
value is used as plain text.data
: A string that represents the data to be encoded.
Supported Data Types
Value | Description |
---|---|
STRING_DATA (Default value) | Plain text |
BASE64_BYTES | Base64 encoded binary data |
HEX_BYTES | Hexadecimal encoded binary data |
Barcode Generation Example
Below are examples to demonstrate barcode generation using the GenerateApi
methods.
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.EncodeDataType;
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 getRequest =
new GenerateRequestWrapper(EncodeBarcodeType.QR, "Aspose.BarCode.Cloud");
getRequest.dataType = EncodeDataType.STRING_DATA;
File barcodeImage = generateApi.generate(getRequest);
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.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.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("QXNwb3NlLkJhckNvZGUuQ2xvdWQ=");
encodeData.setDataType(EncodeDataType.BASE64_BYTES);
GenerateParams postParams = new GenerateParams(EncodeBarcodeType.PDF417, encodeData);
GenerateBodyRequestWrapper postRequest = new GenerateBodyRequestWrapper(postParams);
File barcodeImage = generateApi.generateBody(postRequest);
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 (Multipart Form)
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.EncodeDataType;
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 formRequest =
new GenerateMultipartRequestWrapper(
EncodeBarcodeType.CODE128, "4173706F73652E426172436F64652E436C6F7564");
formRequest.dataType = EncodeDataType.HEX_BYTES;
File barcodeImage = generateApi.generateMultipart(formRequest);
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:
This documentation covers how to use the GenerateApi
to set barcodes text.