Set Recognition Quality and Speed
When working with barcodes in applications, optimizing recognition quality and speed is essential. The RecognitionMode
setting in the Aspose.BarCode.Cloud SDK for Go offers a way to adjust these parameters, making it possible to prioritize either quality or speed during barcode recognition.
Recognition Mode Options
The RecognitionMode
parameter can be set to the following values to control the recognition process:
- Fast: Optimizes for speed but may lower the accuracy slightly. Minimal recognition timeout.
- Normal: Balances speed and quality, suitable for most cases. Medium recognition timeout.
- Excellent: Prioritizes accuracy over speed, ideal for challenging barcode images. Maximum recognition timeout.
Usage in API Requests
The RecognizeAPI
interface provides methods to recognize barcodes from files using different HTTP request types. Below are the available methods:
recognize
: Recognizes barcode from a file on the server using aGET
request.recognizeBase64
: Recognizes barcode from a file provided in the request body using aPOST
request with JSON or XML format.recognizeMultipart
: Recognizes barcode from a file provided in the request body using aPOST
request withmultipart/form-data
.
Example Implementations
Example 1: Using recognize
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
}
result, _, err := client.RecognizeAPI.Recognize(authCtx, barcode.DecodeBarcodeTypeQR, "https://products.aspose.app/barcode/scan/img/how-to/scan/step2.png", nil)
if err != nil {
panic(err)
}
if len(result.Barcodes) > 0 {
fmt.Printf("File recognized, result: '%s'\n", result.Barcodes[0].BarcodeValue)
} else {
fmt.Printf("File recognized, but no barcodes found.\n")
}
}
Example 2: Using 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", "qr.png"))
imageBytes, err := ioutil.ReadFile(fileName)
if err != nil {
panic(err)
}
imageBase64 := base64.StdEncoding.EncodeToString(imageBytes)
base64Request := barcode.RecognizeBase64Request{
BarcodeTypes: []barcode.DecodeBarcodeType{barcode.DecodeBarcodeTypeQR},
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)
}
}
Example 3: Using recognizeMultipart
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"))
file, err := os.Open(fileName)
if err != nil {
panic(err)
}
defer file.Close()
opts := &barcode.RecognizeAPIRecognizeMultipartOpts{
RecognitionImageKind: optional.NewInterface(barcode.RecognitionImageKindScannedDocument),
RecognitionMode: optional.NewInterface(barcode.RecognitionModeNormal),
}
response, _, err := client.RecognizeAPI.RecognizeMultipart(authCtx, barcode.DecodeBarcodeTypeAztec, file, opts)
if err != nil {
panic(err)
}
if len(response.Barcodes) > 0 {
fmt.Printf("File '%s' recognized with multipart post, result: '%s'\n", fileName, response.Barcodes[0].BarcodeValue)
} else {
fmt.Printf("File '%s' recognized with multipart post, but no barcodes found.\n", fileName)
}
}
Conclusion
Choosing the appropriate RecognitionMode
is essential in tailoring barcode recognition to the specific needs of your application. Whether prioritizing speed or accuracy, the examples provided demonstrate how to use the RecognizeAPI
interface to adjust settings for optimal performance. By fine-tuning RecognitionMode
and RecognitionImageKind
, you can enhance both efficiency and accuracy, making your barcode processing solution robust across various use cases.