exception - java.lang.ArrayIndexOutOfBoundsException when getting image array -
when try run following code
samplerun.java
public class samplerun { public static void main(string[] args) { jlibfprint jlibfprint = new jlibfprint(); jlibfprint.fp_image_data fpimg = new jlibfprint.fp_image_data(); jlibfprint.fp_image_data fpimg1 = new jlibfprint.fp_image_data() ; try { file file = new file("/some/path/capture.bin"); //reads binary image file fileinputstream fin = new fileinputstream(file); bytearrayoutputstream bos = new bytearrayoutputstream(); byte[] buff = new byte[1024]; try { for(int i;(i = fin.read(buff)) !=-1;){ bos.write(buff,0,i); } } catch (exception e) { // todo: handle exception } byte[] imgbytes = bos.tobytearray(); if(file.length() == 327680) { h=620; w=512; l=327680; } else { h=320; w=256; l=81408; } fpimg.setdata(imgbytes); fpimg.setheight(h); fpimg.setwidth(w); fpimg.setlength(l); try { fpimg1 = jlibfprint.binary_image(fpimg); //error @ line //return object of image structure jni code system.out.println("image binarized\n"); } catch (arrayindexoutofboundsexception e) { // todo auto-generated catch block e.printstacktrace(); } } catch (exception e) { e.printstacktrace(); } } } jlibfprint.java
public class jlibfprint { static { system.loadlibrary("jlibfprint_jni"); } static public class fp_image_data implements java.io.serializable{ int width; int height; int length; byte data[]; public int getwidth() { return width; } public void setwidth(int width) { this.width = width; } public int getheight() { return height; } public void setheight(int height) { this.height = height; } public int getlength() { return length; } public void setlength(int length) { this.length = length; } public byte[] getdata() { return data; } public void setdata(byte[] data) { this.data = data; } public fp_image_data() {} public void clear() { width = 0; height = 0; length = 0; (int = 0; < data.length; i++) data[i] = 0; } } } jni code
jniexport jobject jnicall java_jlibfprint_jlibfprint_binary_1image(jnienv *env, jclass jcls,jobject imgobj) { struct fp_img img; struct fp_img *imgptr; imgptr = &img; jfp2cfp(env,imgobj,imgptr); // function gets value java fp_init(); imgptr = fp_img_binarize(imgptr); cfp2jfp(env, imgobj, imgptr); //this function sets value hava fp_exit(); printf("\nlibrary closed..........."); return imgobj; } i following error
java.lang.arrayindexoutofboundsexception @ jlibfprint.jlibfprint.binary_image(native method) @ jlibfprint.samplerun.main(samplerun.java:23) i trying use functions of libfprint library image processing. have return jni code accessing library functions , have returned object jni java layer @ line getting error.
just thought here. hard-coding length variable 327680 or 81408 . instead try setting imgbytes.length
l = imgbytes.length; also please modify code below , try again :
byte[] buff = new byte[1024]; try { for(int i;(i = fin.read(buff)) !=-1;){ bos.write(buff,0,i); } bos.flush(); //this important } catch (exception e) { e.printstacktrace();//see if error happened here } byte[] imgbytes = bos.tobytearray(); system.out.println(imgbytes.len);//verify has data , length greater 0 !!
Comments
Post a Comment