5

Khi tôi cố gắng chạy đoạn mã saujava.lang.ArrayIndexOutOfBoundsException khi nhận mảng hình ảnh

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 the 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 at this line 
      //return the object of image structure from the 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; 
     for (int i = 0; i < data.length; i++) data[i] = 0; 
    } 
    } 

} 

JNI Mã số

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); // this function gets the value from java 
    fp_init(); 

    imgptr = fp_img_binarize(imgptr); 

    cfp2jfp(env, imgobj, imgptr); //this function sets the value to hava 

    fp_exit(); 
    printf("\nlibrary closed..........."); 
    return imgobj; 
} 

tôi nhận được lỗi sau

java.lang.ArrayIndexOutOfBoundsException 

    at jlibfprint.JlibFprint.binary_image(Native Method) 
    at jlibfprint.SampleRun.main(SampleRun.java:23) 

Tôi cố gắng để sử dụng chức năng của thư viện cho libfprint xử lý hình ảnh. Tôi đã trả về mã JNI để truy cập các hàm thư viện đó và tôi cũng đã trả về đối tượng từ JNI đến lớp java nhưng tại dòng đó tôi nhận được lỗi.

+0

Thêm mã của lớp 'fp_image_data' và mã JNI xin –

+0

@ c.s. OK, tôi đã chỉnh sửa câu hỏi của mình. – rachana

+0

Các mảng 'binarized [] ',' data [] 'được khởi tạo ở đâu? –

Trả lời

1

Chỉ cần suy nghĩ ở đây. Bạn đang mã hóa cứng biến độ dài thành 327680 hoặc 81408. Thay vì cố gắng thiết lập đó để imgbytes.length

l = imgbytes.length; 

Cũng xin sửa đổi các mã như dưới đây và thử lại:

 byte[] buff = new byte[1024]; 
      try 
      { 
       for(int i;(i = fin.read(buff)) !=-1;){ 
        bos.write(buff,0,i);     
       } 
       bos.flush(); //this is important 
      } 
      catch (Exception e) { 
       e.printStackTrace();//see if any error happened here 

      } 

     byte[] imgbytes = bos.toByteArray(); 
     System.out.println(imgbytes.len);//Verify that it has some data and its length is greater than 0 !! 
+0

Tôi đã cố gắng đặt 'imgbytes.length' nhưng vẫn hiển thị lỗi – rachana

+0

Bạn có thể in toàn bộ stacktrace ngoại lệ không? Tôi thấy nó đáng ngờ rằng một cuộc gọi JNI là ném một ngoại lệ java .. Ngoài ra giá trị thực tế của imgbytes.length là gì? (Chỉ muốn xác nhận nó không phải 0!) – Zenil

+0

Ngoài ra khi đọc vào để buff khối catch của bạn là bỏ qua bất kỳ trường hợp ngoại lệ . Vui lòng thực hiện e.printStacktrace() để đảm bảo rằng không có lỗi nào xảy ra – Zenil

Các vấn đề liên quan