wood burning stoves 2.0*
The moose likes Other JSE/JEE APIs and the fly likes JNI ( Java Native Interface) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "JNI ( Java Native Interface)" Watch "JNI ( Java Native Interface)" New topic
Author

JNI ( Java Native Interface)

sarju savani
Greenhorn

Joined: Sep 06, 2005
Posts: 2
Hello...

I have just entered in JNI world and facing some problem to working on it. I have read online tutorial from Sun Java website to understand JNI clearly. But getting poblem while writing implementation of C source code of my following code. I am giving whole detail whatever i have done so you could give me better answer of my question. I am very hopeful on you as i am not going ahead due to my poor knowladge to JNI and also in Java .

__________________________________________________________

1) First i made //Wsq.java //

___________________________________________________________

public class Wsq

{
// Native Method Declaration
public native int wsq_decode_mem(ImageParam param); // encoder.c


public native int wsq_encode_mem(ImageParam param); //decoder.c




//Load the library
static
{
System.loadLibrary("Wsq");
}



public static void main(String args[])
{
/* image characteristic parameters */
int width=336;
int height=512;
int depth=8;
int ppi=500;
byte[] odata = new byte [128];
byte[] idata = new byte [128];

/* Encode/compress the image pixmap. */
ImageParam param = new ImageParam(width, height, depth, ppi, idata, odata);

Wsq wsq = new Wsq ();
int ret = wsq.wsq_encode_mem(param);
}
}
_____________________________________________________

2) Then ,I have created Object class // ImageParam.java//
______________________________________________________


import java.lang.System;



public class ImageParam

{
privateint m_width = 336;
private int m_height = 512;
private int m_depth = 8;
private int m_ppi = 500;
private byte [] m_idata = null; /* image rawdata */
private byte [] m_odata = null; /* image wsqdata */



// Constructor

public ImageParam(int width, int height, int depth, int ppi, byte[] idata, byte[] odata)
{
m_width = width;
m_height = height;
m_depth = depth;
m_ppi = ppi;
m_idata = idata;
m_odata = odata;
}


// All Accessor methods

public final int getImageWidth()
{
return m_width;
}



public final int getImageHeight()
{
return m_height;
}



public final int getImageDepth()
{
return m_depth;
}


public final int getImagePpi()
{
return m_ppi;
}



public final byte [] getIdataContent()
{
return m_idata;
}


public final byte [] getOdataContent()
{
return m_odata;
}



// All Mutator Methods

public void setImageWidth(int width)
{
m_width=width;
}

public void setImageHeight(int height)
{
m_height=height;
}

public void setImageDepth(int depth)
{
m_depth=depth;
}

public void setImagePpi(int ppi)
{
m_ppi=ppi;
}


public void setIdataContent(byte[] idata) //idata
{
if (idata.length > 0)
{
m_idata = new byte [idata.length];
System.arraycopy (idata, 0, m_idata, 0, idata.length);
}
}


public void setWsqOdataContent(byte[] odata) //odata
{
if (odata.length > 0)
{
m_odata = new byte [odata.length];
System.arraycopy (odata, 0, m_odata, 0, odata.length);
}
}

}

_________________________________________________________

3) Then , I compiled Wsq.java and it has been compiled without any error . So i am having wsq.class and ImageParam.Class file

Afterthat, I have created headerfile using javah -jni Wsq command. And it has been also generated . So i am also having wsq.h file .

________________________________________________________

Machine generated headerfile // Wsq.h //

_________________________________________________________

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>

/* Header for class com_axa_wsq_Wsq */

#ifndef _Included_com_axa_wsq_Wsq
#define _Included_com_axa_wsq_Wsq

#ifdef __cplusplus
extern "C" {
#endif

/*

* Class: com_axa_wsq_Wsq

* Method: wsq_decode_mem

* Signature: (Lcom/axa/wsq/ImageParam I

*/

JNIEXPORT jint JNICALL Java_com_axa_wsq_Wsq_wsq_1decode_1mem

(JNIEnv *, jobject, jobject);


/*

* Class: Wsq

* Method: wsq_encode_mem

* Signature: (Lcom/axa/wsq/ImageParam I

*/


JNIEXPORT jint JNICALL Java_com_axa_wsq_Wsq_wsq_1encode_1mem

(JNIEnv *, jobject, jobject);


#ifdef __cplusplus
}
#endif

#endif


__________________________________________________________

5) Then , I am writing Implementaion of Wsq.c file And here i am facing the problem like ???..... First i would like to show you my Wsq.c file implementation whatever i have cwritten , it is still incompleted as dont know what to do ?



// Wsq.c //

__________________________________________________________

