Hey everyone! 👋 If you're working with Uniface, you'll eventually need to handle images. Whether it's a user's profile picture or a product photo, knowing the right way to store and manage this data is key. Let's dive into one of Uniface's fundamental data types for this job: image.
Just a quick note: This blog post was put together with the help of an AI assistant to break down the official Uniface documentation into a more digestible format. 🤖
What is the image Data Type?
In simple terms, the image data type in Uniface is a container for binary data. While its name suggests it's only for pictures, it's really about holding any kind of binary information. Think of it as a special box for data that isn't simple text or numbers.
You would typically use it to handle images from various sources, such as:
- 🖼️ Images stored directly in a database.
- 📁 Glyphs or icons used in your application's UI.
- 💾 Image files loaded from a disk (like a .jpg or .png).
- 🧩 Data from third-party filters that output binary image data.
How Do You Use It?
In your Uniface application, you can declare variables or parameters with the image data type within a ProcScript block. This is done in the variables or params section of your script.
Here’s a simple example of how you might declare an image variable and load a picture into it from a file:
variables
image v_user_avatar
endvariables
; ProcScript to load an image from a local file into our variable
read_image v_user_avatar, "C:\temp\avatar.png"
; Now you can use the v_user_avatar variable, for example,
; to assign it to an image widget in your form.
$widget("AVATAR_WIDGET") = v_user_avatar
In this snippet, v_user_avatar is now holding the binary data of the avatar.png file, ready to be displayed or processed further.
Important Rules to Remember 🧐
Working with the image data type comes with a couple of important rules that can save you from headaches later.
1. No Math! 🧮
You cannot perform arithmetic operations on image data. It might sound obvious, but it's worth remembering. Trying to add, subtract, or multiply two images together just doesn't make sense and isn't allowed.
2. The Big Gotcha: Comparing Images 🤔
This is the most critical point. You cannot directly compare two image variables to see if they are identical.
For example, this code will always be true, even if the images are different!
if (v_image1 == v_image2)
; This block will ALWAYS execute, which is not what you want!
endif
The correct way to check if two images are the same is to calculate a hash for each one and then compare the hashes. A hash is like a unique fingerprint for data. If the fingerprints match, the data is identical.
Here’s a conceptual example:
variables
string v_hash1, v_hash2
image v_image1, v_image2
endvariables
; ... imagine v_image1 and v_image2 are loaded with image data ...
; Calculate a SHA256 hash for each image
v_hash1 = $$hash(v_image1, "SHA256")
v_hash2 = $$hash(v_image2, "SHA256")
; Now, compare the string hashes
if (v_hash1 == v_hash2)
message "The images are identical! ✅"
else
message "The images are different. ❌"
endif
Conclusion
The image data type is your go-to for handling binary image data in Uniface. Just remember to declare it, load your data, and be very careful when comparing two images—always use a hash! ✨
Happy coding!
Top comments (0)