How to run the Ricoh Theta preview in c#?

206 Views Asked by At

I'm working on an application requiering the Ricoh Theta live preview. The doc of Ricoh Theta says nothing. I found a request to run it in javascript but nothing for c# so far.

First, I've send the http request with the camera._getLivePreview, which is working (at least I have no error) :

var url = requests("commands/execute");
        request = WebRequest.Create(url);
        request.ContentType = "application/json";
        request.Method = "POST";

        using (var streamWriter = new StreamWriter(request.GetRequestStream()))
        {
            string json = "{\"name\":\"camera._getLivePreview\"," +
                          "\"parameters\":{\"sessionId\":\"" + sid + "\"}}";

            streamWriter.Write(json);
            streamWriter.Flush();
            streamWriter.Close();
        }

Then I've tried to use a solution I've found here in order to print the image given in bytes in the response :

        var response = request.GetResponse().GetResponseStream();

        BinaryReader reader = new BinaryReader(new BufferedStream(response), new System.Text.ASCIIEncoding());

        List<byte> imageBytes = new List<byte>();
        bool isLoadStart = false; 
        byte oldByte = 0; 
        while (true)
        {
            byte byteData = reader.ReadByte();

            if (!isLoadStart)
            {
                if (oldByte == 0xFF)
                {
                    // First binary image
                    imageBytes.Add(0xFF);
                }
                if (byteData == 0xD8)
                {
                    // Second binary image
                    imageBytes.Add(0xD8);

                    // I took the head of the image up to the end binary
                    isLoadStart = true;
                }
            }
            else
            {
                // Put the image binaries into an array
                imageBytes.Add(byteData);

                // if the byte was the end byte
                // 0xFF -> 0xD9 case、end byte
                if (oldByte == 0xFF && byteData == 0xD9)
                {
                    // As this is the end byte
                    // we'll generate the image from the data and can create the texture
                    // imageBytes are used to reflect the texture
                    // imageBytes are left empty
                    // the loop returns the binary image head
                    isLoadStart = false;
                }
            }
          oldByte = byteData;
          byteArrayToImage(imageBytes.ToArray());
        }

    }

Since it was not printing anything, I tried to make a method sending the imageurl to an image src (using code found here) :

public void byteArrayToImage(byte[] byteArrayIn)
        {
            string imreBase64Data = Convert.ToBase64String(byteArrayIn);
            string imgDataURL = string.Format("data:image/png;base64,{0}", imreBase64Data);
            imgPreview.Source = imgDataURL;
        }

The XAML of the Image :

<Image Source="" 
               x:Name="imgPreview"/>

How to make it work ?

Edit 1 : Basically, I think the real question here is "how to print an image from a byte array ?"

Edit 2 : I found the layout is updated only after the first called method ends, and it would be easier to display the stream with the url though packages

1

There are 1 best solutions below

1
On

The way I did this basically is an html, and every time the image is completed, it runs the script to update the image.
Still it can be sluggish, but it's better than updating the image every time, because xamarin and .net maui need to clear and then repaint it.