Generate QR Code:
using Aspose.BarCode.Cloud.Sdk.Api;
using Aspose.BarCode.Cloud.Sdk.Interfaces;
using Aspose.BarCode.Cloud.Sdk.Model;
using System;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
namespace GenerateQR;
internal static class Program
{
private static Configuration MakeConfiguration()
{
var config = new Configuration();
string? envToken = Environment.GetEnvironmentVariable("TEST_CONFIGURATION_JWT_TOKEN");
if (string.IsNullOrEmpty(envToken))
{
config.ClientId = "Client Id from https://dashboard.aspose.cloud/applications";
config.ClientSecret = "Client Secret from https://dashboard.aspose.cloud/applications";
}
else
{
config.JwtToken = envToken;
}
return config;
}
private static async Task GenerateQR(IGenerateApi api, string fileName)
{
await using Stream generated = await api.GenerateAsync(
EncodeBarcodeType.QR,
"QR code text",
textLocation: CodeLocation.None,
imageFormat: BarcodeImageFormat.Png
);
await using FileStream stream = File.Create(fileName);
await generated.CopyToAsync(stream);
}
public static async Task Main(string[] args)
{
string fileName = Path.GetFullPath(Path.Join(
Path.GetDirectoryName(Assembly.GetEntryAssembly()!.Location),
"..", "..", "..", "..",
"qr.png"
));
GenerateApi api = new GenerateApi(MakeConfiguration());
await GenerateQR(api, fileName);
Console.WriteLine($"File '{fileName}' generated.");
}
}
Read QR Code:
using Aspose.BarCode.Cloud.Sdk.Api;
using Aspose.BarCode.Cloud.Sdk.Interfaces;
using Aspose.BarCode.Cloud.Sdk.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
namespace ReadQR;
internal static class Program
{
private static Configuration MakeConfiguration()
{
var config = new Configuration();
string? envToken = Environment.GetEnvironmentVariable("TEST_CONFIGURATION_JWT_TOKEN");
if (string.IsNullOrEmpty(envToken))
{
config.ClientId = "Client Id from https://dashboard.aspose.cloud/applications";
config.ClientSecret = "Client Secret from https://dashboard.aspose.cloud/applications";
}
else
{
config.JwtToken = envToken;
}
return config;
}
private static async Task<string> ReadQR(IRecognizeApi api, string fileName)
{
byte[] imageBytes = await File.ReadAllBytesAsync(fileName);
string imageBase64 = Convert.ToBase64String(imageBytes);
BarcodeResponseList recognized = await api.RecognizeBase64Async(
new RecognizeBase64Request()
{
BarcodeTypes = new List<DecodeBarcodeType> { DecodeBarcodeType.QR },
FileBase64 = imageBase64
}
);
return recognized.Barcodes[0].BarcodeValue;
}
public static async Task Main(string[] args)
{
string fileName = Path.GetFullPath(Path.Join(
Path.GetDirectoryName(Assembly.GetEntryAssembly()!.Location),
"..", "..", "..", "..",
"qr.png"
));
var api = new RecognizeApi(MakeConfiguration());
string result = await ReadQR(api, fileName);
Console.WriteLine($"File '{fileName}' recognized, result: '{result}'");
}
}
<?php
declare(strict_types=1);
require __DIR__ . '/vendor/autoload.php';
use Aspose\BarCode\Configuration;
use Aspose\BarCode\GenerateApi;
use Aspose\BarCode\Requests\GenerateRequestWrapper;
use Aspose\BarCode\Model\{EncodeBarcodeType, EncodeDataType, CodeLocation, BarcodeImageFormat};
$config = new Configuration();
$config->setClientId('ClientId from https://dashboard.aspose.cloud/applications');
$config->setClientSecret('Client Secret from https://dashboard.aspose.cloud/applications');
if (getenv("TEST_CONFIGURATION_ACCESS_TOKEN")) {
$config->setAccessToken(getenv("TEST_CONFIGURATION_ACCESS_TOKEN"));
}
$request = new GenerateRequestWrapper(EncodeBarcodeType::QR, 'PHP SDK Test');
$request->image_format = BarcodeImageFormat::Png;
$request->text_location = CodeLocation::None;
$api = new GenerateApi(null, $config);
$response = $api->generate($request);
$type = 'image/png';
$size = $response->getSize();
header("Content-Type: $type");
header("Content-Length: $size");
echo $response->fread($size);
const fs = require('fs');
const Barcode = require('aspose-barcode-cloud-node');
const config = new Barcode.Configuration(
'Client Id from https://dashboard.aspose.cloud/applications',
'Client Secret from https://dashboard.aspose.cloud/applications',
null,
process.env['TEST_CONFIGURATION_ACCESS_TOKEN']
);
async function generateBarcode(api) {
const request = new Barcode.GenerateRequestWrapper(
Barcode.EncodeBarcodeType.Qr,
'Aspose.BarCode for Cloud Sample');
const oneBarcode = await api.generate(request);
const fileName = 'QR.png'
fs.writeFileSync(fileName, oneBarcode.body);
return fileName;
}
async function scanBarcode(api, fileName) {
const scanRequest = new Barcode.ScanMultipartRequestWrapper(fs.readFileSync(fileName));
const result = await api.scanMultipart(scanRequest);
return result.body.barcodes;
}
const genApi = new Barcode.GenerateApi(config);
const scanApi = new Barcode.ScanApi(config);
console.log('Generating barcode...');
generateBarcode(genApi)
.then(fileName => {
console.log('Barcode saved to ' + fileName);
console.log('Trying to recognize barcode...');
scanBarcode(scanApi, fileName)
.then(barcodes => {
console.log('Recognized barcodes are:');
console.log(JSON.stringify(barcodes, null, 2));
});
})
.catch(err => {
console.error("Error: " + JSON.stringify(err, null, 2));
process.exitCode = 1;
});
import os
from pprint import pprint
from aspose_barcode_cloud import (
GenerateApi,
RecognizeApi,
ApiClient,
Configuration,
EncodeBarcodeType,
CodeLocation,
DecodeBarcodeType,
)
config = Configuration(
client_id="Client Id from https://dashboard.aspose.cloud/applications",
client_secret="Client Secret from https://dashboard.aspose.cloud/applications",
access_token=os.environ.get("TEST_CONFIGURATION_ACCESS_TOKEN"), # Only for testing in CI, remove this line
)
# Generate barcode
generateApi = GenerateApi(ApiClient(config))
response = generateApi.generate(EncodeBarcodeType.QR, "Example", text_location=CodeLocation.NONE)
with open("example.png", "wb") as f:
f.write(response.data)
print("Barcode saved to file 'example.png'")
# Recognize barcode
recognizeApi = RecognizeApi(ApiClient(config))
response = recognizeApi.recognize_multipart(DecodeBarcodeType.QR, open("example.png", "rb"))
pprint(response)
Generate QR Code:
package main
import (
"context"
"fmt"
"os"
"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 main() {
jwtConf := jwt.NewConfig(
"Client Id from https://dashboard.aspose.cloud/applications",
"Client Secret from https://dashboard.aspose.cloud/applications",
)
fileName := "testdata/generated.png"
authCtx := context.WithValue(context.Background(),
barcode.ContextJWT,
jwtConf.TokenSource(context.Background()))
client := barcode.NewAPIClient(barcode.NewConfiguration())
opts := &barcode.GenerateAPIGenerateOpts{
TextLocation: optional.NewInterface(barcode.CodeLocationNone),
}
data, _, err := client.GenerateAPI.Generate(authCtx,
barcode.EncodeBarcodeTypeQR,
"Go SDK example",
opts)
if err != nil {
panic(err)
}
out, err := os.Create(fileName)
if err != nil {
panic(err)
}
defer func(out *os.File) {
_ = out.Close()
}(out)
written, err := out.Write(data)
if err != nil {
panic(err)
}
fmt.Printf("Written %d bytes to file %s\n", written, fileName)
}
Scan QR Code:
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 main() {
jwtConf := jwt.NewConfig(
"Client Id from https://dashboard.aspose.cloud/applications",
"Client Secret from https://dashboard.aspose.cloud/applications",
)
fileName := "testdata/generated.png"
imageFile, err := os.Open(fileName)
if err != nil {
panic(err)
}
defer func(file *os.File) {
_ = file.Close()
}(imageFile)
client := barcode.NewAPIClient(barcode.NewConfiguration())
authCtx := context.WithValue(context.Background(),
barcode.ContextJWT,
jwtConf.TokenSource(context.Background()))
recognized, _, err := client.ScanAPI.ScanMultipart(
authCtx,
imageFile)
if err != nil {
panic(err)
}
if len(recognized.Barcodes) == 0 {
fmt.Printf("No barcodes in %s", fileName)
}
for i, oneBarcode := range recognized.Barcodes {
fmt.Printf("Recognized #%d: %s %s", i+1, oneBarcode.Type, oneBarcode.BarcodeValue)
}
}
import 'dart:io';
import 'dart:typed_data';
import 'package:aspose_barcode_cloud/aspose_barcode_cloud.dart';
Future<void> main() async {
const fileName = "qr.png";
final client = ApiClient(Configuration(
clientId: "Client Id from https://dashboard.aspose.cloud/applications",
clientSecret:
"Client Secret from https://dashboard.aspose.cloud/applications",
// For testing only
accessToken: Platform.environment["TEST_CONFIGURATION_ACCESS_TOKEN"],
));
final genApi = GenerateApi(client);
final scanApi = ScanApi(client);
// Generate image with barcode
final Uint8List generated =
await genApi.generate(EncodeBarcodeType.QR, "text");
// Save generated image to file
File(fileName).writeAsBytesSync(generated);
print("Generated image saved to '$fileName'");
// Recognize generated image
final BarcodeResponseList recognized = await scanApi.scanMultipart(generated);
if (recognized.barcodes.isNotEmpty) {
print("Recognized Type: ${recognized.barcodes[0].type!}");
print("Recognized Value: ${recognized.barcodes[0].barcodeValue!}");
} else {
print("No barcode found");
}
}
package com.aspose.barcode.cloud.examples;
import com.aspose.barcode.cloud.ApiClient;
import com.aspose.barcode.cloud.ApiException;
import com.aspose.barcode.cloud.api.GenerateApi;
import com.aspose.barcode.cloud.api.ScanApi;
import com.aspose.barcode.cloud.model.BarcodeImageFormat;
import com.aspose.barcode.cloud.model.BarcodeResponseList;
import com.aspose.barcode.cloud.model.EncodeBarcodeType;
import com.aspose.barcode.cloud.requests.GenerateRequestWrapper;
import com.aspose.barcode.cloud.requests.ScanMultipartRequestWrapper;
import java.io.File;
public class Example {
public static void main(String[] args) {
ApiClient client =
new ApiClient(
"Client Id from https://dashboard.aspose.cloud/applications",
"Client Secret from https://dashboard.aspose.cloud/applications");
GenerateApi genApi = new GenerateApi(client);
ScanApi scanApi = new ScanApi(client);
try {
System.out.println("Generating barcode...");
File barcodeImage = generateBarcode(genApi);
System.out.println("Barcode image saved to file " + barcodeImage.getAbsolutePath());
System.out.println("Recognizing barcode on image...");
BarcodeResponseList recognized = scanBarcode(scanApi, barcodeImage);
System.out.print("Barcode on image:");
System.out.println(recognized.toString());
} catch (ApiException e) {
System.err.println("Error");
e.printStackTrace();
}
}
private static File generateBarcode(GenerateApi api) throws ApiException {
EncodeBarcodeType type = EncodeBarcodeType.QR;
String text = "Aspose.BarCode for Cloud Sample";
GenerateRequestWrapper request = new GenerateRequestWrapper(type, text);
request.imageFormat = BarcodeImageFormat.JPEG;
return api.generate(request);
}
private static BarcodeResponseList scanBarcode(ScanApi api, File barcodeImage)
throws ApiException {
ScanMultipartRequestWrapper request = new ScanMultipartRequestWrapper(barcodeImage);
return api.scanMultipart(request);
}
}