ByteBuffer to byte[] in AsyncTask result to Fatal signal 11 (SIGSEGV)

480 views Asked by At

I'm develloping an application for image process. To start I get an Image that i give to an AsyncTask :

@Override
            public void onImageAvailable(ImageReader reader) {
             int jump = 5; //Le nombre d'image à sauter avant d'en traiter une, pour liberer de la mémoire
             Image readImage = reader.acquireNextImage();
                ImageCopy tmpImage = new ImageCopy(readImage.getPlanes()[0].getBuffer(),readImage.getWidth(),readImage.getHeight());
             readImage.close();
                if(count == jump){
                    //Image Process here
                    new ImageProcess().execute(tmpImage);
                    count = 0;
                    realCount++;
                }
                else count++;

                totalCount++;

            }

ImageCopy is a class I made because the Image Class give me trouble with AsyncTask. Then this is what it is in ImageProcess class :

@Override
protected IplImage doInBackground(ImageCopy... images) {
    ByteBuffer buffer = images[0].getBuffer();
    int width = images[0].getWidth();
    int height = images[0].getHeight();

    //Conversion de l'Image passé en paramêtre en une IplImage
    IplImage iplImage = bufferToIpl(buffer,width,height);
    //Egalisation de l'histogramme en niveau de gris
    opencv_imgproc.cvEqualizeHist(iplImage,iplImage);
    //Seuillage de l'image
    opencv_imgproc.cvThreshold(iplImage,iplImage,96.0,255.0,opencv_imgproc.CV_THRESH_BINARY);
    //TEST : Enregistrement image
    //opencv_highgui.cvSaveImage("test.jpg",iplImage);

    return iplImage;
}

But the problem come from the method bufferToIple :

protected IplImage bufferToIpl(ByteBuffer buffer, int width, int height) {
    int f = 1;// SUBSAMPLING_FACTOR;
    //Conversion du ByteBuffer en un byte array
    byte[] data = new byte[buffer.remaining()];
    buffer.get(data);

    // First, downsample our image and convert it into a grayscale IplImage
    IplImage grayImage = IplImage.create(width / f, height / f, opencv_core.IPL_DEPTH_8U, 1);

    int imageWidth = grayImage.width();
    int imageHeight = grayImage.height();
    int dataStride = f * width;
    int imageStride = grayImage.widthStep();
    ByteBuffer imageBuffer = grayImage.getByteBuffer();
    for (int y = 0; y < imageHeight; y++) {
        int dataLine = y * dataStride;
        int imageLine = y * imageStride;
        for (int x = 0; x < imageWidth; x++) {
            imageBuffer.put(imageLine + x, data[dataLine + f * x]);
        }
    }

    return grayImage;
}

Most precisly from those lines :

byte[] data = new byte[buffer.remaining()];
    buffer.get(data);

When i add buffer.get(data) to my code i have those bugs at execution :

  • 06-20 15:25:05.524 4015-4028/ca.uqtr.camera2videobasic W/art﹕ Suspending all threads took: 5.253ms
  • 06-20 15:25:10.020 4015-4100/ca.uqtr.camera2videobasic A/libc﹕ Fatal signal 11 (SIGSEGV), code 1, fault addr 0xe1a3c000 in tid 4100 (AsyncTask #1)

It seem to come from AsyncTask and i Have no idea why...

0

There are 0 answers