reading barcode using Aspose - aspose

I have a PDF file that includes a page with the Barcode. The barcode was originally created using DecodeType.Code128
I am using ASPOSE.PDF and Aspose.Barcode to find the barcode and the page number.
var barcodePageNumbers = new List<int>();
var currentPageNumber = 0;
using (PdfConverter converter = new PdfConverter())
{
converter.BindPdf(sourcefilePath);
converter.RenderingOptions.BarcodeOptimization = true;
converter.DoConvert();
while (converter.HasNextImage())
{
currentPageNumber++;
using (var ms = new MemoryStream())
{
converter.GetNextImage(ms);
ms.Seek(0L, SeekOrigin.Begin);
BarCodeReader reader = new BarCodeReader(ms, DecodeType.Code128);
while (reader.Read())
{
var text = reader.GetCodeText();
if (text == _barcodeTextToMatch)
{
barcodePageNumbers.Add(currentPageNumber);
}
}
}
}
}
Every now and then we get bad quality barcode pdf like the one attached herewith. (Note the attached PDF has 3 pages. I have created page 1 and 3 using tool. and the 2nd page which has barcode is from the original PDF.)
The code above works as long as the barcode quality is good. but if the quality is bad then it not able able to recognize its barcode.
Sample File
What other optimization technique can be used here?

At first, what the version of Aspose.PDF and Aspose.Barcode do you use? Because I tried the pdf file on the last Aspose.Barcode(develop version) it is recognized well.
string lFileName = #"d:\save\rec\merged.pdf";
var currentPageNumber = 0;
var barcodePageNumbers = new List<int>();
using (Aspose.Pdf.Facades.PdfConverter converter = new Aspose.Pdf.Facades.PdfConverter())
{
converter.BindPdf(lFileName);
converter.RenderingOptions.BarcodeOptimization = true;
converter.DoConvert();
while (converter.HasNextImage())
{
currentPageNumber++;
using (var ms = new MemoryStream())
{
converter.GetNextImage(ms);
ms.Seek(0L, SeekOrigin.Begin);
BarCodeReader reader = new BarCodeReader(ms, DecodeType.Code128);
while (reader.Read())
{
var text = reader.GetCodeText();
barcodePageNumbers.Add(currentPageNumber);
}
}
}
}
MessageBox.Show(string.Join(";", barcodePageNumbers.ToArray()));
Here are extracted images from pdf file:
page_2.tiff - full image
page2_cut.png - cut image.
Both are recognized well.
Image distortion, which you can see on the image, is salt and paper issue. We solve this problem in most cases in the recognition module, but current detection module of barcode position could mark barcodes with so strong salt and paper distortion as nonbarcode area. At this time we don’t have optimization for this, but we are developing SVM(support vector machines) detector which detects barcode areas better and also will have options to optimize recognition with these issues. SVM barcode areas detector will be added to Aspose.Barcode in 2020Q1.
Current workaround for this: if you know possible barcode area you can set barcode recognition region with SetBarCodeImage(Bitmap value, Rectangle area) and this helps current Gradient detector to detect so noisy areas.

Related

How can I improve the quality of output from Chart.js --> both pdf and png files are lower image quality

I was successful in taking Chart.js output and sending it to an image file (png) and to a pdf. The issue is that with both outputs the image quality is much lower than the chart in the browser. The png file is marginally acceptable, while the pdf is very grainy. Is there another way to produce output from Chart.js that has the same visual quality that it has in the browser?
Here's the way I output to png:
var a = document.createElement('a');
a.href = myChart.toBase64Image();
a.download = 'Site 1 - Users Per Department.png';
a.click();
And here's the way I output to pdf:
$('#create_PDF_btn').on('click', function() {
var canvas = document.querySelector("#myChart");
var canvas_img = canvas.toDataURL("image/png",1.0);
var pdf = new jsPDF('landscape','in', 'letter');
pdf.addImage(canvas_img, 'png', .5, 1.75, 10, 5);
pdf.save('Site 1 - Users Per Department.pdf')
});
One additional observation of the PDF is that this code produces a much larger file than I expected - 4,700 KB vs 21 KB (output to pdf from my current solution in MS Access). I can't add the pdf, so I'm including a copy of the image here. Notice the graininess in the text and legend points:
I'm using a relatively new version of Chart.js (3.4.1) and jsPDF (1.5.3).
Thanks for any input.

