使用 JNI 将图像从C++发送到 Java
Send image from C++ to Java using JNI
我正在尝试使用JNI将图像从C++发送到java。该图像是在C++中创建的位图,我在其中使用GetDIBits
将像素投射到char*
。使用C++将图像保存到文件时没有问题,但是将像素发送到Java时,图像都是模糊的。JavaDocs说我必须为BufferedImage使用3BYTE_BGR
,但我觉得压缩有问题
C++位图
在Java中转换为BufferedImage,宽度和高度也通过jni接收
这是图像的结果
鉴于 bi.bitCount 是 32,使用 BufferedImage 3BYTE_BGR
格式是不正确的:它假设每三个字节一个像素,而不是每四个字节一个像素。 请改用 TYPE_INT_BGR
。 正如我们在注释中所讨论的,您的 DataBuffer 需要是一个 DataBufferInt,您可以使用 ByteBuffer 和 IntBuffer 来完成,如以下代码片段所示:
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
IntBuffer intBuffer = ByteBuffer.wrap(img).asIntBuffer();
int[] imgInts = new int[img.length * Byte.SIZE / Integer.SIZE];
intBuffer.get(imgInts);
image.setData(Raster.createRaster(image.getSampleModel(), new DataBufferInt(imgInts, imgInts.length), new Point()));
相关文章:
- 在java中解决这段代码时面临循环中的问题
- 尝试用java代码编译和运行c++代码
- 在这种情况下,java对象是否可以调用本机函数
- 在java中读取c++字节的位字段
- 为什么C++对链表中的下一个节点使用指针,而像 C# 或 Java 这样的语言只使用类 Node 的名称?
- 使用已使用 java 编码的 openssl 解码数据
- SWIG Java 在使用 -DSWIGWORDSIZE64 时将int64_t转换为 jlong
- Android Java USB for native cpp
- 在由Sublime文本3编译后在cmd上显示Java程序输出
- C++ equivalent to Java Map getOrDefault?
- C++ 中的 Java 样式枚举
- 是否有技术原因阻止 Java 中的 final C++ 像 const 一样严格?
- 加密在 Windows、C++ 和 Java 中传输中的数据
- Java从C++回调到C++回调
- 在 c++ 中模拟输入并在 JAVA 中读取它?
- 用C++包装 Java 库 (JNI)
- 使用 TreeSet Java 对反转进行计数
- 验证openssl c++中的签名,这是由JAVA DSA签名的?
- 如何通过 JNI 将 C 字符串表情符号传递给 Java
- 如何从保存在 Java 中C++的字节数组中读取数字?