Skip to content

Commit 43c1a3a

Browse files
author
agent-aspose-barcode-examples
committed
Add generated examples for swiss-qr-code
1 parent da92c56 commit 43c1a3a

24 files changed

Lines changed: 1464 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.BarCode;
4+
using Aspose.BarCode.BarCodeRecognition;
5+
using Aspose.BarCode.ComplexBarcode;
6+
7+
class Program
8+
{
9+
static void Main()
10+
{
11+
// Path to the SwissQR barcode image
12+
string imagePath = "SwissQR.png";
13+
14+
if (!File.Exists(imagePath))
15+
{
16+
Console.WriteLine($"File not found: {imagePath}");
17+
return;
18+
}
19+
20+
// Read the barcode from the image using all supported types
21+
using (var reader = new BarCodeReader(imagePath, DecodeType.AllSupportedTypes))
22+
{
23+
var results = reader.ReadBarCodes();
24+
25+
if (results == null || results.Length == 0)
26+
{
27+
Console.WriteLine("No barcode detected.");
28+
return;
29+
}
30+
31+
// Take the first detected barcode
32+
string encodedText = results[0].CodeText;
33+
34+
// Decode the SwissQR codetext
35+
SwissQRCodetext decoded = ComplexCodetextReader.TryDecodeSwissQR(encodedText);
36+
37+
if (decoded == null)
38+
{
39+
Console.WriteLine("Failed to decode SwissQR codetext.");
40+
return;
41+
}
42+
43+
// Access required properties
44+
string creditorName = decoded.Bill.Creditor?.Name ?? "N/A";
45+
string iban = decoded.Bill.Account ?? "N/A";
46+
decimal amount = decoded.Bill.Amount;
47+
string currency = decoded.Bill.Currency ?? "N/A";
48+
49+
// Output the values
50+
Console.WriteLine($"Creditor Name: {creditorName}");
51+
Console.WriteLine($"IBAN: {iban}");
52+
Console.WriteLine($"Amount: {amount}");
53+
Console.WriteLine($"Currency: {currency}");
54+
}
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.BarCode.ComplexBarcode;
4+
using Aspose.BarCode.Generation;
5+
using Aspose.Drawing;
6+
using Aspose.Drawing.Imaging;
7+
8+
class Program
9+
{
10+
static void Main()
11+
{
12+
// Sample Swiss QR bill data (valid IBAN, amount, version)
13+
var swissCodetext = new SwissQRCodetext();
14+
swissCodetext.Bill.Account = "CH9300762011623852957";
15+
swissCodetext.Bill.Amount = 199.95m;
16+
swissCodetext.Bill.Version = SwissQRBill.QrBillStandardVersion.V2_0;
17+
swissCodetext.Bill.Creditor.CountryCode = "CH";
18+
swissCodetext.Bill.Creditor.Name = "John Doe";
19+
20+
// Configurations to test: module size (XDimension) and margin (padding)
21+
float[] moduleSizes = { 2f, 4f };
22+
float[] margins = { 0f, 5f, 10f };
23+
24+
foreach (float moduleSize in moduleSizes)
25+
{
26+
foreach (float margin in margins)
27+
{
28+
using (ComplexBarcodeGenerator generator = new ComplexBarcodeGenerator(swissCodetext))
29+
{
30+
// Set module size (XDimension) in points
31+
generator.Parameters.Barcode.XDimension.Point = moduleSize;
32+
33+
// Set uniform padding on all sides
34+
generator.Parameters.Barcode.Padding.Left.Point = margin;
35+
generator.Parameters.Barcode.Padding.Top.Point = margin;
36+
generator.Parameters.Barcode.Padding.Right.Point = margin;
37+
generator.Parameters.Barcode.Padding.Bottom.Point = margin;
38+
39+
// Save barcode to a memory stream in PNG format
40+
using (MemoryStream ms = new MemoryStream())
41+
{
42+
generator.Save(ms, BarCodeImageFormat.Png);
43+
long fileSize = ms.Length;
44+
45+
// Load the image to obtain dimensions
46+
ms.Position = 0;
47+
using (Bitmap bitmap = new Bitmap(ms))
48+
{
49+
int width = bitmap.Width;
50+
int height = bitmap.Height;
51+
52+
Console.WriteLine($"ModuleSize: {moduleSize}pt, Margin: {margin}pt => Width: {width}px, Height: {height}px, FileSize: {fileSize} bytes");
53+
}
54+
}
55+
}
56+
}
57+
}
58+
}
59+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using System;
2+
using System.IO;
3+
using System.Globalization;
4+
using Aspose.BarCode;
5+
using Aspose.BarCode.ComplexBarcode;
6+
using Aspose.BarCode.Generation;
7+
8+
class Program
9+
{
10+
static void Main()
11+
{
12+
// Define input CSV and output folder
13+
string inputCsv = "invoices.csv";
14+
string outputFolder = "output";
15+
16+
// Ensure output folder exists
17+
if (!Directory.Exists(outputFolder))
18+
{
19+
Directory.CreateDirectory(outputFolder);
20+
}
21+
22+
// Seed a sample CSV if it does not exist
23+
if (!File.Exists(inputCsv))
24+
{
25+
string[] sampleLines = new[]
26+
{
27+
"InvoiceNumber,Amount,Reference",
28+
"1001,199.95,RF1234567890",
29+
"1002,250.00,RF0987654321",
30+
"1003,75.50,RF1122334455",
31+
"1004,120.00,RF5566778899",
32+
"1005,99.99,RF6677889900"
33+
};
34+
File.WriteAllLines(inputCsv, sampleLines);
35+
}
36+
37+
// Read all lines from the CSV
38+
string[] lines = File.ReadAllLines(inputCsv);
39+
if (lines.Length <= 1)
40+
{
41+
Console.WriteLine("No invoice data found in the CSV file.");
42+
return;
43+
}
44+
45+
// Process each invoice (skip header)
46+
for (int i = 1; i < lines.Length; i++)
47+
{
48+
string line = lines[i];
49+
if (string.IsNullOrWhiteSpace(line))
50+
continue;
51+
52+
string[] parts = line.Split(',');
53+
if (parts.Length < 3)
54+
continue; // Invalid line, skip
55+
56+
string invoiceNumber = parts[0].Trim();
57+
if (!decimal.TryParse(parts[1].Trim(), NumberStyles.Any, CultureInfo.InvariantCulture, out decimal amount))
58+
continue; // Invalid amount, skip
59+
60+
string reference = parts[2].Trim();
61+
62+
// Create Swiss QR code text and populate bill data
63+
var swissQr = new SwissQRCodetext();
64+
swissQr.Bill.Account = "CH9300762011623852957"; // known valid IBAN
65+
swissQr.Bill.Amount = amount;
66+
swissQr.Bill.Currency = "CHF";
67+
swissQr.Bill.Version = SwissQRBill.QrBillStandardVersion.V2_0;
68+
swissQr.Bill.Reference = reference;
69+
swissQr.Bill.BillInformation = $"Invoice {invoiceNumber}";
70+
swissQr.Bill.Creditor = new Address();
71+
swissQr.Bill.Creditor.CountryCode = "CH";
72+
73+
// Generate and save the QR code image
74+
using (var generator = new ComplexBarcodeGenerator(swissQr))
75+
{
76+
string outputPath = Path.Combine(outputFolder, $"Invoice_{invoiceNumber}.png");
77+
generator.Save(outputPath, BarCodeImageFormat.Png);
78+
}
79+
}
80+
81+
Console.WriteLine("Swiss QR code generation completed.");
82+
}
83+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using System;
2+
using System.IO;
3+
using System.Collections.Generic;
4+
using System.Text.Json;
5+
using Aspose.BarCode.Generation;
6+
using Aspose.BarCode.BarCodeRecognition;
7+
8+
class Program
9+
{
10+
// Simple model for JSON output
11+
private class PaymentInfo
12+
{
13+
public string FileName { get; set; }
14+
public string CodeText { get; set; }
15+
}
16+
17+
static void Main(string[] args)
18+
{
19+
// Determine input folder (argument or default)
20+
string inputFolder = args.Length > 0 ? args[0] : "InputQRCodes";
21+
// Ensure the folder exists
22+
if (!Directory.Exists(inputFolder))
23+
{
24+
Directory.CreateDirectory(inputFolder);
25+
// Seed a sample QR code so the example can run end‑to‑end
26+
string samplePath = Path.Combine(inputFolder, "sample.png");
27+
using (var generator = new BarcodeGenerator(EncodeTypes.QR, "PAYMENT:12345"))
28+
{
29+
// Set a moderate error correction level
30+
generator.Parameters.Barcode.QR.ErrorLevel = QRErrorLevel.LevelM;
31+
generator.Save(samplePath);
32+
}
33+
}
34+
35+
// Prepare list to hold extracted payment details
36+
var payments = new List<PaymentInfo>();
37+
38+
// Get image files (png and jpg) from the folder
39+
string[] files = Directory.GetFiles(inputFolder, "*.*", SearchOption.TopDirectoryOnly);
40+
foreach (string filePath in files)
41+
{
42+
// Simple filter for common image extensions
43+
string ext = Path.GetExtension(filePath).ToLowerInvariant();
44+
if (ext != ".png" && ext != ".jpg" && ext != ".jpeg" && ext != ".bmp" && ext != ".gif")
45+
continue;
46+
47+
// Verify the file still exists
48+
if (!File.Exists(filePath))
49+
continue;
50+
51+
try
52+
{
53+
// Use BarCodeReader to decode QR codes
54+
using (var reader = new BarCodeReader(filePath, DecodeType.QR))
55+
{
56+
// Read all barcodes in the image
57+
foreach (var result in reader.ReadBarCodes())
58+
{
59+
payments.Add(new PaymentInfo
60+
{
61+
FileName = Path.GetFileName(filePath),
62+
CodeText = result.CodeText
63+
});
64+
}
65+
}
66+
}
67+
catch (Exception ex)
68+
{
69+
// If a file cannot be processed, write a warning to console and continue
70+
Console.WriteLine($"Warning: Could not process '{filePath}'. {ex.Message}");
71+
}
72+
}
73+
74+
// Serialize the collected payment info to JSON
75+
string jsonOutput = JsonSerializer.Serialize(payments, new JsonSerializerOptions { WriteIndented = true });
76+
77+
// Write JSON to output file
78+
string outputPath = Path.Combine(Directory.GetCurrentDirectory(), "payment_details.json");
79+
File.WriteAllText(outputPath, jsonOutput);
80+
81+
// Inform the user where the result is stored
82+
Console.WriteLine($"Payment details written to: {outputPath}");
83+
}
84+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.BarCode.ComplexBarcode;
4+
using Aspose.BarCode.Generation;
5+
using Aspose.Drawing;
6+
7+
class Program
8+
{
9+
static void Main()
10+
{
11+
var swissQRCodetext = new SwissQRCodetext();
12+
13+
// Required creditor information
14+
swissQRCodetext.Bill.Creditor.Name = "John Doe";
15+
swissQRCodetext.Bill.Creditor.Street = "Main Street 1";
16+
swissQRCodetext.Bill.Creditor.PostalCode = "8000";
17+
swissQRCodetext.Bill.Creditor.Town = "Zurich";
18+
swissQRCodetext.Bill.Creditor.CountryCode = "CH";
19+
20+
// Required account information
21+
swissQRCodetext.Bill.Account = "CH9300762011623852957";
22+
23+
// Optional fields
24+
swissQRCodetext.Bill.Amount = 199.95m;
25+
swissQRCodetext.Bill.Version = SwissQRBill.QrBillStandardVersion.V2_0;
26+
27+
using (ComplexBarcodeGenerator generator = new ComplexBarcodeGenerator(swissQRCodetext))
28+
{
29+
generator.Parameters.Barcode.BarColor = Aspose.Drawing.Color.Blue;
30+
generator.Parameters.BackColor = Aspose.Drawing.Color.Yellow;
31+
32+
string outputPath = Path.Combine(Directory.GetCurrentDirectory(), "SwissQR.png");
33+
generator.Save(outputPath);
34+
}
35+
36+
Console.WriteLine("Barcode image generated successfully.");
37+
}
38+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.IO;
3+
using System.Xml.Linq;
4+
using Aspose.BarCode.ComplexBarcode;
5+
using Aspose.BarCode.Generation;
6+
7+
class Program
8+
{
9+
static void Main()
10+
{
11+
// Step 1: Create a sample SwissQR codetext object and fill required fields.
12+
var originalCodetext = new SwissQRCodetext();
13+
originalCodetext.Bill.Account = "CH9300762011623852957";
14+
originalCodetext.Bill.Creditor.CountryCode = "CH";
15+
originalCodetext.Bill.Creditor.Name = "John Doe";
16+
originalCodetext.Bill.Creditor.Street = "Main Street 1";
17+
originalCodetext.Bill.Creditor.PostalCode = "8000";
18+
originalCodetext.Bill.Creditor.Town = "Zurich";
19+
originalCodetext.Bill.Amount = 199.95m;
20+
originalCodetext.Bill.Version = SwissQRBill.QrBillStandardVersion.V2_0;
21+
22+
// Construct the encoded string from the object.
23+
string constructed = originalCodetext.GetConstructedCodetext();
24+
25+
// Step 2: Serialize the constructed codetext into a simple XML string.
26+
string xmlContent = $"<SwissQR><CodeText>{constructed}</CodeText></SwissQR>";
27+
28+
// Step 3: Parse the XML and extract the codetext.
29+
XDocument doc = XDocument.Parse(xmlContent);
30+
string extractedCodeText = doc.Root.Element("CodeText")?.Value;
31+
if (string.IsNullOrEmpty(extractedCodeText))
32+
{
33+
Console.WriteLine("Failed to extract codetext from XML.");
34+
return;
35+
}
36+
37+
// Step 4: Initialize a new SwissQRCodetext object from the extracted string.
38+
var deserializedCodetext = new SwissQRCodetext();
39+
deserializedCodetext.InitFromString(extractedCodeText);
40+
41+
// Step 5: Generate the Swiss QR barcode image using ComplexBarcodeGenerator.
42+
using (ComplexBarcodeGenerator generator = new ComplexBarcodeGenerator(deserializedCodetext))
43+
{
44+
string outputPath = "SwissQR.png";
45+
generator.Save(outputPath);
46+
Console.WriteLine($"Swiss QR barcode saved to '{Path.GetFullPath(outputPath)}'.");
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)