Disable hand written text detection

I am using Google.Cloud.Vision.V1 for OCR.
I need to disable the hand written text detection in method when I call the "DetectDocumentText" method in "ImageAnnotatorClient" class.
I only want to perform the OCR not ICR.
This is the code snippet I am using:
FileInfo fi = new FileInfo(imageFile);
var image = Image.FromFile(fi.FullName);
var client = ImageAnnotatorClient.Create();
var response = client.DetectDocumentText(image);

Sitecore - Read alt property from treelist of images

I am reading a treelist of images like this:
var images = new List<Image>();
MultilistField mlf = context.Item.Fields["Images"];
foreach (var id in mlf.TargetIDs)
{
var item = (MediaItem)Sitecore.Context.Database.GetItem(id);
images.Add(new Image
{
Url = MediaManager.GetMediaUrl(item),
Alt = item.Alt,
Extension = item.Extension,
});
}
The Url and Extension is correct, but I get no Alt text. I want to read the Alt text that is directly on the item in the media library and not in an ImageField.
Any suggestions is appreciated
Your code is correct.
Check if alt text is set on the proper language version of your media item. And check if your media items are published.
Remember that media items (as all the other items in Sitecore) may have versions, so check if the correct version is in your web database.

Sitecore How to get the value inside the field 'Media' of a Media Item

