0

我正在使用 C++ 加载图像并通过 ByteBuffer 将像素提供给 JNI。我知道像素输入得很好,因为如果图像是正方形的,它们渲染得非常好。如果它们是矩形的,它们就会变形。我还成功地将图像保存在 DLL 中,并且可以正常工作。Java 不幸地放弃了我(除非它是方形的)。我不知道为什么!我究竟做错了什么?

package library;
import java.awt.image.BufferedImage;
import javax.swing.*;

public class Frame extends JFrame {

    public Frame(int Width, int Height, String FrameName, BufferedImage Buffer) {
        setName(FrameName);
        setSize(Width, Height);
        getContentPane().add(new JLabel(new ImageIcon(Buffer)));
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setVisible(true);
    }
}

所有加载:

package library;

import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.IOException;
import java.nio.ByteBuffer;

class SharedLibrary {

    static{System.loadLibrary("TestDLL");} 
    private static native void GetGLBuffer(ByteBuffer Buffer);

    private ByteBuffer Buffer = null;    
    private int ByteSize = 0, Width = 0, Height = 0, BitsPerPixel = 32;

    public SharedLibrary(int ImageWidth, int ImageHeight) throws IOException {

        Width = ImageWidth;
        Height = ImageHeight;

        ByteSize = ((Width * BitsPerPixel + 31) / 32) * 4 * Height;     //Compute Image Size in Bytes.
    Buffer = ByteBuffer.allocateDirect(ByteSize);                   //Allocate Space for the image data.

        GetGLBuffer(Buffer);                                            //Fill the buffer with Image data from the DLL.

        byte[] Bytes = new byte[ByteSize];
        Buffer.get(Bytes);

        BufferedImage Image = new BufferedImage(Width, Height, BufferedImage.TYPE_3BYTE_BGR);
        WritableRaster raster = (WritableRaster) Image.getData();
        raster.setPixels(0, 0, Width, Height, ByteBufferToIntBuffer(Bytes));
        Image.setData(raster);

        Frame F = new Frame(Width, Height, "", Image);
    }

    private int[] ByteBufferToIntBuffer(byte[] Data) {
        int IntBuffer[] = new int[Data.length];
        for (int I = 0; I < Data.length; I++) {
            IntBuffer[I] = (int)Data[I] & 0xFF;
        }
        return IntBuffer;
    }
}

在此处输入图像描述 上面的图像被完美地绘制,因为它几乎是正方形的。如果我将其调整为矩形,它会变形。例子:

在此处输入图像描述

变得扭曲,看起来像: 在此处输入图像描述

4

0 回答 0