【问题标题】:java.lang.ArrayIndexOutOfBoundsException when getting image array获取图像数组时出现 java.lang.ArrayIndexOutOfBoundsException
【发布时间】:2013-08-18 19:16:58
【问题描述】:

当我尝试运行以下代码时

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 代码

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;
}

我收到以下错误

java.lang.ArrayIndexOutOfBoundsException

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

我正在尝试使用libfprint 库的函数进行图像处理。我已返回用于访问该库函数的 JNI 代码,并且我已将对象从 JNI 返回到 java 层,但在那一行我收到了错误。

【问题讨论】:

  • 请添加fp_image_data类的代码和JNI代码
  • @c.s.好的,我已经编辑了我的问题。
  • 数组binarized[]data[]在哪里初始化?
  • 我已经通过调用 set 方法在 Sample.java 文件中设置了该值。
  • 你的意思是setData()?请不要在评论时更改代码。在您的示例中,这个 setData() 在哪里调用?

标签: java exception exception-handling java-native-interface indexoutofboundsexception


【解决方案1】:

这里只是一个想法。您将长度变量硬编码为 327680 或 81408 。而是尝试将其设置为 imgbytes.length

l = imgbytes.length;

另外请修改如下代码并重试:

     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 !!

【讨论】:

  • 我尝试设置imgbytes.length,但仍然显示错误
  • 你能打印完整的异常堆栈跟踪吗?我发现 JNI 调用抛出 java 异常很可疑。还有 imgbytes.length 的实际值是多少?(只是想确认它不是 0 !)
  • 此外,在读取以增强您的 catch 块时,它会忽略任何异常。请在那里执行 e.printStacktrace() 以确保没有发生错误
  • 您还需要执行 bos.flush() 以确保所有字节都写入内部缓冲区。我已将代码放入我的答案中
  • 我已经尝试过您建议的更改。imgbytes.length 给出的图像长度为 81402,bos.flush() 之后也不例外。
猜你喜欢
  • 1970-01-01
  • 2019-06-17
  • 1970-01-01
  • 1970-01-01
  • 2017-11-02
  • 1970-01-01
  • 2023-03-14
  • 2012-08-01
  • 2020-08-17
相关资源
最近更新 更多