static void Main(string[] args) { //List of files to scan List<string> files = new List<string>(); //Copy arguments into files (ignore this part) for(int i = 1; i < args.Length; i++){ if(args[i] == "--help" || args[i] == "-h") Console.WriteLine("Usage ./{0} [file1] [file2] [file3] ...", args[0]); else files.Add(args[i]); } if(files.Count == 0) files.Add("barcode.bmp"); //Create an instance of scanner using(ImageScanner scanner = new ImageScanner()){ //We won't use caching here scanner.Cache = false; //For each file that we need scanned foreach(string file in files){ Console.WriteLine("Symbols in {0}:", file); //Open the file, using System.Drawing to read it System.Drawing.Image img = System.Drawing.Image.FromFile(file); //Scan the image for symbols, using System.Drawing and ZBar for conversation //Please note that this is no way an efficient implementation, more optimizations //of the conversation code etc, could easily be implemented. List<Symbol> symbols = scanner.Scan(img); //For each symbol we've found foreach(Symbol symbol in symbols) Console.WriteLine("\t" + symbol.ToString()); } } }
static void Main(string[] args) { //List of files to scan List <string> files = new List <string>(); //Copy arguments into files (ignore this part) for (int i = 1; i < args.Length; i++) { if (args[i] == "--help" || args[i] == "-h") { Console.WriteLine("Usage ./{0} [file1] [file2] [file3] ...", args[0]); } else { files.Add(args[i]); } } if (files.Count == 0) { files.Add("barcode.bmp"); } //Create an instance of scanner using (var scanner = new ZBar.ImageScanner()){ //We won't use caching here scanner.Cache = false; //For each file that we need scanned foreach (string file in files) { Console.WriteLine("Symbols in {0}:", file); //Open the file, using System.Drawing to read it System.Drawing.Image img = System.Drawing.Image.FromFile(file); //Scan the image for symbols, using System.Drawing and ZBar for conversation //Please note that this is no way an efficient implementation, more optimizations //of the conversation code etc, could easily be implemented. List <Symbol> symbols = scanner.Scan(img); //For each symbol we've found foreach (Symbol symbol in symbols) { Console.WriteLine("\t" + symbol.ToString()); } } } }
static void Main(string[] args) { //Don't care to read the header now... it takes time :) //So I'm just hardcoding size... uint width = 1200; uint height = 775; byte[] data = new byte[width * height * 3]; //Read the file using(FileStream fs = new FileStream("barcode.bmp", FileMode.Open)) { //Skip the header fs.Seek(54, SeekOrigin.Begin); fs.Read(data, 0, data.Length); } //Create an empty image instance Image img = new Image(); img.Data = data; //Set the data property img.Width = width; //Set width and height img.Height = height; img.Format = 0x33424752; //Trying with RGB3, as I know it's 24bit BMP Image grey = img.Convert(0x30303859); //Convert to GREY/Y800 //Create a scanner using(ImageScanner scanner = new ImageScanner()) { scanner.Cache = false; scanner.Scan(grey); //Scan the image } Console.WriteLine("Symboles: "); //Now enumerate over the symboles found... These have been associated with grey foreach(Symbol sym in grey.Symbols) { Console.WriteLine(sym.ToString()); } Console.WriteLine("-End of program-"); Console.ReadLine(); }
/// <summary> /// Process video, the worker thread /// </summary> private void ProcessVideo() { using(Video video = new Video()){ try{ video.Open(this.currentDevice); video.Enabled = true; using(ImageScanner scanner = new ImageScanner()){ scanner.Cache = true; this.CaptureVideo(video, scanner); } video.Enabled = false; } catch(ZBarException ex){ lock(this.drawLock){ this.toDraw = null; this.symbols = null; } GLib.IdleHandler hdl = delegate(){ if(this.Stopped != null) this.Stopped(this, new EventArgs()); if(this.Error != null) this.Error(this, new ErrorEventArgs(ex.Message, ex)); this.QueueDraw(); return false; }; GLib.Idle.Add(hdl); } } }
public PreviewCallback () { scanner = new ImageScanner (); scanner.SetConfig (0, Config.XDensity, 3); scanner.SetConfig (0, Config.YDensity, 3); }
public void ConvertAndScanBMP() { System.Drawing.Image img = System.Drawing.Image.FromFile("images/barcode.bmp"); ImageScanner scanner = new ImageScanner(); List<Symbol> symbols = scanner.Scan(img); Assert.AreEqual(1, symbols.Count, "Didn't find the symbols"); Assert.IsTrue(SymbolType.EAN13 == symbols[0].Type, "Didn't get the barcode type right"); Assert.AreEqual("0123456789012", symbols[0].Data, "Didn't read the correct barcode"); }
public void ScanEan13_SymbolEnabled() { //var rawImage = System.IO.File.ReadAllBytes ("../ean13.png"); var bitmap = Android.Graphics.BitmapFactory.DecodeFile ("../ean13.png"); var image = new Image (bitmap.Width, bitmap.Height, "Y800"); image.SetData (bitmap.ToArray<byte>()); var scanner = new ImageScanner (); scanner.DisableSymbols (SymbolType.None); scanner.EnableSymbols (SymbolType.Ean13); var result = scanner.ScanImage (image); Assert.True (result > 0); var symbols = scanner.Results; foreach (var symbol in symbols) { Assert.AreEqual (symbol.Type, SymbolType.Ean13); Assert.AreEqual (symbol.Data, "2398000012344"); } }
/// <summary> /// Capture and scan images /// </summary> /// <remarks> /// This method will also flip the images so that they need not be flipped when drawing. /// </remarks> private void CaptureVideo(Video video, ImageScanner scanner) { while(true){ using(ZBar.Image frame = video.NextFrame()){ using(ZBar.Image bwImg = frame.Convert(0x30303859)){ //Scan the image for bar codes scanner.Scan(bwImg); //Get the data, width, height and symboles for the image byte[] data = bwImg.Data; int w = (int)bwImg.Width; int h = (int)bwImg.Height; var symbols = new List<Symbol> (bwImg.Symbols); // Resize // number of pixels to shift in the original image double stepX = (double)w / AllocatedWidth; double stepY = (double)h / AllocatedHeight; // don't do enlarging if (stepX <= 1 && stepY <= 1) return; double smallestStep = Math.Max (stepX, stepY); stepX = stepY = smallestStep; int maxHeight = (int)((double)h / smallestStep); int maxWidth = (int)((double)w / smallestStep); int newPixels = maxWidth * maxHeight; byte[] resizedFrame = new byte[newPixels]; double sX = 0, sY = 0; int i; for (int j = 0; j < newPixels; j++) { i = (int)sX + (int)((int)sY * w); // stop exceptions if (i >= data.Length) { Console.WriteLine ("Trying to access {0} on the old frame, up to {1} on new frame", i, j); break; } resizedFrame[j] = data[i]; sX += stepX; if ((j + 1) % maxWidth == 0) { sY += stepY; sX = 0; } } w = maxWidth; h = maxHeight; data = resizedFrame; //Flip the image vertically, if needed if(this.Flip){ for(int ih = 0; ih < h; ih++){ for(int iw = 0; iw < w / 2; iw++){ //TODO: The offsets below could be computed more efficiently, but I don't care this happens on a thread... :) int p1 = w * ih + iw; int p2 = w * ih + (w - iw- 1); //Swap bytes: byte b1 = data[p1]; data[p1] = data[p2]; data[p2] = b1; } } } if (Rotate) { int l = data.Length - 1; for (int p = 0; p < data.Length / 2; p++) { //Swap bytes: var j = l - p; byte b1 = data[j]; data[j] = data[p]; data[p] = b1; } } //Lock the drawing process and pass it the data we aquired lock(this.drawLock){ this.toDraw = data; this.toDrawWidth = w; this.toDrawHeight = h; this.symbols = symbols; } this.ThreadSafeRedraw(); } } } }