我正在开发一个图像处理应用程序。 首先,我得到一个我给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)添加到我的代码中时,执行时会出现这些错误:
它似乎来自AsyncTask,我不知道为什么......