Set Barcode Colorscheme
This documentation provides a comprehensive guide on how to set the colorscheme for barcodes using the Aspose.BarCode Cloud SDK for Java. The API allows customization of barcode appearance, including foreground and background colors, ensuring your barcodes match your design needs.
Overview
The GenerateApi
interface includes methods to generate barcodes using different request formats:
- generate: GET request with parameters in route and query string.
- generateBody: POST request with parameters in the body (JSON or XML).
- generateMultipart: POST request with parameters in multipart form.
Colorscheme Parameters
The foregroundColor
and backgroundColor
properties enable you to define the colors for the barcode:
- foregroundColor: Specifies the color of the bars and text.
- backgroundColor: Specifies the background color of the barcode image.
Both properties accept standard color names or ARGB values (e.g., AliceBlue
or #FF000000
).
Examples
Example 1: Setting Colors Using GET Request
import com.aspose.barcode.cloud.ApiClient;
import com.aspose.barcode.cloud.api.GenerateApi;
import com.aspose.barcode.cloud.model.BarcodeImageFormat;
import com.aspose.barcode.cloud.model.EncodeBarcodeType;
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, "https://products.aspose.cloud/barcode/family/");
request.foregroundColor = "DarkBlue";
request.backgroundColor = "LightGray";
request.imageFormat = BarcodeImageFormat.PNG;
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: Setting Colors Using POST Request with JSON Body
import com.aspose.barcode.cloud.ApiClient;
import com.aspose.barcode.cloud.api.GenerateApi;
import com.aspose.barcode.cloud.model.BarcodeImageFormat;
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.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.setForegroundColor("#FF5733");
imageParams.setBackgroundColor("#FFFFFF");
imageParams.setImageFormat(BarcodeImageFormat.JPEG);
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: Setting Colors Using Multipart Form POST
import com.aspose.barcode.cloud.ApiClient;
import com.aspose.barcode.cloud.api.GenerateApi;
import com.aspose.barcode.cloud.model.BarcodeImageFormat;
import com.aspose.barcode.cloud.model.EncodeBarcodeType;
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.CODE39, "Aspose");
request.foregroundColor = "Green";
request.backgroundColor = "Yellow";
request.imageFormat = BarcodeImageFormat.GIF;
File barcodeFile = generateApi.generateMultipart(request);
System.out.println("Barcode image saved to file " + barcodeFile.getAbsolutePath());
} catch (Exception e) {
System.err.println("Error");
e.printStackTrace();
System.exit(1);
}
}
}
Result Image is:
Conclusion
With GenerateApi
in Aspose.BarCode Cloud SDK for Java, you can easily generate barcodes with customized colors. Whether you prefer GET, POST with JSON, or multipart form POST, the flexibility of this API ensures seamless integration and customization of barcodes in your applications.