opencv4android开发获取摄像头帧数据处理后返回屏幕显示问题

我在JNI中用C++的opencv对图片进行了处理,就是在人脸上画了个框,但是传回Java再显示就没有框了(用同样的方法进行单张的图片处理就没问题),请问这是为什么以及该如何解决。部分代码如下:

Java代码:

  public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
        mRgba = inputFrame.rgba();
        Bitmap bmp_new = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(),Bitmap.Config.ARGB_8888);
        Utils.matToBitmap(mRgba, bmp_new, false);
        int w = bmp_new.getWidth();  
        int h = bmp_new.getHeight();  
        int[] pixels = new int[w*h];      
        bmp_new.getPixels(pixels, 0, w, 0, 0, w, h);  
        int[] resultInt = facedetect(pixels, w, h);  //native方法,在JNI进行处理,就是画框
        Bitmap resultImg = Bitmap.createBitmap(w, h, Config.ARGB_8888);  
        resultImg.setPixels(resultInt, 0, w, 0, 0, w, h);  
        Mat mat_bmp = new Mat(resultImg.getHeight(), resultImg.getWidth(), CvType.CV_8UC4);
        Utils.bitmapToMat(resultImg, mat_bmp, false);
        return mat_bmp;
}

C++代码:

 JNIEXPORT jintArray JNICALL Java_org_opencv_samples_facedetect_FdActivity_facedetect
(JNIEnv *env, jclass obj, jintArray buf, jint w, jint h){
   jint *cbuf;
   cbuf = env->GetIntArrayElements(buf, false);
   if(cbuf == NULL){
       return 0;
       }
   Mat imgData(h, w, CV_8UC4, (unsigned char*)cbuf);


  //Load the cascades
   face_cascade.load( face_cascade_name );
   eyes_cascade.load( eyes_cascade_name );

//detect&draw
   detectAndDisplay( imgData );

   int size=w * h;
      jintArray result = env->NewIntArray(size);
      env->SetIntArrayRegion(result, 0, size, cbuf);
      env->ReleaseIntArrayElements(buf, cbuf, 0);
      return result;
}

/**
* @function detectAndDisplay
*/
void detectAndDisplay( Mat frame )
{
   std::vector<Rect> faces;
   Mat frame_gray;

   cvtColor( frame, frame_gray, COLOR_BGR2GRAY );
   equalizeHist( frame_gray, frame_gray );
   //-- Detect faces
   face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );

   for( size_t i = 0; i < faces.size(); i++ )
    {
      Point center( faces.x + faces.width/2, faces.y + faces.height/2 );
      ellipse( frame, center, Size( faces.width/2, faces.height/2), 0, 0, 360, Scalar( 255, 0, 255 ), 2, 8, 0 );

      Mat faceROI = frame_gray( faces );
      std::vector<Rect> eyes;

      //-- In each face, detect eyes
      eyes_cascade.detectMultiScale( faceROI, eyes, 1.1, 2, 0 |CV_HAAR_SCALE_IMAGE, Size(30, 30) );

      for( size_t j = 0; j < eyes.size(); j++ )
       {
         Point eye_center( faces.x + eyes[j].x + eyes[j].width/2, faces.y + eyes[j].y + eyes[j].height/2 );
         int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 );
         circle( frame, eye_center, radius, Scalar( 255, 0, 0), 3, 8, 0 );
       }
    }

}

希望大神能够告知问题所在。

2个回答

楼主问题解决否,跪求答案

楼主能把原项目发给我看下吗?刚好我最近也在弄这个,QQ416734877@qq.com,不胜感激

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问