Byte Array to UIImage returns Null

1

Hello I'm trying to get the pixel value from an image, convert the image to grayscale and finaly to return a UIImage from byte array with NSData. But it returns null

    partial void Analyze_button_TouchUpInside(UIButton sender)
    {

        int Rows = 324; //Width
        int Cols = 182; //Height
        var ImageSize = Rows * Cols; 

        AimageBytes = new byte[ImageSize];
        RimageBytes = new byte[ImageSize];
        GimageBytes = new byte[ImageSize];
        BimageBytes = new byte[ImageSize];
        GreyImageBytes = new byte[ImageSize];

        var pixels = GetPixels(img);

        for (ind = 0; ind < ImageSize; ind++)
        {
            pval = pixels[ind];
            AimageBytes[ind] = (byte)((pval & 0xFF000000) >> 24);
            RimageBytes[ind] = (byte)((pval & 0x00FF0000) >> 16);
            GimageBytes[ind] = (byte)((pval & 0x0000FF00) >> 8);
            BimageBytes[ind] = (byte)(pval & 0x000000FF);
            GreyImageBytes[ind] = (byte)((RimageBytes[ind] + GimageBytes[ind] + BimageBytes[ind]) / 3);

        }

        var dataTest = NSData.FromArray(GreyImageBytes);
        var ImageTest = UIImage.LoadFromData(dataTest);
        PlantPhoto.Image = ImageTest;

   }

Method used to get the pixels:

    public static int[] GetPixels(UIImage image)
    {

        const CGBitmapFlags flags = CGBitmapFlags.ByteOrder32Big | CGBitmapFlags.PremultipliedLast;

        var width = (int)image.CGImage.Width;
        var height = (int)image.CGImage.Height;
        var bytesPerPixel = image.CGImage.BitsPerPixel;
        var bytesPerRow = bytesPerPixel * width;
        var bitsPerComponent = image.CGImage.BitsPerComponent;
        var buffer = new byte[bytesPerRow * height];
        var pixels = new int[width * height];

        var handle = GCHandle.Alloc(buffer);
        try
        {
            using (var colorSpace = CGColorSpace.CreateGenericRgb())
            using (var context = new CGBitmapContext(buffer, width, height, bitsPerComponent, bytesPerRow, colorSpace, flags))
                context.DrawImage(new RectangleF(0, 0, width, height), image.CGImage);

            for (var y = 0; y < height; y++)
            {
                var offset = y * width;
                for (var x = 0; x < width; x++)
                {
                    var idx = bytesPerPixel * (offset + x);
                    var r = buffer[idx + 0];
                    var g = buffer[idx + 1];
                    var b = buffer[idx + 2];
                    var a = buffer[idx + 3];
                    pixels[x * y] = a << 24 | r << 16 | g << 8 | b << 0;
                }
            }
        }
        finally
        {
            handle.Free();
        }

        return pixels;
    }
c#
xamarin.ios
asked on Stack Overflow Apr 14, 2019 by Kostasmel • edited Apr 14, 2019 by Bùi Đức Khánh

1 Answer

0

I'm trying to figure out if the data format is different from the format that match the uiimage data parameters. Unfortunately i have to use that method i've posted. Tried to convert an image to byte array and back and it works.

NSData imageDataTest = img.AsPNG();

                Byte[] testByteArray = new Byte[imageDataTest.Length];
                Marshal.Copy(imageDataTest.Bytes, testByteArray, 0, Convert.ToInt32(imageDataTest.Length));

UIImage backToImage = UIImage.LoadFromData(imageDataTest);

It seems that both testByteArray and GreyImageBytes have the same structure into byte Array.

answered on Stack Overflow May 4, 2019 by Kostasmel

User contributions licensed under CC BY-SA 3.0