Set Barcode Source
Contents
[
Hide
]
To perform barcode recognition using the Aspose.BarCode.Cloud SDK for Go, you need to set the source of the barcode image. Methods of this SDK support loading barcode images from multiple sources, such as file URL, *File, and base64 encoded data. This guide provides detailed examples on how to set barcode image source through different API methods.
Supported Barcode Image Formats
- JPEG
- TIFF
- PNG
- BMP
- GIF
Supported Barcode Source Formats
- File URL: Link directly to an image URL.
- *File: The image file for processing.
- Base64 Encoded Image: Convert the image to a base64 encoded string for secure transfer.
Examples for ScanAPI
- Using GET Request with File URL
package main
import (
"context"
"fmt"
"os"
"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 Recognize() {
client, authCtx, err := makeConfiguration()
if err != nil {
fmt.Printf("Error setting up configuration: %v\n", err)
return
}
fileUrl := "https://products.aspose.app/barcode/scan/img/how-to/scan/step2.png"
opts := &barcode.RecognizeAPIRecognizeOpts{}
result, _, err := client.RecognizeAPI.Recognize(authCtx, barcode.DecodeBarcodeTypeQR, fileUrl, opts)
if err != nil {
fmt.Printf("Error recognizing barcode: %v\n", err)
return
}
if len(result.Barcodes) > 0 {
fmt.Printf("Recognized barcode value: '%s'\n", result.Barcodes[0].BarcodeValue)
} else {
fmt.Println("No barcodes recognized.")
}
}
func main() {
Recognize()
}
- Using POST Request with Base64 Encoded Image
package main
import (
"context"
"encoding/base64"
"fmt"
"io/ioutil"
"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", "ManyTypes.png"))
imageBytes, err := ioutil.ReadFile(fileName)
if err != nil {
panic(err)
}
imageBase64 := base64.StdEncoding.EncodeToString(imageBytes)
base64Request := barcode.ScanBase64Request{
FileBase64: imageBase64,
}
result, _, err := client.ScanAPI.ScanBase64(authCtx, base64Request)
if err != nil {
panic(err)
}
if len(result.Barcodes) > 0 {
fmt.Printf("File '%s' recognized, result: '%s'\n", fileName, result.Barcodes[0].BarcodeValue)
} else {
fmt.Printf("File '%s' recognized, but no barcodes found.\n", fileName)
}
}
- Using POST Request with File Stream
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", "qr.png"))
file, err := os.Open(fileName)
if err != nil {
panic(err)
}
defer file.Close()
response, _, err := client.ScanAPI.ScanMultipart(authCtx, file)
if err != nil {
panic(err)
}
if len(response.Barcodes) > 0 {
fmt.Printf("File '%s' recognized, results: value: '%s', type: %s\n",
fileName, response.Barcodes[0].BarcodeValue, response.Barcodes[0].Type)
} else {
fmt.Printf("File '%s' recognized, but no barcodes found.\n", fileName)
}
}
Examples for RecognizeAPI
- Using GET Request with File URL
package main
import (
"context"
"fmt"
"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) {
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 Recognize() {
client, authCtx, err := makeConfiguration()
if err != nil {
fmt.Printf("Error setting up configuration: %v\n", err)
return
}
fileUrl := "https://products.aspose.app/barcode/scan/img/how-to/scan/step2.png"
opts := &barcode.RecognizeAPIRecognizeOpts{}
result, _, err := client.RecognizeAPI.Recognize(authCtx, barcode.DecodeBarcodeTypeQR, fileUrl, opts)
if err != nil {
fmt.Printf("Error recognizing barcode: %v\n", err)
return
}
if len(result.Barcodes) > 0 {
fmt.Printf("File recognized, result: '%s'\n", result.Barcodes[0].BarcodeValue)
} else {
fmt.Println("No barcodes were recognized.")
}
}
func main() {
Recognize()
}
- Using POST Request with Base64 Encoded Image
package main
import (
"context"
"encoding/base64"
"fmt"
"io/ioutil"
"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"))
imageBytes, err := ioutil.ReadFile(fileName)
if err != nil {
panic(err)
}
imageBase64 := base64.StdEncoding.EncodeToString(imageBytes)
base64Request := barcode.RecognizeBase64Request{
BarcodeTypes: []barcode.DecodeBarcodeType{barcode.DecodeBarcodeTypePdf417},
FileBase64: imageBase64,
}
result, _, err := client.RecognizeAPI.RecognizeBase64(authCtx, base64Request)
if err != nil {
panic(err)
}
if len(result.Barcodes) > 0 {
fmt.Printf("File '%s' recognized, result: '%s'\n", fileName, result.Barcodes[0].BarcodeValue)
} else {
fmt.Printf("File '%s' recognized, but no barcodes found.\n", fileName)
}
}
- Using POST Request with File Stream
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
}
filePath, err := filepath.Abs(filepath.Join("testdata", "qr.png"))
file, err := os.Open(filePath)
if err != nil {
panic(err)
}
defer file.Close()
opts := &barcode.RecognizeAPIRecognizeMultipartOpts{}
response, _, err := client.RecognizeAPI.RecognizeMultipart(authCtx, barcode.DecodeBarcodeTypeQR, file, opts)
if err != nil {
panic(err)
}
if len(response.Barcodes) > 0 {
fmt.Printf("File '%s' recognized, result: '%s'\n", filePath, response.Barcodes[0].BarcodeValue)
} else {
fmt.Printf("No barcodes found in '%s'.\n", filePath)
}
}
Conclusion
Using the Aspose.BarCode.Cloud SDK for .NET, you can easily load and recognize barcode images in file URL, *File, and base64 encoded formats.
Optimize your barcode recognition workflows by selecting the most appropriate method based on your specific image format and application requirements.