Set Barcode Image Kind

This documentation provides an overview of how to use the Aspose.BarCode.Cloud SDK for Go to set the type of barcode image for recognition. Setting the correct RecognitionImageKind is essential for optimizing the recognition process and ensuring high accuracy, particularly when dealing with diverse barcode types or varying image qualities.

Introduction

The Aspose.BarCode.Cloud SDK for Go allows for flexible barcode scanning and recognition from different image sources. One of the features in this API is the ability to specify the RecognitionImageKind based on the source of the barcode image. This improves accuracy by guiding the recognition engine with additional context about the image.

Usage in API Requests

The RecognitionImageKind parameter is optional and can be set in several requests within the API. It is available in the following request classes:

  • recognize
  • recognizeBase64
  • recognizeMultipart

Image Kind Options

The RecognitionImageKind enum provides three options:

  • Photo: Use this option when recognizing barcodes from smartphone cameras. High resolution. Barcode is a large part of the image.
  • ScannedDocument: Select this for scanned documents pages. Medium resolution. Barcode is a small part of the image.
  • ClearImage: Ideal for clean, digitally-rendered barcode images with minimal background noise or preprocessed photos. Low resolution. The barcode is almost the entire image.

Examples

Below are examples of using the RecognitionImageKind setting with various API methods.

Using recognize

The recognize method allows for barcode recognition using a GET request.

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 main() {
	client, authCtx, err := makeConfiguration()
	if err != nil {
		fmt.Printf("Error setting up configuration: %v\n", err)
		return
	}
	imageUrl := "https://products.aspose.app/barcode/scan/img/how-to/scan/step2.png"

	result, _, err := client.RecognizeAPI.Recognize(authCtx, barcode.DecodeBarcodeTypeQR, imageUrl, nil)
	if err != nil {
		panic(err)
	}

	fmt.Printf("File '%s' recognized, result: '%s'\n", imageUrl, result.Barcodes[0].BarcodeValue)
}

Using recognizeBase64

For recognition using a POST request with JSON or XML, use recognizeBase64:

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.RecognizeBase64Request{
		BarcodeTypes:         []barcode.DecodeBarcodeType{barcode.DecodeBarcodeTypeAztec, barcode.DecodeBarcodeTypeQR},
		FileBase64:           imageBase64,
		RecognitionImageKind: barcode.RecognitionImageKindScannedDocument,
	}

	result, _, err := client.RecognizeAPI.RecognizeBase64(authCtx, base64Request)
	if err != nil {
		panic(err)
	}

	if len(result.Barcodes) > 0 {
		fmt.Printf("File '%s' recognized, results: value: '%s', type: %s\n",
			fileName, result.Barcodes[0].BarcodeValue, result.Barcodes[0].Type)
	} else {
		fmt.Printf("File '%s' recognized, but no barcodes found.\n", fileName)
	}
}

Using recognizeMultipart

For file uploads with multipart form data, recognizeMultipart is used:

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.png"))

	file, err := os.Open(fileName)
	if err != nil {
		panic(err)
	}
	defer file.Close()

	result, _, err := client.RecognizeAPI.RecognizeMultipart(authCtx, barcode.DecodeBarcodeTypeMostCommonlyUsed, file,
		&barcode.RecognizeAPIRecognizeMultipartOpts{
			RecognitionImageKind: optional.NewInterface(barcode.RecognitionImageKindScannedDocument),
		})
	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)
	}
}

Conclusion

The Barcode Recognition API provides flexible options to recognize barcodes in various formats including photos, scanned documents, and clear images. By setting the RecognitionImageKind, developers can enhance recognition accuracy, particularly when working with diverse image sources. Supported barcode types include popular formats like QR Code, Aztec, Pdf417, and many more. The API allows integration with GET and POST methods, supporting JSON, XML, and multipart form data formats.

For high accuracy, configure the RecognitionImageKind parameter based on your image source. This small adjustment can lead to faster processing times and better recognition results across all supported barcode types.