Android Camera2 Image to Uhro2d Texture

FanvaronFanvaron USMember ✭✭

Hello, I'm trying to display a camera feed in my Android Urho Application.

I already managed to setup my Camera2 Class and now have an Android.Media.Image but I can not get that to display as a Texture2d.
My current (test) approach looks like this:

        public void OnImageAvailable(ImageReader reader)
        {
         // Image Format  is JPEG
            Image Loc_Image = reader.AcquireLatestImage();

            int Loc_Width = Loc_Image.Width;
            int Loc_Height = Loc_Image.Height;

        // Get as Byte Array
            Image.Plane[] planes = Loc_Image.GetPlanes();
            ByteBuffer buffer = planes[0].Buffer;
            buffer.Rewind();
            byte[] data = new byte[buffer.Capacity()];
            buffer.Get(data);

            if (Loc_Image != null)
                Loc_Image.Close();


            Urho.Application.InvokeOnMain(() =>
            {
                Cloc_Texture.SetNumLevels(1);
                Cloc_Texture.SetSize(Loc_Width, Loc_Height, Urho.Graphics.RGBAFormat, Urho.TextureUsage.Dynamic);
                Cloc_Texture.SetData(0, 0, 0, Loc_Width, Loc_Height, data);
            });

        }

My Problem is that I am not familiar with Image Processing and I'm not sure what format I need the Bytes to be in to be acceptable for SetData.
Right now as is the very first run just shows a few rows of random pixels and the second one just crashes the application without exception.

Like I said I'm using the Camera2Api and have set the ImageFormatType set to Jpeg.

I would appreciate any help or even hints on what I could possibly do to get this running.

EDIT:

I also tried turning it into a bitmap and then compressing into a jpeg like this

        Bitmap bitmap = BitmapFactory.DecodeByteArray(data, 0, data.Length);

                MemoryStream stream = new MemoryStream();
                bitmap.Compress(Bitmap.CompressFormat.Jpeg, 0, stream);
                byte[] bitmapData = stream.ToArray();

Which helped with the crashes but still only gives me a few random pixel each image.

Best Answer

  • FanvaronFanvaron US ✭✭
    Accepted Answer

    Ok, me looking at all my old attempts to list them for this Post actually brought me in the right direction and now I got it to work,
    I still have to see how good the performance is but I at least got a picture using Skia Bitmap like so:

    [...]
            Image.Plane[] planes = Loc_Image.GetPlanes();
                ByteBuffer buffer = planes[0].Buffer;
                buffer.Rewind();
                byte[] data = new byte[buffer.Capacity()];
                buffer.Get(data);
    
           var skbitmap = SkiaSharp.SKBitmap.Decode(data);
    [...]
    
    Cloc_Texture.SetData(0, 0, 0, Loc_Width, Loc_Height, skbitmap.Bytes);
    

Answers

  • FanvaronFanvaron USMember ✭✭
    Accepted Answer

    Ok, me looking at all my old attempts to list them for this Post actually brought me in the right direction and now I got it to work,
    I still have to see how good the performance is but I at least got a picture using Skia Bitmap like so:

    [...]
            Image.Plane[] planes = Loc_Image.GetPlanes();
                ByteBuffer buffer = planes[0].Buffer;
                buffer.Rewind();
                byte[] data = new byte[buffer.Capacity()];
                buffer.Get(data);
    
           var skbitmap = SkiaSharp.SKBitmap.Decode(data);
    [...]
    
    Cloc_Texture.SetData(0, 0, 0, Loc_Width, Loc_Height, skbitmap.Bytes);
    
Sign In or Register to comment.