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?
我有以下问题。我有C代码,它将PNG图像作为基本原始数据获取并将其保存在内存中。我希望通过使用JNI将这些原始数据转换为Java中的BufferedImage。有谁知道这样做或以前做过这个?
3 个解决方案
#1
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.
我假设您了解如何使用JNI调用函数的基础知识。它并不复杂,虽然它可能是一个痛苦的屁股。
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.
如果您想快速完成它,只需将PNG写入临时文件,通过JNI传递文件名并使用ImageIO加载它。
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.
如果您想要更复杂,并且避免需要文件路径,可以在ByteArrayInputStream上使用ImageIO.read(InputStream),它包装您通过JNI传入的字节数组。您可以从C调用NewByteArray(),然后使用SetByteArrayRegion来设置数据。
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.
最后,您可以考虑使用HTTP来传输数据,Apache有一组可以使用的组件,包括一个小的Web服务器,您可以从C代码POST到Java。
#2
if you've never used JNI before, I'd recommend you to take a look at the JNI Programmer's Guide and Specification.
如果您之前从未使用过JNI,我建议您查看JNI程序员指南和规范。
in summary, what you have to do is:
总之,你要做的是:
- create a Java method with the
native
keyword, with no implementation. - use the command
javah
on the class with the native method to generate a header file (.h).javah
comes with a JDK installation. - implement your native Java function in C/C++.
- search the class java.awt.image.BufferedImage.
- search the constructor you want to use.
- create a BufferedImage object with the specified constructor.
- search the setPixel method.
- run that method to set each pixel value in your image. you'll need to run it height x width times.
- return the object.
搜索类java.awt.image.BufferedImage。
搜索您要使用的构造函数。
使用指定的构造函数创建BufferedImage对象。
搜索setPixel方法。
运行该方法来设置图像中的每个像素值。你需要运行它的高度x宽度时间。
返回对象。
- compile your native file into a shared library.
- load your shared library inside your Java class.
- run your Java class indicating, linking your shared library.
使用native关键字创建Java方法,没有实现。
使用本机方法在类上使用命令javah来生成头文件(.h)。 javah附带JDK安装。
在C / C ++中实现您的本机Java函数。搜索类java.awt.image.BufferedImage。搜索您要使用的构造函数。使用指定的构造函数创建BufferedImage对象。搜索setPixel方法。运行该方法来设置图像中的每个像素值。你需要运行它的高度x宽度时间。返回对象。
将您的本机文件编译为共享库。
在Java类中加载共享库。
运行Java类,指示链接共享库。
there are other ways of copying the raw data of your image, but this way I explained should be enough.
还有其他方法可以复制图像的原始数据,但我解释的这种方式应该足够了。
#3
Since the Java library supports PNG, I would add a mechanism that copied all the bytes from C to Java and use the ImageIO class as Chad Okere suggests.
由于Java库支持PNG,我将添加一种机制,将所有字节从C复制到Java,并使用ImageIO类,如Chad Okere所建议的那样。
Also, consider using JNA to make life easier (example using JNA to draw a Windows cursor).
另外,考虑使用JNA使生活更轻松(例如使用JNA绘制Windows游标)。
#1
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.
我假设您了解如何使用JNI调用函数的基础知识。它并不复杂,虽然它可能是一个痛苦的屁股。
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.
如果您想快速完成它,只需将PNG写入临时文件,通过JNI传递文件名并使用ImageIO加载它。
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.
如果您想要更复杂,并且避免需要文件路径,可以在ByteArrayInputStream上使用ImageIO.read(InputStream),它包装您通过JNI传入的字节数组。您可以从C调用NewByteArray(),然后使用SetByteArrayRegion来设置数据。
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.
最后,您可以考虑使用HTTP来传输数据,Apache有一组可以使用的组件,包括一个小的Web服务器,您可以从C代码POST到Java。
#2
if you've never used JNI before, I'd recommend you to take a look at the JNI Programmer's Guide and Specification.
如果您之前从未使用过JNI,我建议您查看JNI程序员指南和规范。
in summary, what you have to do is:
总之,你要做的是:
- create a Java method with the
native
keyword, with no implementation. - use the command
javah
on the class with the native method to generate a header file (.h).javah
comes with a JDK installation. - implement your native Java function in C/C++.
- search the class java.awt.image.BufferedImage.
- search the constructor you want to use.
- create a BufferedImage object with the specified constructor.
- search the setPixel method.
- run that method to set each pixel value in your image. you'll need to run it height x width times.
- return the object.
搜索类java.awt.image.BufferedImage。
搜索您要使用的构造函数。
使用指定的构造函数创建BufferedImage对象。
搜索setPixel方法。
运行该方法来设置图像中的每个像素值。你需要运行它的高度x宽度时间。
返回对象。
- compile your native file into a shared library.
- load your shared library inside your Java class.
- run your Java class indicating, linking your shared library.
使用native关键字创建Java方法,没有实现。
使用本机方法在类上使用命令javah来生成头文件(.h)。 javah附带JDK安装。
在C / C ++中实现您的本机Java函数。搜索类java.awt.image.BufferedImage。搜索您要使用的构造函数。使用指定的构造函数创建BufferedImage对象。搜索setPixel方法。运行该方法来设置图像中的每个像素值。你需要运行它的高度x宽度时间。返回对象。
将您的本机文件编译为共享库。
在Java类中加载共享库。
运行Java类,指示链接共享库。
there are other ways of copying the raw data of your image, but this way I explained should be enough.
还有其他方法可以复制图像的原始数据,但我解释的这种方式应该足够了。
#3
Since the Java library supports PNG, I would add a mechanism that copied all the bytes from C to Java and use the ImageIO class as Chad Okere suggests.
由于Java库支持PNG,我将添加一种机制,将所有字节从C复制到Java,并使用ImageIO类,如Chad Okere所建议的那样。
Also, consider using JNA to make life easier (example using JNA to draw a Windows cursor).
另外,考虑使用JNA使生活更轻松(例如使用JNA绘制Windows游标)。