使用JNI从C启动Java main方法会导致OpenGL错误

时间:2012-07-30 16:55:15

标签: java c java-native-interface jogl

我正在用Java创建一个程序,它将与C库连接,从硬件中获取图像并使用OpenGL显示(使用JOGL)。所以工作流程如下:

Hardware -> C -> disk image file -> Java -> JOGL

我让Java -> JOGL部分工作正常。图像完全显示,我可以一次加载多个图像。我也有Hardware -> C工作,C中的临时查看器显示图像正在创建中。

问题的症结在于:我希望能够在C中启动Java程序的main()方法,然后仅使用C中的JNI代码显示图像(使用我创建的静态方法)。但是,当我这样做时,图像被截断,我只得到前20行左右。我知道我正在加载整个图像,因为我可以检查图像中每个像素的像素值。仅截断显示。对于每个加载的图像,显​​示相同数量的像素。

简而言之,这是C代码:

int main () { ...
    HMODULE * jvm_dll;

    //HWND hWnd = GetConsoleWindow();
    //ShowWindow( hWnd, SW_HIDE );

    env = create_vm(&jvm, jvm_dll);

    if (env == NULL) { return 1; }

    //Create a new String array with one blank String
    cls = (*env)->FindClass(env,"java/lang/String");
    arg = (*env)->NewStringUTF(env,"");
    args = (jobjectArray)(*env)->NewObjectArray(env, 1, cls, arg);

    //Call the main method with the String array argument
    cls = (*env)->FindClass(env, "path/to/package/program");
    mID = (*env)->GetStaticMethodID(env, cls, "main", "([Ljava/lang/String;)V");
    (*env)->CallStaticVoidMethod(env, cls, mID, args);

    PrintStackTrace(env);

    blockAndClose(jvm, env, jvm_dll);

    return ret;
}

int blockAndClose() {...
    int ret = 0;

    if (jvm == 0 || env == 0) {
        FreeLibrary(*jvm_dll);
        return 1;
    }

    ret = (*jvm)->DestroyJavaVM(jvm);
    if(jvm_dll) {
        FreeLibrary(*jvm_dll);
        jvm_dll = 0;
    }

    env = 0;
    jvm = 0;

    return ret;
}

我知道我只发布了C部分,但是当我纯粹用Java运行时,Java部分正在运行。此时,C部分只是各种“发射器”,所以我想知道它为什么会影响代码的运行。有什么建议吗?

修改

这是加载图片的代码。我使用JAI作为PlanarImage类型加载图像(TiledImage类是子类)。

tempI = JAI.create("fileload", path);

DataBufferUShort dbOut = null;
ColorModel CM = PlanarImage.getDefaultColorModel(DataBuffer.TYPE_USHORT, tempI.getNumBands());
SampleModel SM = CM.createCompatibleSampleModel(tempI.getWidth(), tempI.getHeight());

//Ensure that the buffer is in the internal format (USHORT)
if (tempI.getData().getDataBuffer().getDataType() != DataBuffer.TYPE_USHORT) {
    DataBuffer dBIn = tempI.getData().getDataBuffer();

    short [] dBPixels = new short[dBIn.getSize()];

    switch(dBIn.getDataType()) {
        case DataBuffer.TYPE_BYTE:
        DataBufferByte dBByte = (DataBufferByte)dBIn;
        byte [] pByte = dBByte.getData();
        for (int b = 0; b < pByte.length; b++)
        { dBPixels[b] = (short)((double)pByte[b] / 0xFF * 0xFFFF); }
        dbOut = new DataBufferUShort(dBPixels, dBPixels.length);
        break;
        case DataBuffer.TYPE_SHORT:
        DataBufferShort dBShort = (DataBufferShort)dBIn;
        dBPixels = dBShort.getData();
        dbOut = new DataBufferUShort(dBPixels, dBPixels.length);
        break;
    } //SWITCH DATA TYPE --END

    WritableRaster rs = Raster.createWritableRaster(SM, dbOut, new Point(0,0));
    tempI = new TiledImage(0,0,tempI.getWidth(),tempI.getHeight(),0,0,SM,CM);
    ((TiledImage)tempI).setData(rs);
}

0 个答案:

没有答案