This project has moved. For the latest updates, please go here.

IndexOutOfRangeException in BitMatrix.this

Mar 10, 2014 at 4:43 PM
Hello,

I'm getting this error repeatedly, using the latest download of ZXing. Here is the exception:
System.IndexOutOfRangeException occurred
  HResult=-2146233080
  Message=Index was outside the bounds of the array.
  Source=zxing
  StackTrace:
       at ZXing.Common.BitMatrix.get_Item(Int32 x, Int32 y) in C:\ZXing.Net.Build.0.12.0.0\Source\lib\common\BitMatrix.cs:line 118
  InnerException: 
The stack trace:
    zxing.dll!ZXing.Common.BitMatrix.this[int, int].get(int x = -2147483648, int y = -2147483648) Line 118 + 0x13 bytes C#
    zxing.dll!ZXing.Common.DefaultGridSampler.sampleGrid(ZXing.Common.BitMatrix image = {ZXing.Common.BitMatrix}, int dimensionX = 4, int dimensionY = 2, ZXing.Common.PerspectiveTransform transform = {ZXing.Common.PerspectiveTransform}) Line 64 + 0x66 bytes   C#
    zxing.dll!ZXing.Common.DefaultGridSampler.sampleGrid(ZXing.Common.BitMatrix image = {ZXing.Common.BitMatrix}, int dimensionX = 4, int dimensionY = 2, float p1ToX = 0.5, float p1ToY = 0.5, float p2ToX = 3.5, float p2ToY = 0.5, float p3ToX = 3.5, float p3ToY = 1.5, float p4ToX = 0.5, float p4ToY = 1.5, float p1FromX = 350.0, float p1FromY = 247.0, float p2FromX = 320.0, float p2FromY = 242.0, float p3FromX = 326.0, float p3FromY = 244.0, float p4FromX = 350.0, float p4FromY = 245.0) Line 32 + 0x17 bytes  C#
    zxing.dll!ZXing.Datamatrix.Internal.Detector.sampleGrid(ZXing.Common.BitMatrix image = {ZXing.Common.BitMatrix}, ZXing.ResultPoint topLeft = {ZXing.ResultPoint}, ZXing.ResultPoint bottomLeft = {ZXing.ResultPoint}, ZXing.ResultPoint bottomRight = {ZXing.ResultPoint}, ZXing.ResultPoint topRight = {ZXing.ResultPoint}, int dimensionX = 4, int dimensionY = 2) Line 383 + 0x18c bytes C#
    zxing.dll!ZXing.Datamatrix.Internal.Detector.detect() Line 202 + 0x39 bytes C#
    zxing.dll!ZXing.Datamatrix.DataMatrixReader.decode(ZXing.BinaryBitmap image = {ZXing.BinaryBitmap}, System.Collections.Generic.IDictionary<ZXing.DecodeHintType,object> hints = {ZXing.Common.DecodingOptions.ChangeNotifyDictionary<ZXing.DecodeHintType,object>}) Line 60 + 0x2f bytes    C#
    zxing.dll!ZXing.MultiFormatReader.decodeInternal(ZXing.BinaryBitmap image = {ZXing.BinaryBitmap}) Line 204 + 0x12 bytes C#
    zxing.dll!ZXing.MultiFormatReader.decodeWithState(ZXing.BinaryBitmap image = {ZXing.BinaryBitmap}) Line 89 + 0xb bytes  C#
    zxing.dll!ZXing.BarcodeReaderGeneric<System.Drawing.Bitmap>.Decode(ZXing.LuminanceSource luminanceSource = {ZXing.BitmapLuminanceSource}) Line 356 + 0xe bytes  C#
    zxing.dll!ZXing.BarcodeReaderGeneric<System.Drawing.Bitmap>.Decode(System.Drawing.Bitmap barcodeBitmap = {System.Drawing.Bitmap}) Line 319 + 0xe bytes  C#
>   Demo.exe!Demo.MainForm.OnImageCaptured(Touchless.Vision.Contracts.IFrameSource frameSource = {Touchless.Vision.Camera.CameraFrameSource}, Touchless.Vision.Contracts.Frame frame = {Touchless.Vision.Contracts.Frame}, double fps = 14.0) Line 111 + 0x17 bytes C#
    Touchless.Vision.dll!Touchless.Vision.Camera.CameraFrameSource.OnImageCaptured(object sender = {Touchless.Vision.Camera.Camera}, Touchless.Vision.Camera.CameraEventArgs e = {Touchless.Vision.Camera.CameraEventArgs}) Line 73 + 0x3c bytes    C#
    Touchless.Vision.dll!Touchless.Vision.Camera.Camera.ImageCaptured(System.Drawing.Bitmap bitmap = {System.Drawing.Bitmap}) Line 289 + 0x44 bytes C#
    Touchless.Vision.dll!Touchless.Vision.Camera.Camera.CaptureCallbackProc(int dataSize = 921600, byte[] data = {byte[921600]}) Line 251 + 0xb bytes   C#
This is my code:
    public void OnImageCaptured(Touchless.Vision.Contracts.IFrameSource frameSource, Touchless.Vision.Contracts.Frame frame, double fps)
    {
      _latestFrame = frame.Image;
      if (this.scanForQrCode)
      {
        var result = this.codeReader.Decode(_latestFrame);
        if (result != null)
        {
          this.scanForQrCode = false;
          this.btnStart.BackColor = Color.Red;
        }
      }
      pictureBoxDisplay.Invalidate();
    }
I've tried to clone the bitmap as well as lock the bitmap but this had no effect. The latest libraries crash the first time the code is called, but the Nuget package seems to crash at random times. If anyone could shed some light on a solution, I would greatly appreciate it.
Mar 10, 2014 at 5:39 PM
Digging into the source a bit, I found that the points array created in DefaultGridSampler sampleGrid has Inifinity in it at indices 2 and 3, apparently created by PerspectiveTransform.transformPoints:

      public void transformPoints(float[] points)
      {
         int max = points.Length;
         float a11 = this.a11;
         float a12 = this.a12;
         float a13 = this.a13;
         float a21 = this.a21;
         float a22 = this.a22;
         float a23 = this.a23;
         float a31 = this.a31;
         float a32 = this.a32;
         float a33 = this.a33;
         for (int i = 0; i < max; i += 2)
         {
            float x = points[i];
            float y = points[i + 1];
            float denominator = a13 * x + a23 * y + a33;
            points[i] = (a11 * x + a21 * y + a31) / denominator;
            points[i + 1] = (a12 * x + a22 * y + a32) / denominator;
         }
      }
The denominator is zero; dividing by zero results in the infinity. What do the parameters in this function mean and why would denominator == 0?
Coordinator
Mar 11, 2014 at 9:26 PM
Do you have a sample image for me?