Search code examples
c#arrays.netunity-game-enginezxing.net

Mirroring in image vertical from a byte array


I want to create a DataMatrix decoder in C#. I'm using the ZXing.NET libary for this. I already coded a QR decoder with ZXing, but somehow for the DataMatrix decoder i need to mirror the picture to get it successful decoded.

So i have in image (128x128) its data is stored in a byte 1d array. The bytes in the array are representing the color of each pixel, so byte[0] would be the color of the pixel (0/0).

Now i want to mirror the picture and save the mirrored picture in another byte array

Can somebody tell me how to do this?


Solution

  • I guess you're looking for something like this:

    Mirror vertically:

    byte[] MirrorY(int size, byte[] inputArray)
    {
        byte[] reversedArray = new byte[inputArray.Length];
    
        for (int i = 0; i < inputArray.Length/size; i++){ 
            System.Array.Copy(inputArray, reversedArray.Length - (i+ 1) * size, reversedArray, i* size, size);
        }
    
        return reversedArray;
    }
    

    Mirror horizontally:

    byte[] MirrorX(int size, byte[] inputArray)
    {
        byte[] reversedArray = new byte[inputArray.Length];
    
        for (int i = 0; i < inputArray.Length/size; i++){ 
            for (int j = 0; j < size; j++){ 
                reversedArray [i * size + j] = inputArray [(i + 1) * size - j - 1];
            }
        }
    
        return reversedArray;
    }