Labfans是一个针对大学生、工程师和科研工作者的技术社区。 论坛首页 | 联系我们(Contact Us)
MATLAB爱好者论坛-LabFans.com
返回   MATLAB爱好者论坛-LabFans.com > 其它 > 资料存档
资料存档 资料存档
回复
 
主题工具 显示模式
旧 2019-12-10, 16:49   #1
poster
高级会员
 
注册日期: 2019-11-21
帖子: 3,006
声望力: 66
poster 正向着好的方向发展
帖子 如何使用Java将Postgres中的图像检索到Matlab中?

我得到了一个奇怪的任务,大约有1500-2000张jpeg图像,大小均为1-50kb。它们目前存储在我用Postgres创建的简单数据库中。自从我大量使用Matlab和Postgres以来,已经有很长时间了,因此,非常感谢您提供任何帮助或建议!

我需要将存储在数据库中的图像从数据库中取出到Java中。最后一步是将图像从Java检索到Matlab中,以便以与imread函数在Matlab中相同的方式存储图像。 imread函数读取图像并创建uint8值的m×3矩阵阵列,该阵列表示RGB像素强度。

Atm我已经用Java将图像输入和输出到数据库中,当前将图像存储在bytea列数据类型中。是最好的数据类型吗?

如何从数据库中取回数据,以便它是我放入的已构建jpeg图像,还是处于请求的矩阵数组格式?

目前,我不了解检索到的数据。它位于大约70,000个元素的字节数组中,其值介于-128到128之间。

注意:数据库工具包对我不可用

另一个更新:我已经解决了与有关“ UTF-8”编码错误的帖子有关的问题。

如果有人偶然发现了此页面,将尽快尝试发布任何答案!我真的很感谢您的想法和答案。再次感谢。


回答:
问题解决了 :-)

我设法使存储在数据库中的bytea列中的字节变成字节数组。然后通过创建一个临时文件(使用ByteArrayInputStream和Reader对象形成一个BufferedImage对象,我将其写入文件)将其发送回数组中的Matlab中。

然后处理我从Matlab中的临时文件中检索和读取的数据。一旦数据在Matlab中,所有临时文件都将被删除。

下面显示了处理ResultSet以便从从数据库bytea列接收的字节数组创建临时图像的代码:

private static void processImageResultSet(ResultSet rs) throws SQLException, FileNotFoundException, IOException{ int i = 0; //used as a count and to name various temp files while(rs.next()){ //loop through result sets byte[] b = rs.getBytes(1); //the bytea column result String location = getFileName(rs.getString(2)); //the name of the jpg file ByteArrayInputStream bis = new ByteArrayInputStream(b); //creates stream storing byts //To make individual names of temporary files unique the current time and date is stored SimpleDateFormat df = new SimpleDateFormat("'Date 'yyyy-MM-dd HH'H'-mm'M'-ss'secs'-SS'ms'"); //formats date string Calendar cal = Calendar.getInstance(); //gets instance of calendar time String fileDate = df.format(cal.getTime()); //gets the time and date as a String Iterator readers = ImageIO.getImageReadersByFormatName("jpg"); //creates a reader object, that will read jpg codec compression format Object source = bis; //object to store stream of bytes from database ImageReader reader = (ImageReader) readers.next(); ImageInputStream iis = ImageIO.createImageInputStream(source); //creates image input stream from object source which stores byte stream reader.setInput(iis, true); //sets the reader object to read image input stream ImageReadParam param = reader.getDefaultReadParam(); Image image = reader.read(0, param); BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB); //creates buffered image Graphics2D g2 = bufferedImage.createGraphics(); g2.drawImage(image, null, null); File imageFile = new File(location + " " + fileDate + " " + i + ".jpg"); //creates image file ImageIO.write(bufferedImage, "jpg", imageFile); //writes buffered image object to created file i++; //counts number of results from query within the ResultSet } }

更多&回答...
poster 当前离线   回复时引用此帖
回复


发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛禁用 表情符号
论坛启用 [IMG] 代码
论坛启用 HTML 代码



所有时间均为北京时间。现在的时间是 23:44


Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.