IPB

Üdvözöllek a Fórumban! ( Bejelentkezés | Regisztráció )

 
Reply to this topicStart new topic
Convert PNG to JPG without writing to disk
bining
hozzászólás Apr 19 2019, 05:41 PM
Létrehozva: #1


Advanced Member
***

Csoport: Members
Hozzászólások: 480
Csatlakozott: 8-September 18
Azonosító: 761



Goal
I'm creating a feature that takes a screen capture of the Unity game and uploads it to my web server using the web player. I've got this working just fine.

Issue
The problem is that the files being uploaded are unnecessarily large (300KB+). I could do some image manipulation in PHP, but I'd like to stay away from having my server doing all that work. .Net has some functionality to convert images to other formats, but as far as I've seen those methods require you to write the newly converted image to disk... which I can't have happen since I'm wanting to do this in the web player.

Solution Needed
I need Unity to convert the PNG image to JPG format without writing to disk.

Any ideas on how I can achieve this?
Go to the top of the page
 
+Quote Post
ftrsquish
hozzászólás Apr 19 2019, 05:42 PM
Létrehozva: #2


Advanced Member
***

Csoport: Members
Hozzászólások: 62
Csatlakozott: 28-August 18
Azonosító: 731



This should be fairly easy with .NET, but it looks like Unity's Mono environment is lacking the proper functionality.

This line:

Code (csharp):

var codecs:ImageCodecInfo[] = ImageCodecInfo.GetImageEncoders();

Produces:

Code (csharp):

DllNotFoundException: gdiplus.dll
System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders ()
System.Drawing.GDIPlus..cctor ()
UnityEditor.EditorGUIUtility:RenderGameViewCameras(Rect, Rect, Boolean, Boolean)
UnityEditor.EditorGUIUtility:RenderGameViewCameras(Rect, Rect, Boolean, Boolean)
UnityEditor.GameView:OnGUI()
System.Reflection.MonoMethod:InternalInvoke(Object, Object[])
System.Reflection.MonoMethod:InternalInvoke(Object, Object[])
System.Reflection.MonoMethod:Invoke(Object, BindingFlags, Binder, Object[], CultureInfo)
System.Reflection.MethodBase:Invoke(Object, Object[])
UnityEditor.HostView:Invoke(String)
UnityEditor.DockArea:OnGUI()
I suppose you could find a totally exposed C# implementation of JPEG compression, but with functionality already present in .NET I doubt there's one out there...
Go to the top of the page
 
+Quote Post
bining
hozzászólás Apr 19 2019, 05:43 PM
Létrehozva: #3


Advanced Member
***

Csoport: Members
Hozzászólások: 480
Csatlakozott: 8-September 18
Azonosító: 761



Ya, its turning out to be really inconvenient. Would it be worth me asking for a feature request? Or is this too obscure of a problem?

Matthew said:
I suppose you could find a totally exposed C# implementation of JPEG compression, but with functionality already present in .NET I doubt there's one out there...
I doubt that too. I'll keep looking for a bit, but I'm afraid that for the time being I'll just have to handle the compression with PHP.
Go to the top of the page
 
+Quote Post
ftrsquish
hozzászólás Apr 19 2019, 05:43 PM
Létrehozva: #4


Advanced Member
***

Csoport: Members
Hozzászólások: 62
Csatlakozott: 28-August 18
Azonosító: 731



IDÉZET(bining @ Apr 19 2019, 05:43 PM) *
Ya, its turning out to be really inconvenient. Would it be worth me asking for a feature request? Or is this too obscure of a problem?

Matthew said:
I suppose you could find a totally exposed C# implementation of JPEG compression, but with functionality already present in .NET I doubt there's one out there...
I doubt that too. I'll keep looking for a bit, but I'm afraid that for the time being I'll just have to handle the compression with PHP.

It's worth requesting, at least. It might be something simple for Unity to add, although my guess is it's a deliberate feature reduction in order to save file size on the web player...
Go to the top of the page
 
+Quote Post
fillerjupiter
hozzászólás Apr 19 2019, 05:45 PM
Létrehozva: #5


Advanced Member
***

Csoport: Members
Hozzászólások: 242
Csatlakozott: 31-October 18
Azonosító: 886



I've a mobile project where i should use a jpg encoder to lower traffic, when uploading/downloading images from the device.

Now I wanted to translate the js version of the script above to C#, but I fail with the bitoperations.
Maybe someone has a solution.
Go to the top of the page
 
+Quote Post
bining
hozzászólás Apr 19 2019, 05:46 PM
Létrehozva: #6


Advanced Member
***

Csoport: Members
Hozzászólások: 480
Csatlakozott: 8-September 18
Azonosító: 761



writeByte(value:byte) expects byte, and obviously throws an error.

where an int is used as byte, I suspect that I just need to convert the int values to bytes, and use them as bytes, but how is this done?

I have the impression that i need to use something like a bit converter, but thats some levels above my understanding sad.gif
So is my assumption to use a BitConverter right?
And if I'm right, what Im really not sure about, what is the mono thing for System.BitConverter? does it also exist or what do I have to use there?

Maybe someone whats to see the whole script, for this I attatched a Zip with the UnityProject from Matthew above, and made a Second JPGEncoder script in C# with its own namespace, to avoid Class name Problems.

Whats done until now is that I changed all variable and method declarations to C# syntax, and set their private/public state, if it wasn't done in js.
Go to the top of the page
 
+Quote Post
ambiguousscourge
hozzászólás Apr 19 2019, 05:54 PM
Létrehozva: #7


Advanced Member
***

Csoport: Members
Hozzászólások: 261
Csatlakozott: 28-October 18
Azonosító: 884



While technically, this is not really "the answer", I have "an answer" smile.gif

If you look around on this forum thread: https://onlineconvertfree.com/convert/png/ then you can find code for a public domain JPG library as well as pointers to some PNG decompression source code. You could likely cobble those together to do what you need if you can port all that to C#... Or if you have the Advanced version, you could do it in native Obj-C or C++ and do it that way?
Go to the top of the page
 
+Quote Post

Fast ReplyReply to this topicStart new topic
2 felhasználó olvassa jelenleg ezt a témát (2 vendég és 0 anonim felhasználó)
0 felhasználó:

 



Szöveges verzió A pontos idő: 15th June 2024 - 11:31 PM