I’m trying to do a mock-up in Matlab on some image data in a database. The image data is from java, output as a base64 encoded byte array. I’m not familiar with the [java image] format. However, I wrote the following Matlab code based on some java written by someone working with me. They follow the same basic outline, and the Java code is able to read the image just fine. The Matlab code looks like this:
function [ result ] = queryDb( theQuery )
conn = database( ... ); % connect to the database
result = fetch( exec( conn, theQuery ) );
result = result.Data;
close( conn );
end
data = queryDb( 'sql query to get the data' );
data = uint8( data{1,1} );
data = org.apache.commons.codec.binary.Base64.decodeBase64( data );
data = uint8( 127 + data ); % the base64 decoder returns signed int8
import javax.imageio.ImageIO;
import java.io.ByteArrayInputStream;
dataStream = ByteArrayInputStream( data );
bufferedimage = ImageIO.read( dataStream );
Upon inspection of bufferedimage, it is an empty array of double as opposed to a java BufferedImage instance.
I ran a few tests on dataStream to see if it behaved as expected; sort of a mini unit test along the lines of:
for jj = 1:10
for kk 1:10
assert( dataStream.read() == data(kk) );
end;
dataStream.reset();
end;
It checked out, so this leads me to believe the problem is with ImageIO or my use of it.
Unfortunately, none of the examples I’ve found for using ImageIO (and some of these other APIs) are used in quite the manner I outline here (in Matlab that is).
This code uses java.io.ByteArrayInputStream in the same manner — in the sense that the provided data is an array of bytes.
This code is essentially what I’m looking to do — convert a java image to a Matlab array. Unfortunately, they cheat by taking a Matlab image, turning it into a java image, then turn it back.
This code uses ImageIO, but it does so by reading from a file stream. I tried writing the data out to a file then reading it in using java.io.File, but I get the same result either way.
So, I’m at a loss.
The approach outlined in the question works … the only thing I have that differs in any way from the code above is I’m not adding 127 to the uint8 array:
edit Incorporating the text @kchja linked to, in case that link becomes invalid: