Nerd For Tech
Published in

Nerd For Tech

Encoding/Decoding a Picture in Unity

Objective: Break a picture into an array of bytes and reassemble it.

In yesterday’s article (https://lordkakabel.medium.com/taking-a-photo-in-unity-e6e3f5328da6), we allowed the user to take a picture with their device and we stored the path to the photo within the device. Today, we’ll use that path, break the photo into a array of bytes, and reassemble it back to a photo that we can display within our app.

First, we’ll prepare an array of bytes. If we have a path to the user’s photo, we’ll store it as a Texture2D. Then we’ll EncodeToPNG to store it in the array of bytes. We’ll store this array in our ActiveCase object, which is being maintained by the current instance of our UIManager.

Here’s an example of what our encoded image would look like if we saved it on our system:

A small portion of our pic’s .dat file.

Now we have to reverse the process to recreate the photo. We’ll create an empty Texture2D to hold the result. Then we’ll get the PhotoTaken byte array from the ActiveCase object and use the LoadImage method to assign the data to the Texture2D. Finally, we’ll assign the Texture2D to texture property of our RawImage object.

It seems like a complicated process, but it only took a few lines of code!

--

--

--

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Recommended from Medium

Audio Conversion API: Convert File to M4A Format using Java

Hadoop Cluster on AWS

Tau ($AGRS) Development Update | January 2022 (Ep. 42)

Microsoft Teams + Consumer Skype, Almost

JVM 101: Introduction, ClassLoader Sub-System & JIT compiler (Part 1)

Whistle While Your Node.js Queue Works

How I created the Search algorithm myself

Create Dynamic Calendly Scheduling button in Salesforce

Create custom user field screen

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ryan Sweigart

Ryan Sweigart

An independent Unity Developer.

More from Medium

5 Common Unity Game Development Mistakes

My journey becoming a Unity game developer: 2.5D

Benefits of Prototyping with Cubes

Game Design Analysis of Flappy Bird and Swing Copters