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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Save images as jpeg on a background thread

Discussion in 'Scripting' started by david_unity440, Mar 12, 2019.

  1. david_unity440

    david_unity440

    Joined:
    May 31, 2018
    Posts:
    4
    We want to save images as jpeg on a background thread. The method that unity sdk exposes cannot be run on a different thread. Do you know any other plugins/ open source projects of saving images as jpeg that we can use? (we found quite a few libraries that do not run on android)
     
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,078
  3. david_unity440

    david_unity440

    Joined:
    May 31, 2018
    Posts:
    4
    Thanks. We did try it. It throws errors in the latest unity version and after some work it eventually encodes a cropped image :(
     
    Last edited: Mar 12, 2019
  4. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,078
    Ah, ok - it's been ages since I used it. If I get any free time I'll have a look at updating it.
     
  5. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,078
    p.s. I just loaded that github project into Unity 2018.2.21f1 and it seems to work fine for me (using the Test Encode scene). I didn't see any cropping and it worked threaded too. Are you testing the project on a PC? I also didn't spot any errors when it converted to 2018 from Unity 5.

    If you want to post a cut down version of your project I can have a look at it and see if I spot any problems.
     
  6. AndreasBroager

    AndreasBroager

    Joined:
    Jun 7, 2013
    Posts:
    5
    To be clear. I ported it from from unity javascript :) (as an exercise)
    The repo is here: https://bitbucket.org/AndreasBL/unity-jpeg-encoder/src/master/ (With credits and etc)

    But I don't do any active dev on it. Even though it is tempting, you know how it feels to look at your own old code ;)
    Anyway, unity has its own jpg encoder now.
     
  7. ArnoBen

    ArnoBen

    Joined:
    Apr 24, 2019
    Posts:
    13

    It has its own jpg encoder but it's on the main thread, yours is multithreaded which is like a godsend, but indeed it throws errors at me too in JPGEncoder.cs.
    Mainly :
    IndexOutOfRangeException line 653 :
    for (; (DU[i]==0) && (i<=end0pos); i++) {}

    and
    NullRefException Line 880:
    mainBuffer.WriteBuffer( kvp.Value.buffer.GetAllBytes() );    

    After some tests I noticed that sometimes the Value is null.

    Tested on 2020.1.7f1.