我正在尝试使用 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 - 使用 JNI 将图像从 C++ 发送到 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30874242/