Update 2:
I am still fighting to get the Icon save on the server.
Here what I am doing:
Item item = Sitecore.Context.Database.GetItem(imageId);
var imageIconUrl = Sitecore.Resources.Images.GetThemedImageSource(item.Appearance.Icon, ImageDimension.id32x32);
if (!string.IsNullOrEmpty(imageIconUrl))
{
// download the icon from the url
var iconFullPath = "e:\\pngIcons\\excelIcon.png";
var webClient = new System.Net.WebClient();
var downloadPath = "http://serverName/" + imageIconUrl;
webClient.DownloadFile(downloadPath, iconFullPath);
}
The variable downloadPath contains following string:
http://serverName/sitecore/shell/themes/standard/~/media/E503BA48C89B422D8400393F1C7086A7.ashx?h=32&thn=1&w=32&db=master
At the end what I can see is a png file but there is nothing in it. I also copy the string I get in variable downloadPath and pasted it in browser and I can see the Icon as follow:
Please let me know what I am doing wrong. Or How I can save the Icon. Thanks!!
Original Question:
The sitecore media item has a field "Media". I am talking about this:
I want to access this field. And the reason is:
If I access it with e.g. item.GetMediaStream() then I will get the complete file. I just want to save this little icon some how on the server. Is it possible ?
To get the icon/thumbnail you can use
var icon = Sitecore.Resources.Media.MediaManager.GetThumbnailUrl(mediaItem);
To get the url of the thumbnail.
If you want the stream of the thumbnail you need to use the MediaData object. Like this:
var mediaItem = new MediaItem(item)
var mediaData = new MediaData(mediaItem);
var iconStream = mediaData.GetThumbnailStream();
if (iconStream.Length < 0)
{
// The stream is empty, its probably a file that Sitecore can't
// generate a thumbnail for. Just use the Icon
var icon = item.Appearance.Icon;
}
This will get the icon or thumbnail of the actual media blob that is attached to the media item. If you just want the icon of the Sitecore item, then use Martins method.
If the stream is empty, then Sitecore can't generate a thumbnail for it, so you can just use the icon file for the media item template. item.Appearance.Icon
Appearance section (of Standard Template) has a field called Icon - this is where you can modify icon for the item. There is also a field called Thumbnail - your excel icon is sitting there
You can access the field programmatically, just mind that it starts with two underscores: __Icon:
var iconField = item.Fields["__Icon"];
var thumbnailField = item.Fields["__Thumbnail"];
Update: As you asked, below is the code that saves either of fields into the file on a drive. I have tested the code and confirm successfully stores icon from thumbnail field into the file:
string mediaItemPath = "/sitecore/media library/Files/ExcelFile";
string mediaFiedlName = "Thumbnail"; // also can be "__Icon"
var item = Sitecore.Context.Database.GetItem(mediaItemPath);
var iconField = item.Fields[mediaFiedlName];
if (iconField.HasBlobStream)
{
var thumb = (ImageField)iconField;
var bl = ((MediaItem)thumb.MediaItem).InnerItem.Fields["blob"];
Stream stream = bl.GetBlobStream();
byte[] buffer = new byte[8192];
using (FileStream fs = File.Create("D:\\you_file_name.ico")) // change your path
{
int length;
do
{
length = stream.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, length);
}
while (length > 0);
fs.Flush();
fs.Close();
}
}
Update 2: If that does not help then I would advise to look around the way how that icon gets generated on Media field in Sitecore. In the worst case you may do the following - right click that icon and see its url. You will have something similar to what I assign to variable below:
string url = "http://test81/sitecore/shell/Applications/-/media/B4F61650CBE84EE396649602C0C48E1B.ashx?bc=White&db=master&h=128&la=en&mw=640&thn=1&vs=1&ts=b8046903-ae57-4f9d-9dd5-b626ee5eee90";
Of course your URL should have your hostname and media prefix and the rest of parameters. Then use Webclient with that modified URL:
WebClient webClient = new WebClient();
webClient.DownloadFile(url, "e:\\you_file_name.ico");
Not ideal, but may work. Note, that the code above should work in a context of already logged user, so you need to authorise you Webclient prior that (many articles on S.O. how to do that).
Please reply if that approach has worked for you (I've spent decent time writing and testing that code in debugger, so would want to know whether that helped)

Export every frame as image from a Movie-File (QuickTime-API)

I want to open an existing Movie-File and export every frame of this file to an image like JPEG or TIFF. I got so far until now:
int main(int argc, char* argv[]) {
char filename[255]; // Filename to ping.
OSErr e; // Error return.
FSSpec filespec; // QT file specification
short filemovie; // QT movie handle.
Movie movie; // QT movie "object".
InitializeQTML(0);
EnterMovies();
// Because of QT's Mac origin, must convert C-string filename
// to Pascal counted string, then use that to make a filespec.
c2pstr(filename);
FSMakeFSSpec(0, 0L, (ConstStr255Param)filename, &filespec);
OpenMovieFile(&filespec, &filemovie, fsRdPerm);
NewMovieFromFile(&movie, filemovie, nil, nil, newMovieActive, nil);
...
Until now it works fine (I tested with TimeValue movietime = GetMovieDuration(movie); and print it), but now I want to get every frame of the movie and export it to a file (for first, later i just want to keep the data in memory to work with it, but i have to know if it really works, so export to an image-file is better for now).
How do I do that? Do I need a GWorld or a PixMap? How do I get a GWorld/PixMap from a Movie-File, especially each frame of it?
edit: My Platform is WinXP
As a beginning, this article on Movie exporters should pretty much get you started:
http://www.mactech.com/articles/mactech/Vol.16/16.05/May00QTToolkit/index.html
Even though MacTech is a Mac resource, all described API functions should be available in the QuickTime for Windows SDK as well.
I will slap some sample code together myself as a reference here as soon as I find the time.
Edit
See this book excerpt for additional info:
QuickTime Toolkit - Basic Movie Playback and Media Types # Google Books
Edit 2 - The High-Level Approach: Movie Exporters
If all you need to accomplish is to extract all video frames from a QuickTime Movie and convert them to another format supported by the QuickTime API you won't have to take any low-level actions whatsoever if using a Movie Exporter.
The below sample code allows to extract and convert all video frames from a QuickTime Movie to, f.e., a bunch of JPEG files using a programmatically invoked Movie Export Dialog.
Just select Movie to Image Sequence in the Export combo box of the dialog and select your desired image format by hitting Options.
Note 1: If you need to do this non-interactively, just let me know.
Note 2: error handling has been omitted for clarity
#include "Movies.h"
#include "QTML.h"
#pragma comment (lib, "QTMLClient.lib")
...
int flags = createMovieFileDeleteCurFile
| showUserSettingsDialog
| movieToFileOnlyExport;
ItemCount movie_prop_count = 0;
CFStringRef cfpath = 0;
Boolean bool_true = true;
QTNewMoviePropertyElement movie_props[ 2 ];
Movie movie;
// initialize QuickTime API
InitializeQTML( 0 );
EnterMovies();
// set up Core Foundation string for source path (argv[ 1 ]) contains the full path to the MOV file to convert
cfpath = CFStringCreateWithCString( 0, argv[ 1 ], kCFStringEncodingASCII );
movie_props[movie_prop_count].propClass = kQTPropertyClass_DataLocation;
movie_props[movie_prop_count].propID = kQTDataLocationPropertyID_CFStringNativePath;
movie_props[movie_prop_count].propValueSize = sizeof(cfpath);
movie_props[movie_prop_count].propValueAddress = (void*)&cfpath;
movie_props[movie_prop_count].propStatus = 0;
++movie_prop_count;
// make Movie active
movie_props[movie_prop_count].propClass = kQTPropertyClass_NewMovieProperty;
movie_props[movie_prop_count].propID = kQTNewMoviePropertyID_Active;
movie_props[movie_prop_count].propValueSize = sizeof(bool_true);
movie_props[movie_prop_count].propValueAddress = &bool_true;
movie_props[movie_prop_count].propStatus = 0;
++movie_prop_count;
// aquire Movie for our Movie file
NewMovieFromProperties( movie_prop_count, movie_props, 0, 0, &movie );
// invoke conversion dialog
ConvertMovieToFile( movie, 0, 0, 0, 'TVOD', 0, 0, flags, 0 );
// clean up
DisposeMovie( movie );
CFRelease( cfpath );
ExitMovies();
TerminateQTML();
...
If you are working on OS X only I would stick with the QTKit APIs because they are much higher level and generally easier to work with.
The general gist of what you want to do is:
Step Through each frame in a movie
Get a image representation for the current frame
Save the current frame's image to a file on disk
To step through the frames in a QuickTime movie, you can use the QTMovie class in QTKit to do this as follows:
- (void)dumpFramesWithMovie:(QTMovie*)movie toFolder:(NSString*)folderPath
{
[movie setIdling:NO]; // Don't idle the movie
[movie gotoEnd];
QTTime endTime = [movie currentTime];
[movie gotoBeginning];
// Number of frames counted so far
unsigned long numFrames = 0;
// Turn off the Movie's looping so we are quaranteed to stop
[movie setAttribute:[NSNumber numberWithBool:NO] forKey:QTMovieLoopsAttribute];
// Construct a Dictionary of to use when reading frames from a movie
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setObject:QTMovieFrameImageTypeNSImage forKey:QTMovieFrameImageType];
while (true)
{
QTTime curTime = [movie currentTime];
if (QTTimeCompare(curTime, endTime) == NSOrderedSame)
{
// Reached end of file
break;
}
// Get the Current Frame as an NSImage
NSImage* image = [movie frameImageAtTime:curTime
withAttributes:attributes error:nil];
// Get the bitmap representation of this image
// NOTE: This code assumes the first image representation is a NSBitmapImageRep, which is true
// by default in OS X 10.5 and later.
// Production code should do error checking here.
NSBitmapImageRep *bitmap = [[image representations] objectAtIndex: 0];
// Construct a filename based upon the folder path and number of frames read so far
NSString* fileName = [NSString stringWithFormat:#"%#/frame%d.png", folderPath, numFrames];
// Get an PNG representation of this file
NSData *data = [bitmap representationUsingType: NSPNGFileType
properties: nil];
// Write to disk
[data writeToFile: fileName
atomically: NO];
// Step to the next Frame
[movie stepForward];
numFrames++;
}
[movie gotoBeginning];
}
#end
This code compiles but has not been fully tested.
One caveat with this approach is that MPEG-1 files will not decode properly on OS X 10.5 and earlier. This has been fixed as of 10.6 as far as I know. Also, if are writing a Windows application, you'll need to use the lower lever Quicktime-C APIs.
Be sure to check the following reference pages while working on this:
QTKit Application Programming Guide
QTKit Frequently Asked Questions
QTMovie Class Reference
Quickies for NSImage
NSBitmapImageRep Reference
NSImage Reference
These command line OSX utilities are really good at extracting the contents of a Quicktime movie without having to buy QTPro:
qt_tools
I usually just dump the decoded frames into a binary one after another and then use some YUV-Viewer to see if they look Ok. For that you have to know the exact color format and resolution of the decoded frames. If you have RGB, there are certainly also viewers for that.
UPDATE:
Link doesn't seem to work. You will have to google for a YUV-Viewer. Alternatively, you can buy one from Elecard.