通过JNI C代码将PNG图像作为BufferedImage加载到Java中 [英] Load a PNG image into Java as BufferedImage through JNI C code

查看:199
本文介绍了通过JNI C代码将PNG图像作为BufferedImage加载到Java中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题。我有C代码,它将PNG图像作为基本原始数据获取并将其保存在内存中。我希望通过使用JNI将这些原始数据转换为Java中的BufferedImage。有没有人知道这样做或以前做过这个?

I have the following problem. I have C code that acquires a PNG image as basically raw data and keeps it in memory. I would like this raw data to be translated to a BufferedImage in Java, through the use of JNI. Does anyone know any way of doing this or has done this before?

推荐答案

我会假设你知道如何做的基本知识用JNI调用函数。它并不复杂,虽然它可能是一个痛苦的屁股。

I'll assume you know the basics of how to call functions with JNI. It's not that complicated, although it can be a pain in the ass.

如果你想快速完成它,只需将PNG写入临时文件,通过JNI传递文件名并使用ImageIO加载它。

If you want to get it done quickly, just write the PNG to a temp file, pass the file name up through JNI and load it using ImageIO.

如果你想变得更复杂,并且避免需要文件路径,你可以使用 ImageIO.read(InputStream) .oracle.com / javase / 7 / docs / api / java / io / ByteArrayInputStream.htmlrel =nofollow noreferrer> ByteArrayInputStream ,它包装您通过JNI传入的字节数组。你可以致电 NewByteArray()来自C,然后使用 SetByteArrayRegion 设置数据。

If you want to get more sophisticated, and avoid needing a file path, you can use ImageIO.read(InputStream) on a ByteArrayInputStream that wraps a byte array you pass in through JNI. You can call NewByteArray() from C and then use SetByteArrayRegion to set the data.

最后,您可以考虑使用HTTP传输数据,Apache 有一组你可以使用的组件,包括一个小的Web服务器,你可以从你的C代码发布到Java。

Finally, you might consider using HTTP to transfer the data, Apache has a set of components you can use that include a little web server, you can POST from your C code to Java.

这篇关于通过JNI C代码将PNG图像作为BufferedImage加载到Java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