Set Barcode Colorscheme
This documentation provides a comprehensive guide on how to set the colorscheme for barcodes using the Aspose.BarCode Cloud SDK for Go. 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
package main
import (
"context"
"fmt"
"os"
"path/filepath"
"github.com/antihax/optional"
"github.com/aspose-barcode-cloud/aspose-barcode-cloud-go/v4/barcode"
"github.com/aspose-barcode-cloud/aspose-barcode-cloud-go/v4/barcode/jwt"
)
func makeConfiguration() (*barcode.APIClient, context.Context, error) {
jwtToken := os.Getenv("TEST_JWT_ACCESS_TOKEN")
if jwtToken != "" {
config := barcode.NewConfiguration()
config.AddDefaultHeader("Authorization", "Bearer "+jwtToken)
client := barcode.NewAPIClient(config)
authCtx := context.Background()
return client, authCtx, nil
}
jwtConf := jwt.NewConfig(
"Client Id from https://dashboard.aspose.cloud/applications",
"Client Secret from https://dashboard.aspose.cloud/applications",
)
authCtx := context.WithValue(context.Background(),
barcode.ContextJWT,
jwtConf.TokenSource(context.Background()))
client := barcode.NewAPIClient(barcode.NewConfiguration())
return client, authCtx, nil
}
func main() {
client, authCtx, err := makeConfiguration()
if err != nil {
fmt.Printf("Error setting up configuration: %v\n", err)
return
}
fileName, err := filepath.Abs(filepath.Join("testdata", "qr.png"))
response, _, err := client.GenerateAPI.Generate(authCtx, barcode.EncodeBarcodeTypeQR, "https://products.aspose.cloud/barcode/family/", &barcode.GenerateAPIGenerateOpts{
ForegroundColor: optional.NewString("DarkBlue"),
BackgroundColor: optional.NewString("LightGray"),
ImageFormat: optional.NewInterface(barcode.BarcodeImageFormatPng),
})
if err != nil {
fmt.Printf("Error generating barcode: %v\n", err)
return
}
err = os.WriteFile(fileName, response, 0644)
if err != nil {
fmt.Printf("Error saving barcode to file: %v\n", err)
return
}
fmt.Printf("File '%s' generated.\n", fileName)
}
Result Image is:
Example 2: Setting Colors Using POST Request with JSON Body
package main
import (
"context"
"fmt"
"os"
"path/filepath"
"github.com/aspose-barcode-cloud/aspose-barcode-cloud-go/v4/barcode"
"github.com/aspose-barcode-cloud/aspose-barcode-cloud-go/v4/barcode/jwt"
)
func makeConfiguration() (*barcode.APIClient, context.Context, error) {
jwtToken := os.Getenv("TEST_JWT_ACCESS_TOKEN")
if jwtToken != "" {
config := barcode.NewConfiguration()
config.AddDefaultHeader("Authorization", "Bearer "+jwtToken)
client := barcode.NewAPIClient(config)
authCtx := context.Background()
return client, authCtx, nil
}
jwtConf := jwt.NewConfig(
"Client Id from https://dashboard.aspose.cloud/applications",
"Client Secret from https://dashboard.aspose.cloud/applications",
)
authCtx := context.WithValue(context.Background(),
barcode.ContextJWT,
jwtConf.TokenSource(context.Background()))
client := barcode.NewAPIClient(barcode.NewConfiguration())
return client, authCtx, nil
}
func main() {
client, authCtx, err := makeConfiguration()
if err != nil {
fmt.Printf("Error setting up configuration: %v\n", err)
return
}
fileName, err := filepath.Abs(filepath.Join("testdata", "Pdf417.png"))
imageParams := barcode.BarcodeImageParams{
ForegroundColor: "#FF5733",
BackgroundColor: "#FFFFFF",
ImageFormat: barcode.BarcodeImageFormatJpeg,
}
encodeData := barcode.EncodeData{
Data: "Aspose.BarCode.Cloud",
DataType: barcode.EncodeDataTypeStringData,
}
generateParams := barcode.GenerateParams{
BarcodeType: barcode.EncodeBarcodeTypePdf417,
EncodeData: encodeData,
BarcodeImageParams: imageParams,
}
fileBytes, _, err := client.GenerateAPI.GenerateBody(authCtx, generateParams)
if err != nil {
fmt.Printf("Error generating barcode: %v\n", err)
return
}
err = os.WriteFile(fileName, fileBytes, 0644)
if err != nil {
fmt.Printf("Error saving barcode to file: %v\n", err)
return
}
fmt.Printf("File '%s' generated.\n", fileName)
}
Result Image is:
Example 3: Setting Colors Using Multipart Form POST
package main
import (
"context"
"fmt"
"os"
"path/filepath"
"github.com/antihax/optional"
"github.com/aspose-barcode-cloud/aspose-barcode-cloud-go/v4/barcode"
"github.com/aspose-barcode-cloud/aspose-barcode-cloud-go/v4/barcode/jwt"
)
func makeConfiguration() (*barcode.APIClient, context.Context, error) {
jwtToken := os.Getenv("TEST_JWT_ACCESS_TOKEN")
if jwtToken != "" {
config := barcode.NewConfiguration()
config.AddDefaultHeader("Authorization", "Bearer "+jwtToken)
client := barcode.NewAPIClient(config)
authCtx := context.Background()
return client, authCtx, nil
}
jwtConf := jwt.NewConfig(
"Client Id from https://dashboard.aspose.cloud/applications",
"Client Secret from https://dashboard.aspose.cloud/applications",
)
authCtx := context.WithValue(context.Background(),
barcode.ContextJWT,
jwtConf.TokenSource(context.Background()))
client := barcode.NewAPIClient(barcode.NewConfiguration())
return client, authCtx, nil
}
func main() {
client, authCtx, err := makeConfiguration()
if err != nil {
fmt.Printf("Error setting up configuration: %v\n", err)
return
}
fileName, err := filepath.Abs(filepath.Join("testdata", "..", "Code39.png"))
fileBytes, _, err := client.GenerateAPI.GenerateMultipart(authCtx,
barcode.EncodeBarcodeTypeCode39, "Aspose",
&barcode.GenerateAPIGenerateMultipartOpts{
ImageFormat: optional.NewInterface(barcode.BarcodeImageFormatGif),
ForegroundColor: optional.NewString("#008000"),
BackgroundColor: optional.NewString("#FFFF00"),
})
if err != nil {
fmt.Printf("Error generating barcode: %v\n", err)
return
}
err = os.WriteFile(fileName, fileBytes, 0644)
if err != nil {
fmt.Printf("Error saving barcode to file: %v\n", err)
return
}
fmt.Printf("File '%s' generated.\n", fileName)
}
Result Image is:
Conclusion
With GenerateAPI
in Aspose.BarCode Cloud SDK for Go, 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.