Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How to Get raw byte array from ESP32 CAM (in compressed JPG) to Texture2D

Discussion in 'Editor & General Support' started by mattycorbett, Oct 5, 2023 at 1:03 AM.

  1. mattycorbett

    mattycorbett

    Joined:
    May 9, 2021
    Posts:
    12
    The question is really the title. I have a byte array of varying sizes, compressed on an ESP32-CAM and sent over a socket connection to my program. I am trying to create a Texture2D from this raw data. I can't seemingly use Texture2D.LoadRawTextureData as the compressed data is of varying sizes and it needs to be exactly the Texture2D buffer size. I also tried
    Code (CSharp):
    1. ImageConversion.EncodeArrayToJPG(myReadBuffer, rawVideoTexturesRGBA.graphicsFormat, 1024, 768);
    where 1024x768 is the resolution of the JPG, and rawVideoTexturesRGBA. is the Texture I am trying to fill with this data. This line of code crashes unity every time. Any ideas?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,283
    I'm confused... what is going on here:

    - do you have JPEG data arriving from this camera that you want to make a texture out of?

    - do you want to encode Texture2D data into JPEG for transmission?

    or

    - do you have some other format of data that only ESP32-CAM knows and want code to turn it into something that can be used more universally?
     
  3. mattycorbett

    mattycorbett

    Joined:
    May 9, 2021
    Posts:
    12
    Apologies for the confusion. The picture originates on an ESP32-CAM. Its encoded as a JPG and transmitted as a byte stream over a socket connection to my application (developed in Unity). I want to take this raw byte data and make a Texture2D, but since each transmission arrives as images of different sizes (I assume due to JPG compression) I can't just load the data into a Texture2D.

    In short, It's the first of your questions.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,283
  5. mattycorbett

    mattycorbett

    Joined:
    May 9, 2021
    Posts:
    12
    Thank you! The function seems to work, but the resultant texture is a red question mark. Any ideas?
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,283
    Then most likely it wasn't valid JPEG.

    Write it to a file on disk, rename it to JPEG and open it with Photoshop or something, see if it is valid.

    Additionally, go get some valid JPEG by creating a JPEG and feed that data in, watch it succeed.
     
  7. mattycorbett

    mattycorbett

    Joined:
    May 9, 2021
    Posts:
    12
    I've sent the data to a python testing script, and PIL created the images perfectly fine. I saw the documentation says the Texture2D needs to be sRGB is the input bytes are coded that way. I'll try that next

    Correction: PIL creates PNGs when asked to open the same raw bytes.
     
    Last edited: Oct 5, 2023 at 7:05 PM
  8. mattycorbett

    mattycorbett

    Joined:
    May 9, 2021
    Posts:
    12
    I figured it out. Thank you again for your help @Kurt-Dekker!
     
    Kurt-Dekker likes this.