ByteBuffer到AsyncTask中的byte []导致致命信号11(SIGSEGV)

时间:2015-06-20 19:41:25

标签: java android android-asynctask bytearray bytebuffer

我正在开发一个图像处理应用程序。 首先,我得到一个我给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是我创建的一个类,因为Image Class给我带来了AsyncTask的麻烦。 那就是ImageProcess类中的内容:

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

但问题来自方法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;
}

最精确地来自这些方面:

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

当我将buffer.get(data)添加到我的代码中时,执行时会出现这些错误:

  • 06-20 15:25:05.524 4015-4028 / ca.uqtr.camera2videobasic W / art:暂停所有线程:5.253ms
  • 06-20 15:25:10.020 4015-4100 / ca.uqtr.camera2videobasic A / libc:致命信号11(SIGSEGV),代码1,故障地址0xe1a3c000 in tid 4100(AsyncTask#1)

它似乎来自AsyncTask,我不知道为什么......

0 个答案:

没有答案