我对 Java 非常陌生,但我决定要在 java 中加载我的 C++ dll。让我解释一下我在 Java 方面要做什么..
在 Java 中,我正在创建本机函数:GetGLBuffer。参数应该是指向 ByteArray 的指针。Java没有指针,所以我有点迷路了。
在 C++ 中,它相当于: GetGLBuffer(byte* &Buffer); 缓冲区从函数内部填充。
在 Java 中,我做了 GetGLBuffer(ByteBuffer Buffer); 缓冲区从 C++ DLL 中填充并发送回 Java,以便 Java 可以在 JFrame 上绘制它。相反,它会在访问 DLL 时立即崩溃。有人愿意解释我做错了什么吗?
package library;
import java.io.IOException;
import java.nio.ByteBuffer;
class SharedLibrary {
static{System.loadLibrary("TestDLL");}
static native void GetGLBuffer(ByteBuffer Buffer);
public SharedLibrary() throws IOException {
int BitsPerPixel = 32, Width = 765, Height = 565;
int IntSize = ((Width * BitsPerPixel + 31) / 32) * Height;
int ByteSize = IntSize * 4;
ByteBuffer Buffer = ByteBuffer.allocateDirect(ByteSize);
GetGLBuffer(Buffer);
Frame F = new Frame("Testing Buffer", Buffer.array()); //Draw The Image on a frame.
}
}
C++ 方面:
JNIEXPORT void JNICALL Java_library_SharedLibrary_GetGLBuffer(JNIEnv *env, jclass cl, jobject buffer)
{
int Bpp = 32;
Bitmap Foo("C:/Users/Brandon/Desktop/Untitled.bmp");
std::vector<RGB> Pixels = Foo.Foo();
std::vector<unsigned char> TEMP(Foo.Size());
unsigned char* BuffPos = &TEMP[0];
for (int I = 0; I < Foo.Height(); ++I)
{
for (int J = 0; J < Foo.Width(); ++J)
{
*(BuffPos++) = Pixels[(Foo.Height() - 1 - I) * Foo.Width() + J].RGBA.B;
*(BuffPos++) = Pixels[(Foo.Height() - 1 - I) * Foo.Width() + J].RGBA.G;
*(BuffPos++) = Pixels[(Foo.Height() - 1 - I) * Foo.Width() + J].RGBA.R;
if (Bpp > 24)
*(BuffPos++) = Pixels[(Foo.Height() - 1 - I) * Foo.Width() + J].RGBA.A;
}
if(Bpp == 24)
BuffPos += Foo.Width() % 4;
}
jbyte *data = (jbyte*)env->GetDirectBufferAddress(buffer); //Crashes as soon as it hits this.. If commented out, I have no problem.
MessageBox(NULL, "", "", 0);
memcpy(data, TEMP.data(), Foo.Size());
}