edwalt 2015-05-20 02:12 采纳率: 0%
浏览 6474

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

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

  • 写回答

2条回答 默认 最新

  • tanlean123 2015-07-22 03:35
    关注
       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了

    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程