Below is
- code that generates barcode
- code that reads barcode
Keep in mind, I can recognize and read barcode text fine, only thing I can't do consistently is to determine whether the barcode has been rotated 180 degrees.
1.
// Instantiate barcode object
BarCodeBuilder barcode = new BarCodeBuilder();
//set barcode image size
barcode.AutoSize = false;
barcode.ImageHeight = 5;
barcode.ImageWidth = 100;
//Set the Code text for the barcode
barcode.CodeText = patientCaseId + "-" + patientId + "-" + formSerial.ToString() + "-" + currPage;
//Set the symbology type to Code39Standard
barcode.SymbologyType = Symbology.Code39Standard;
barcode.xDimension = 0.3f;
MemoryStream barcodeStream = new MemoryStream();
//barcode.Save(barcodeStream, BarCodeImageFormat.Bmp);
barcode.Save(barcodeStream, ImageFormat.Bmp);
byte[] barcodeBuffer = barcodeStream.GetBuffer();
builder.InsertImage(barcodeBuffer);
builder.Writeln();
2.)
using (var ms = new MemoryStream())
{
BarCodeReader reader;
uploadedFile.InputStream.CopyTo(ms);
//check if file is pdf
if (uploadedFile.ContentType == "application/pdf")
{
// create PDF document to obtain single pages
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(uploadedFile.FileName);
// for each single PDF page, obtain barcode and other information
for (var i = 1; i <= pdfDocument.Pages.Count; i++)
{
Aspose.Pdf.Facades.PdfExtractor pdfExtractor = new Aspose.Pdf.Facades.PdfExtractor();
pdfExtractor.BindPdf(ms);
//flag to determine if page needs to be rotated 180 degrees
needToRotate180 = false;
// set page range for image extraction
pdfExtractor.StartPage = i;
pdfExtractor.EndPage = i;
// extract the images
pdfExtractor.ExtractImage();
// save image to stream
MemoryStream imageStream = new MemoryStream();
bool imageResult = pdfExtractor.GetNextImage(imageStream);
imageStream.Position = 0;
//check if there was an image
if (imageResult)
reader = new BarCodeReader(imageStream, BarCodeReadType.Code39Standard);
string code = "";
while (imageResult && reader.Read())
{
float pageAngle = reader.GetAngle();
code = reader.GetCodeText();
if (pageAngle == 180.0)
{
needToRotate180 = true;
}
}
reader.Close();
.......