#include <jni.h>
#include "com_axa_wsq_Wsq.h"
#include <stdio.h>
JNIEXPORT jint JNICALL Java_com_axa_wsq_Wsq_wsq_1decode_1mem(JNIEnv *env, jobject obj, jobject param)
{

jclass cls;
jmethodID mid;
jbyteArray byteArray;


/*********************************************/
/* Get a class reference to ImageParam Class */
/*********************************************/

cls = (*env)->GetObjectClass(env, param);
if(cls == NULL)
{
printf("Class Not Found [com_axa_wsq_ImageParam]\n");
return;
}
else
printf(" - Class Loaded\n");


/**************************************/
/* Invoke method getImageWidth */
/**************************************/

mid = (*env)->GetMethodID(env, cls, "getImageWidth", "()I");
if (mid == 0)
{
printf("Method [getImageWidth(int)] Not Found into [com_axa_wsq_ImageParam]] Class\n");
return;
}

(*env)->CallIntMethod(env, obj, mid);
printf(" - Method [setImageWidth] Invoked\n");



/**************************************/
/* Invoke method getImageHeight */
/**************************************/

mid = (*env)->GetMethodID(env, cls, "getImageHeight", "()I");
if (mid == 0)
{
printf("Method [getImageHeight(int)] Not Found into [com_axa_wsq_ImageParam]] Class\n");
return;
}

(*env)->CallIntMethod(env, obj, mid);
printf(" - Method [getImageHeight] Invoked\n");


/**************************************/
/* Invoke method getImageDepth */
/**************************************/

mid = (*env)->GetMethodID(env, cls, "getImageDepth", "()I");
if (mid == 0)
{
printf("Method [getImageDepth(int)] Not Found into [com_axa_wsq_ImageParam]] Class\n");
return;
}

(*env)->CallIntMethod(env, obj, mid);
printf(" - Method [getImageDepth] Invoked\n");


/**************************************/
/* Invoke method getImagePpi */
/**************************************/

mid = (*env)->GetMethodID(env, cls, "getImagePpi", "()V");
if (mid == 0)
{
printf("Method [getImagePpi(int)] Not Found into [com_axa_wsq_ImageParam]] Class\n");
return;
}

(*env)->CallIntMethod(env, obj, mid);
printf(" - Method [getImagePpi] Invoked\n");

/**************************************/
/* Invoke method getIdataContent */
/**************************************/

mid = (*env)->GetMethodID(env, cls, "getIdataContent", "()[B");
if (mid == 0)
{
printf("Method [getIdataContent(int)] Not Found into [com_axa_wsq_ImageParam]] Class\n");
return;
}

(*env)->CallByteMethod(env, obj, mid);
printf(" - Method [getIdataContent] Invoked\n");



byteArray = (*env)->CallIntMethod(env, obj, mid);




/**************************************/
/* Invoke method getOdataContent */
/**************************************/

mid = (*env)->GetMethodID(env, cls, "getOdataContent", "()[B");
if (mid == 0)
{
printf("Method [getOdataContent(int)] Not Found into [com_axa_wsq_ImageParam]] Class\n");
return;
}

(*env)->CallByteMethod(env, obj, mid);
printf(" - Method [getOdataContent] Invoked\n");


/**************************************/
/* Invoke method setImageWidth */
/**************************************/

mid = (*env)->GetMethodID(env, cls, "setImageWidth", "(I)V");
if (mid == 0)
{
printf("Method [setImageWidth(int)] Not Found into [com_axa_wsq_ImageParam]] Class\n");
return;
}

(*env)->CallVoidMethod(env, obj, mid, width);
printf(" - Method [setImageWidth] Invoked\n");



/**************************************/
/* Invoke method setImageHidth */
/**************************************/

mid = (*env)->GetMethodID(env, cls, "setImageHeight", "(I)V");
if (mid == 0)
{
printf("Method [setImageHeight(int)] Not Found into [com_axa_wsq_ImageParam]] Class\n");
return;
}

(*env)->CallVoidMethod(env, obj, mid, height);
printf(" - Method [setImageHeight] Invoked\n");


/**************************************/
/* Invoke method setImageDepth */
/**************************************/

mid = (*env)->GetMethodID(env, cls, "setImageDepth", "(I)V");
if (mid == 0)
{
printf("Method [setImageDepth(int)] Not Found into [com_axa_wsq_ImageParam]] Class\n");
return;
}

(*env)->CallVoidMethod(env, obj, mid, depth);
printf(" - Method [setImageDepth] Invoked\n");


/**************************************/
/* Invoke method setImagePpi OK */
/**************************************/

mid = (*env)->GetMethodID(env, cls, "setImagePpi", "(I)V");
if (mid == 0)
{
printf("Method [setImagePpi(int)] Not Found into [com_axa_wsq_ImageParam]] Class\n");
return;
}

(*env)->CallVoidMethod(env, obj, mid, ppi);
printf(" - Method [setImagePpi] Invoked\n");




/**************************************/
/* Invoke method setIdataContent OK */
/**************************************/

mid = (*env)->GetMethodID(env, cls, "setIdataContent", "(B)V");
if (mid == 0)
{
printf("Method [setIdataContent(int)] Not Found into [com_axa_wsq_ImageParam]] Class\n");
return;
}

(*env)->CallVoidMethod(env, obj, mid, odata);
printf(" - Method [setIdataContent] Invoked\n");



/**************************************/
/* Invoke method setOdataContent OK */
/**************************************/

mid = (*env)->GetMethodID(env, cls, "setOdataContent", "(B)V");
if (mid == 0)
{
printf("Method [setIdataContent(int)] Not Found into [com_axa_wsq_ImageParam]] Class\n");
return;
}

(*env)->CallVoidMethod(env, obj, mid, null );
printf(" - Method [setOdataContent] Invoked\n");




}






