如何在Android模拟器上显示Mat图像?使用NDK

时间:2017-12-16 12:28:48

标签: java android c++ opencv java-native-interface

我想在android模拟器上显示Mat图像。我将assetManager从java传递到c++并将图像作为资产加载。然后我执行AAsset_read,并将图像数据保存到char *缓冲区,并使用cv::imdecode转换为Mat。 从这里开始,如何将Mat图像添加到模拟器中?

在默认的stringFromJNI方法中,他们似乎使用显示文字的重新turn->envNewStringUTF(hello.c_str());

Java_com_example_user_project_MainActivity_stringFromJNI(JNIEnv *env, jobject /* this */) {

std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());

那么如何使用Mat图像进行此操作呢?

编辑:这是我的c ++代码:

JNIEXPORT jintArray JNICALL     Java_com_example_user_activity_MainActivity_generateAssets(JNIEnv* env,jobject thiz,jobject assetManager) {

AAsset* img;

AAssetManager *mgr = AAssetManager_fromJava(env, assetManager);
AAssetDir* assetDir = AAssetManager_openDir(mgr, "");
const char* filename;
while ((filename = AAssetDir_getNextFileName(assetDir)) != NULL) {
    AAsset *asset = AAssetManager_open(mgr, filename, AASSET_MODE_UNKNOWN);

    if(strcmp(filename, "hi.jpg")==0 ) {
        img = asset;
    }
}
long sizeOfImg = AAsset_getLength(img);
char* buffer = (char*) malloc (sizeof(char)*sizeOfImg);
AAsset_read(img, buffer, sizeOfImg);


std::vector<char> data(buffer, buffer + sizeOfImg);

cv::Mat h = cv::imdecode(data, IMREAD_UNCHANGED);

jintArray resultImage = env->NewIntArray(h.total());
jint *_data = new jint[h.total()];
for (int i = 0; i < h.total(); i++) {
    char b = h.data[4 * i];
    char g = h.data[4 * i + 1];
    char r = h.data[4 * i + 2];
    char a = 255;
    _data[i] = (((jint) a << 24) & 0xFF000000) + (((jint) r << 16) & 0x00FF0000) +
               (((jint) g << 8) & 0x0000FF00) + ((jint) b & 0x000000FF);
}
env->SetIntArrayRegion(resultImage, 0, h.total(), _data);
delete[]_data;
return resultImage;

}

和java代码:

    assetManager = getAssets();
    resultImage = generateAssets(assetManager);

    Bitmap imageinjava = Bitmap.createBitmap(resultImage, 100,100, Bitmap.Config.ARGB_8888);

    ImageView tv1;
    tv1= (ImageView) findViewById(R.id.image);
    tv1.setImageBitmap(imageinjava);

1 个答案:

答案 0 :(得分:0)

为了转换您的原生Mat图像,您可以这样做:

JNIEXPORT jintArray JNICALL  Java_com_example_user_activity_MainActivity_generateAssets(JNIEnv* env, jobject thiz, jobject assetManager) {
AAssetManager *mgr = AAssetManager_fromJava(env, assetManager);
AAsset *img = AAssetManager_open(mgr, "hi.jpg", AASSET_MODE_UNKNOWN);

long sizeOfImg = AAsset_getLength(img);
char* buffer = (char*)AAsset_getBuffer(img);

std::vector<char> data(buffer, buffer + sizeOfImg);

cv::Mat h = cv::imdecode(data, IMREAD_UNCHANGED);

jintArray resultImage = env->NewIntArray(h.total());
jint *_data = new jint[h.total()];
for (int i = 0; i < h.total(); i++) {
    char b = h.data[h.channels() * i];
    char g = h.data[h.channels() * i + 1];
    char r = h.data[h.channels() * i + 2];
    char a = 255;
    _data[i] = (((jint) a << 24) & 0xFF000000) + (((jint) r << 16) & 0x00FF0000) +
               (((jint) g << 8) & 0x0000FF00) + ((jint) b & 0x000000FF);
}
env->SetIntArrayRegion(resultImage, 0, h.total(), _data);
delete[]_data;
return resultImage;

}

在你的java代码中就像那样

    int[] result = generateAssets(getAssets());
    Bitmap bitmap = Bitmap.createBitmap(result, yourimagewidth, yourimageheight, Bitmap.Config.ARGB_8888);

如果要在c ++和java之间传递本机Mat对象 - 返回指向Mat的指针并稍后将其传递给本机函数;

Mat *mat = new Mat(100, 100);
return (jlong) mat;