Matlab:如何正确使用Java ImageIO类来获取BufferedIMage
我正在尝试在 Matlab 中对数据库中的一些图像数据进行模拟。图像数据来自java,输出为base64编码的字节数组。我不熟悉 [java image] 格式。然而,我根据与我一起工作的人编写的一些java编写了以下Matlab代码。它们遵循相同的基本轮廓,并且 Java 代码能够很好地读取图像。 Matlab 代码如下所示:
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 );
检查 bufferedimage
后,发现它是一个 double
空数组,而不是 java BufferedImage
实例。
我对 dataStream 进行了一些测试,看看它的行为是否符合预期;类似于以下内容的小型单元测试:
for jj = 1:10
for kk 1:10
assert( dataStream.read() == data(kk) );
end;
dataStream.reset();
end;
它已检查完毕,因此这使我相信问题出在 ImageIO 或我对它的使用上。
不幸的是,我发现的使用 ImageIO(以及其中一些其他 API)的示例都没有按照我在这里概述的方式使用(即在 Matlab 中)。
此代码使用java .io.ByteArrayInputStream
以相同的方式 - 从某种意义上说,提供的数据是字节数组。
此代码本质上是我的我正在寻找 - 将 java 图像转换为 Matlab 数组。不幸的是,他们通过获取 Matlab 图像,将其转换为 java 图像,然后将其返回来进行作弊。
这段代码使用了ImageIO,但它是这样做的通过从文件流中读取。我尝试将数据写入文件,然后使用 java.io.File 读取它,但无论哪种方式我都得到相同的结果。
所以,我很茫然。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题中概述的方法有效...我与上面的代码有任何不同之处是我没有将 127 添加到 uint8 数组中:
编辑 合并文本 @kchja < a href="http://www.mathworks.com/support/solutions/en/data/1-2WPAYR/?solution=1-2WPAYR" rel="nofollow">链接到,以防该链接变得无效:
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: