public void OnPreviewFrame (byte[] data, Camera camera) { var parameters = camera.GetParameters (); var size = parameters.PreviewSize; var barcode = new Image (size.Width, size.Height, "Y800"); barcode.SetData (data); var result = scanner.ScanImage (barcode); if (result == 0) return; camera.SetPreviewCallback (null); camera.StopPreview (); var scannerResult = GetScannerResult (); ScanComplete?.Invoke (this, new ScanCompleteEventArgs (scannerResult)); }
/// <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(); } } } }
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"); } }