Android中通过JNI传递图片数据的问题(IplImage和Bitmap)

我的C++代码在Visual Studio中运行是正常的,输入和输出图片如下:
图片说明

在安卓中使用native函数调用此C++代码,显示结果如下:
图片说明

部分代码如下:

c++

 int* SharedMatting::getResult(int w, int h){
    int intsize = w*h;
    int* outImage = new int[intsize];
    IplImage *tempp = new IplImage(outputMat);//outputMat is a 4channels Mat.
    int* pixalpha = new int[intsize*4];
    int* pixred = new int[intsize*4];
    int* pixgreen = new int[intsize*4];
    int* pixblue = new int[intsize*4];
    for(int i=0; i<(intsize); i++){
        pixalpha[i] = (int)tempp->imageData[i*4+3];
        pixblue[i] = (int)tempp->imageData[i*4+0];
        pixgreen[i] = (int)tempp->imageData[i*4+1];
        pixred[i] = (int)tempp->imageData[i*4+2];
        outImage[i] = (pixalpha[i] << 24) | (pixred[i] << 16) | (pixgreen[i] << 8) | pixblue[i];
    }
    return outImage;
}

main.cpp

 JNIEXPORT jintArray JNICALL Java_Matting (JNIEnv* env, jclass clas, jint w, jint h){
    int size = w * h;
    jintArray result = env->NewIntArray(size);
    int* resultemp = new int[size];
    resultemp = sm.getResult(w, h);
    env->SetIntArrayRegion(result, 0, size, resultemp);
    return result;
}

Java code with Android:

private MyTask mTask;
ImageView imgView;
int[] resultInt;
int w, h;

private class MyTask extends AsyncTask<String, Integer, String> {
    protected String doInBackground(String... params) {
        resultInt = SharedMat.Matting(w, h);
        return null;
    }

    protected void onPostExecute(String result) {
        Bitmap resultImg = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        resultImg.setHasAlpha(true);
        resultImg.setPixels(resultInt, 0, w, 0, 0, w, h);
        imgView.setImageBitmap(resultImg);
    }
}

我觉得应该是图片数据传来传去传坏掉了?

   uchar* ptr = myimg.ptr(0);

   for(int i=0; i<(intsize); i++){
       //pixalpha[i] = (int)tempp->imageData[i*4+3];
       //pixblue[i] = (int)tempp->imageData[i*4+0];
      // pixgreen[i] = (int)tempp->imageData[i*4+1];
      // pixred[i] = (int)tempp->imageData[i*4+2];
       pixalpha[i] = ptr[4*i+3];
       pixblue[i] = ptr[4*i+0];
       pixgreen[i] = ptr[4*i+1];
       pixred[i] = ptr[4*i+2];

       outImage[i] = ((pixalpha[i] << 24)&(0xff<< 24)) | (pixred[i] << 16) | (pixgreen[i] << 8) | pixblue[i];
   }

估计是你直接操作数据没有定位好文件头ptr(0),这样改完OK了

我也是出现这个问题,数据转了以后变得特别奇怪
楼主怎么解决的