Set Barcode Size
This documentation provides a guide on how to set barcode sizes using in the Aspose.BarCode Cloud SDK for Go. 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.- ``: Defines the measurement units (e.g., pixel, inch, millimeter).
Example Usage
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 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"))
fileBytes, _, err := client.GenerateAPI.Generate(authCtx, barcode.EncodeBarcodeTypeQR, "Aspose.BarCode.Cloud",
&barcode.GenerateAPIGenerateOpts{
ImageHeight: optional.NewFloat32(200),
ImageWidth: optional.NewFloat32(200),
Resolution: optional.NewFloat32(300),
ImageFormat: optional.NewInterface(barcode.BarcodeImageFormatPng),
})
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 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", "Pdf417.png"))
imageParams := barcode.BarcodeImageParams{
ImageFormat: barcode.BarcodeImageFormatPng,
ImageHeight: 2,
ImageWidth: 3,
Resolution: 96,
Units: barcode.GraphicsUnitInch,
}
encodeData := barcode.EncodeData{
Data: "Aspose.BarCode.Cloud",
}
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: Generating a Barcode with POST Request (Form URL Encoded)
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", "aztec.png"))
fileBytes, _, err := client.GenerateAPI.GenerateMultipart(authCtx, barcode.EncodeBarcodeTypeAztec, "Aspose.BarCode.Cloud", &barcode.GenerateAPIGenerateMultipartOpts{
ImageHeight: optional.NewFloat32(200),
ImageWidth: optional.NewFloat32(200),
Resolution: optional.NewFloat32(150),
Units: optional.NewInterface(barcode.GraphicsUnitPoint),
})
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
Setting the correct barcode dimensions ensures optimal readability and compatibility with various scanning devices. With Aspose.BarCode Cloud SDK for Go, 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 Go, you can ensure your barcodes are perfectly tailored for their intended use.