Conversion JPEG image into hexadecimal file packets containing 84 hexadecimal characters

40 views (last 30 days)
Hello all! I'm trying to convert a JPEG image into hexadecimal file packets containing 84 hexadecimal characters. Could you suggest a method of approaching this? The intention is that I want to convert the JPEG image into the hexadecimals packets, then the hexadecimal packets will be sent through a Lora network to the cloud where the hexadecimal packets will be converted back to an image. Thanks!

Answers (1)

Walter Roberson
Walter Roberson on 7 Oct 2021
IMG = imread('flamingos.jpg');
len = reshape(typecast(uint32(numel(IMG)), 'uint8'), [], 1);
imgbytes = reshape(typecast(IMG, 'uint8'), [], 1);
buffer = vertcat(len, imgbytes);
leftover = mod(numel(buffer), 42);
if leftover ~= 0
buffer = vertcat(buffer, zeros(42-leftover, 1, 'uint8'));
end
buffer = sprintf('%02x', buffer);
buffer = reshape(buffer, 84, []);
Now the columns of buffer are the hex contents.
The hex has had the used buffer length pre-pended to it, as a 32 bit number. The buffer was zero padded to make it a multiple of 84 hex digits. The purposes of the length at the beginning is to allow you to know when to stop converting the buffer (because the last buffer had to have padding to make it 84 hex digits.)
I would suggest that you consider also prepending the image size as rows, columns, panes (panes in case of RGB). In fact if you put those in you do not need to put the buffer size in, as it can be calculated. Unless, that is, that you choose to do compression of some sort on the data -- which would probably be a good idea.
There are variations of this in what you send is not the image but instead a copy of the file . The jpg file has already compressed the data, and already has size information -- and might have EXIF information stored as well. But if you send the file then you often need to write the file out to disk on the receiving side and then read the saved file in order to display it.
  3 Comments
West Lim
West Lim on 4 Nov 2021
Hi @Walter Roberson Here I have resized the b&w image in to 255 255, however I'm getting errors as shown in the screenshot. Would you please let me know why theres this errors and how I should solve it? Much appreciated! (and if screenshot is not enough let me know if I need to post the code)

Sign in to comment.

Categories

Find more on Manage Products in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!