Генерация и распознавание штрих-кодов в AWS Lambda
Категория: Штрих-коды; .NET
22 мая 2020






// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: Amazon.Lambda.Core.LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
namespace AWSLambda1
{
public class Function
{
/// <summary>
/// Default constructor. This constructor is used by Lambda to construct the instance. When invoked in a Lambda environment
/// the AWS credentials will come from the IAM role associated with the function and the AWS region will be set to the
/// region the Lambda function is executed in.
/// </summary>
public Function()
{
}
/// <summary>
/// This method is called for every Lambda invocation. This method takes in an SQS event object and can be used
/// to respond to SQS messages.
/// </summary>
public async System.Threading.Tasks.Task FunctionHandler(Amazon.Lambda.SQSEvents.SQSEvent evnt, Amazon.Lambda.Core.ILambdaContext context)
{
foreach (var message in evnt.Records)
{
await ProcessMessageAsync(message, context);
}
}
private async System.Threading.Tasks.Task ProcessMessageAsync(Amazon.Lambda.SQSEvents.SQSEvent.SQSMessage message, Amazon.Lambda.Core.ILambdaContext context)
{
context.Logger.LogLine($"Processed message {message.Body}");
try
{
// создать поток в котором будет хранится созданное изображение штрих-кода
using (System.IO.MemoryStream mem = new System.IO.MemoryStream())
{
// создать генератор штрих-кода
Vintasoft.Barcode.BarcodeWriter barcodeWriter = new Vintasoft.Barcode.BarcodeWriter();
// указать, что генератор должен создать штрих-код DataMatrix
barcodeWriter.Settings.Barcode = Vintasoft.Barcode.BarcodeType.DataMatrix;
// указать значение создаваемого штрих-кода
barcodeWriter.Settings.Value = "1234567890987654321";
// сгенерировать изображение штрих-кода и сохранить его в потоке как PNG изображение
barcodeWriter.SaveBarcodeAsImage(mem, Vintasoft.Barcode.BarcodeImageFormat.Png);
// создать распознаватель штрих-кодов
using (Vintasoft.Barcode.BarcodeReader barcodeReader = new Vintasoft.Barcode.BarcodeReader())
{
// указать, что распознаватель должен искать штрих-коды DataMatrix
barcodeReader.Settings.ScanBarcodeTypes = Vintasoft.Barcode.BarcodeType.DataMatrix;
// указать, что распознаватель должен искать только горизонтальные штрих-коды
barcodeReader.Settings.ScanDirection = Vintasoft.Barcode.ScanDirection.Horizontal;
// распознать штрих-коды в изображении
Vintasoft.Barcode.IBarcodeInfo[] infos = barcodeReader.ReadBarcodes(mem);
// если штрих-коды не найдены
if (infos.Length == 0)
{
context.Logger.LogLine($"No barcodes found.");
}
// если штрих-коды найдены
else
{
// get information about searched barcodes
for (int i = 0; i < infos.Length; i++)
{
Vintasoft.Barcode.IBarcodeInfo info = infos[i];
context.Logger.LogLine(string.Format("Barcode: Type={0}, Value={1}", info.BarcodeType, info.Value));
}
}
}
}
}
catch (System.Exception ex)
{
context.Logger.LogLine(string.Format("Error: {0}", ex.Message));
}
await System.Threading.Tasks.Task.CompletedTask;
}
}
}