JNIEXPORT jint JNICALL Java_com_axalto_wsq_Wsq_wsq_1encode_1mem((JNIEnv env*, jobject obj, jobject param)


??? // I will write afterwards but before i would like to ask several question .....


_________________________________________________________

So Now my question is you think that i am going on write direction . Should i need to call all get and set method which i declareed in my ImageParam.java class (Means its compulsary to call all those methods or its depends on my requirement). I also want to know that i need to pass GetByteArrayElements() JNI function in my WSQ.c code but dont know how ? Could you help me means what i need to declare in Wsq.c class. I am very very hopeful on you . Pleas give me an answer of my questions.

Thanx in advance

Nishu,



_____________________________________________________


SOme more files i am attaching here , may be it will help you to understand more about my code .
________________________________________________________


_______________________

encoder.c

_______________________


/*****************/
/* Include files */
/*****************/

#include <stdio.h>
#include <wsq.h>
#include <img_io.h>



int debug = 1;

int main(int argc, char *argv[])
{
unsigned char *odata; /* Output data */
int olen; /* Number of bytes in output data. */
float r_bitrate=.75; /* target bit compression rate */
unsigned char *idata; /* Input data */
int width=336;
int height=512; /* image characteristic parameters */
int depth=8;
int ppi=500;
int ret;

int rawflag=1; /* input image flag: 1 == Raw, 0 == IHead */

char *ifile="test.raw";
char *ofile="test.wsq"; /* Input/Output filenames */
IHEAD *ihead; /* Ihead pointer */

/* Read the image into memory (IHead or raw pixmap). */
if(ret = read_raw_or_ihead_wsq(!rawflag, ifile,
&ihead, &idata, &width, &height, &depth))
exit(ret);

if(debug > 0)
fprintf(stdout, "File %s read\n", ifile);


/* Encode/compress the image pixmap. */
if(ret = wsq_encode_mem(&odata, &olen, r_bitrate,
idata, width, height, depth, ppi, (char*)NULL)){
free(idata);
exit(ret);
}

free(idata);

if(debug > 0)
fprintf(stdout, "Image data encoded, compressed byte length = %d\n",
olen);

if(ret = write_raw_from_memsize(ofile, odata, olen)){
free(odata);
exit(ret);
}

free(odata);

/* Exit normally. */
exit(0);
}


____________________

Decoder.c

____________________


#include <stdio.h>
#include <wsq.h>
#include <img_io.h>

int debug = 1;

main(argc,argv)
int argc;
char **argv;
{
int ret;
IHEAD *ihead; /* Ihead pointer */
int rawflag=1; /* raw input data or Ihead image */
char *outext; /* ouput file extension */
char *ifile="test.wsq";
char *ofile="test.raw"; /* file names */
int ilen;
int width;
int height; /* image parameters */
int depth;
int ppi;
unsigned char *idata, *odata; /* image pointers */
int lossyflag; /* data loss flag */
NISTCOM *nistcom; /* NIST Comment */
char *ppi_str;


if(ret = read_raw_from_filesize(ifile, &idata, &ilen))
exit(ret);

if(ret = wsq_decode_mem(&odata, &width, &height, &depth, &ppi,
&lossyflag, idata, ilen)){
free(idata);
exit(ret);
}

if(debug > 1)
fprintf(stderr, "Image pixmap constructed\n");


if(ret = getc_nistcom_wsq(&nistcom, idata, ilen)){
free(idata);
exit(ret);
}
free(idata);

if(ret = write_raw_or_ihead(!rawflag, ofile,
odata, width, height, depth, ppi)){
free(odata);
exit(ret);
}
free(odata);

if(debug > 1)
fprintf(stdout, "Image pixmap written to %s\n", ofile);

exit(0);
}
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Hi,

Welcome to JavaRanch!

This "Java News" forum is for announcements of new Java releases and such -- it's not for asking questions about Java. JNI questions belong in the "Other Java APIs" forum, so I'll move this there for you.

Note, though, that I'll almost guarantee that no one will answer this question. There's so much code that even finding the question in your post (if there is one in there, somewhere) is too much work for most people. Try not to post more than 10 lines of code, or people aren't even going to bother looking.


[Jess in Action][AskingGoodQuestions]
Vitaly Shelest
Greenhorn

Joined: Sep 28, 2005
Posts: 4
Try the tool at
http://simtel.net/product.php[id]90910[SiteID]simtel.net
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JNI ( Java Native Interface)
 
Similar Threads
Setting Fields value through JNI
JNI newbie - different const qualifiers
what's wrong with my program about JNI callback
Hi All,
Need explanation on how native method works.