通过JNI C代码将PNG图像作为BufferedImage加载到Java中

时间:2023-02-10 00:20:58

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:

总之,你要做的是:

  1. create a Java method with the native keyword, with no implementation.
  2. 使用native关键字创建Java方法,没有实现。

  3. use the command javah on the class with the native method to generate a header file (.h). javah comes with a JDK installation.
  4. 使用本机方法在类上使用命令javah来生成头文件(.h)。 javah附带JDK安装。

  5. implement your native Java function in C/C++.
    1. search the class java.awt.image.BufferedImage.
    2. 搜索类java.awt.image.BufferedImage。

    3. search the constructor you want to use.
    4. 搜索您要使用的构造函数。

    5. create a BufferedImage object with the specified constructor.
    6. 使用指定的构造函数创建BufferedImage对象。

    7. search the setPixel method.
    8. 搜索setPixel方法。

    9. run that method to set each pixel value in your image. you'll need to run it height x width times.
    10. 运行该方法来设置图像中的每个像素值。你需要运行它的高度x宽度时间。

    11. return the object.
    12. 返回对象。

  6. 在C / C ++中实现您的本机Java函数。搜索类java.awt.image.BufferedImage。搜索您要使用的构造函数。使用指定的构造函数创建BufferedImage对象。搜索setPixel方法。运行该方法来设置图像中的每个像素值。你需要运行它的高度x宽度时间。返回对象。

  7. compile your native file into a shared library.
  8. 将您的本机文件编译为共享库。

  9. load your shared library inside your Java class.
  10. 在Java类中加载共享库。

  11. run your Java class indicating, linking your shared library.
  12. 运行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:

总之,你要做的是:

  1. create a Java method with the native keyword, with no implementation.
  2. 使用native关键字创建Java方法,没有实现。

  3. use the command javah on the class with the native method to generate a header file (.h). javah comes with a JDK installation.
  4. 使用本机方法在类上使用命令javah来生成头文件(.h)。 javah附带JDK安装。

  5. implement your native Java function in C/C++.
    1. search the class java.awt.image.BufferedImage.
    2. 搜索类java.awt.image.BufferedImage。

    3. search the constructor you want to use.
    4. 搜索您要使用的构造函数。

    5. create a BufferedImage object with the specified constructor.
    6. 使用指定的构造函数创建BufferedImage对象。

    7. search the setPixel method.
    8. 搜索setPixel方法。

    9. run that method to set each pixel value in your image. you'll need to run it height x width times.
    10. 运行该方法来设置图像中的每个像素值。你需要运行它的高度x宽度时间。

    11. return the object.
    12. 返回对象。

  6. 在C / C ++中实现您的本机Java函数。搜索类java.awt.image.BufferedImage。搜索您要使用的构造函数。使用指定的构造函数创建BufferedImage对象。搜索setPixel方法。运行该方法来设置图像中的每个像素值。你需要运行它的高度x宽度时间。返回对象。

  7. compile your native file into a shared library.
  8. 将您的本机文件编译为共享库。

  9. load your shared library inside your Java class.
  10. 在Java类中加载共享库。

  11. run your Java class indicating, linking your shared library.
  12. 运行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游标)。