private Bitmap PrepImage(Bitmap image) { float useWidthPercent = (float)nud_width.Value % 101; Int32 useWidth = (Int32)((float)image.Width * (useWidthPercent / 100)); return(Bitmapper.Crop(image, (image.Width - useWidth) / 2, (image.Height / 2) - (Barcode.rowsToAverage / 2), useWidth, Barcode.rowsToAverage)); }
/// <summary> /// Attempts to create a barcode out of an image /// </summary> /// <param name="Image">Image to read</param> /// <exception cref="BarcodeException">No valid barcode found in image.</exception> public Barcode(Bitmap image) { if (image.Height < Barcode.rowsToAverage) { throw new BarcodeException("Image to short."); } DecodeImage(image); this.originalImage = (Bitmap)Bitmapper.Copy(image); }
/// <summary> /// Requests and attempts to read an image for a barcode /// </summary> private void imgCap() { if (!run) { return; } // Do allow multiple reads at the same time from timer interrupts if (mu.WaitOne(1)) { try { Bitmap curBarCode = (Bitmap)cam.AcquireImage(); pb_barcode.Image = Bitmapper.Resize(curBarCode, pb_barcode.Width, pb_barcode.Height); curBarCode = PrepImage(curBarCode); Bitmap temp = new Bitmap(curBarCode); Bitmapper.ThresholdImage(temp, Barcode.threshold); temp = (Bitmap)Bitmapper.Resize(temp, pb_barcode.Width, temp.Height); pb_threshold.Image = temp; if (curBarCode != null) { Barcode code = new Barcode(curBarCode); if (code.uncertainty > (Int32)nud_uncertainty.Value) { throw new BarcodeException("Uncertainty to high:" + code.uncertainty); } DisplayBarcode(code.barcode); lastBarcode = code; lb_uncertainty.Text = code.uncertainty.ToString(); stop(); } } catch (Exception ex) { tb_errors.Text = ex.Message; } finally { mu.ReleaseMutex(); } } }
/// <summary> /// Attempts to read a barcode out of an image /// </summary> /// <param name="Image">Image to read</param> /// <returns>barcode</returns> private void DecodeImage(Bitmap Image) { this.barcode = ""; Bitmapper.ThresholdImage(Image, threshold); Double[] lineWidth = new Double[59]; Int32 startRow = (Image.Height / 2) - (rowsToAverage / 2); for (int i = 0; i < rowsToAverage; ++i) { // Read in a single line Int32[] row = ReadLine(Image, startRow + i); Int32 offset = -1; Int32 currentColor = row[0]; Int32 rowWidth = 0; for (int j = 0; j < row.Length; ++j) { if (row[j] == currentColor) { ++rowWidth; continue; } else { if (offset == -1) { ++offset; currentColor = row[j]; rowWidth = 1; } else { currentColor = row[j]; lineWidth[offset] += rowWidth; rowWidth = 1; ++offset; if (offset == lineWidth.Length) { break; } } } } } //Average out reads for (Int32 i = 0; i < lineWidth.Length; ++i) { lineWidth[i] /= rowsToAverage; } //scale line thicknesses Double thickness = (lineWidth[0] + lineWidth[1] + lineWidth[2]) / 3; for (Int32 i = 0; i < lineWidth.Length; ++i) { //This part is important gotta get scaling of the line thicknesses right. if (lineWidth[i] < thickness * 1.5) { lineWidth[i] = 1; } else if (lineWidth[i] < thickness * 2.5) { lineWidth[i] = 2; } else if (lineWidth[i] < thickness * 3.5) { lineWidth[i] = 3; } else { lineWidth[i] = 4; } } DecodeThicknesses(lineWidth); if (ValidateBarCode(barcode) == false) { throw new BarcodeException("Checksum fail: " + barcode); } }