Customize Barcode Appearance
Contents
[
Hide
]
This documentation explains how to customize the appearance of barcodes using the Aspose.BarCode Cloud SDK for Go. It provides details on the available methods, customization options, and examples of usage.
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.
Customization Options
When generating a barcode, you can customize various aspects of its appearance using the properties of the request classes.
Key Properties:
- barcodeType: Specifies the type of barcode to generate (e.g.
QR
,Aztec
,Pdf417
,Code39
, etc). - data: The data to encode in the barcode.
- ImageFormat: Specifies the output format of the barcode image (e.g.,
Png
,Jpeg
,Svg
). - ForegroundColor: Color of the barcode bars, supports standard color names or ARGB values (e.g.,
AliceBlue
or#FF000000
). - BackgroundColor: Background color of the barcode image.
- TextLocation: Position of the code text relative to the barcode (
Below
,Above
,None
). - ****: Measurement units for image dimensions (
Pixel
,Inch
,Millimeter
). - ****: Resolution of the image in dpi.
- ImageHeight and ImageWidth: Dimensions of the barcode image.
- RotationAngle: Rotation angle of the barcode (e.g., 0, 90, 180).
Examples
Example 1: Generating a Barcode with 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 Generate(client *barcode.APIClient, authCtx context.Context) error {
fileName, err := filepath.Abs(filepath.Join("testdata", "qr.png"))
opts := &barcode.GenerateAPIGenerateOpts{
ImageFormat: optional.NewInterface(barcode.BarcodeImageFormatPng),
}
fileBytes, _, err := client.GenerateAPI.Generate(authCtx, barcode.EncodeBarcodeTypeQR, "Aspose.BarCode.Cloud", opts)
if err != nil {
return fmt.Errorf("error generating barcode: %v", err)
}
err = os.WriteFile(fileName, fileBytes, 0644)
if err != nil {
return fmt.Errorf("error saving barcode to file: %v", err)
}
fmt.Printf("File '%s' generated.\n", fileName)
return nil
}
func main() {
client, authCtx, err := makeConfiguration()
if err != nil {
fmt.Printf("Error setting up configuration: %v\n", err)
return
}
err = Generate(client, authCtx)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
}
Result Image is:
Example 2: Generating a Barcode with POST Request (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", "Code39.jpeg"))
imageParams := barcode.BarcodeImageParams{
ForegroundColor: "#FF0000",
BackgroundColor: "#FFFF00",
ImageFormat: barcode.BarcodeImageFormatJpeg,
RotationAngle: 90,
}
encodeData := barcode.EncodeData{
Data: "Aspose",
DataType: barcode.EncodeDataTypeStringData,
}
generateParams := barcode.GenerateParams{
BarcodeType: barcode.EncodeBarcodeTypeCode39,
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: Generating a Barcode with POST Request (Multipart Form)
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", "Pdf417.svg"))
fileBytes, _, err := client.GenerateAPI.GenerateMultipart(authCtx, barcode.EncodeBarcodeTypePdf417, "Aspose.BarCode.Cloud", &barcode.GenerateAPIGenerateMultipartOpts{
TextLocation: optional.NewInterface(barcode.CodeLocationAbove),
ImageFormat: optional.NewInterface(barcode.BarcodeImageFormatSvg),
})
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: