使用__android_log_print打印Opencv Mat,Android NDK的内容

时间:2017-12-23 12:49:16

标签: android c++ opencv android-ndk

我有以下代码,我想用它来将trainingLables Mat的内容打印到android控制台:

#include <android/log.h>

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

    .....
    .....

    Mat_<int> trainingLabels(1,10);
    trainingLabels << 0, 0, 0, 1, 1, 1, 0, 1, 1, 1;

    __android_log_print(ANDROID_LOG_ERROR, "TESTING", "%d %d", trainingLabels.???????);

    ......
    ......


}

我有两个问题:

1)我使用什么方法从trainingLabels打印一行数据? (我在代码中用什么代替问号?)

2)如何在Android LogCat中搜索__android_log_print中的内容?

1 个答案:

答案 0 :(得分:2)

您可以事先使用您的数据准备一个字符串

char matRow[100] = ""; // You can calculate how much memory u need, but for debug prints just put a big enough number

Mat_<int> trainingLabels(1,10);
trainingLabels << 0, 0, 0, 1, 1, 1, 0, 1, 1, 1;

for (int i = 0; i < 10; i++) {
    sprintf(matRow + strlen(matRow), "%d ", trainingLabels.at<int>(i)); // You can use data from row you need by accessing trainingLabels.row(...).data
}

android_log_print(ANDROID_LOG_ERROR, "SEARCH FOR THIS TAG", "%s", matRow);

在你的logcat中寻找标签,在我的例子中是“搜索这个标签”

关于它如何存储在Mat对象中的另一个问题 - 因为你用int类型创建了Mat - Mat.data是一个int数组,你可以很容易地看到这样的东西:

char matRow[100] = ""; // You can calculate how much memory u need, but for debug prints just put a big enough number

Mat_<int> trainingLabels(1,10);
trainingLabels << 0, 0, 0, 1, 1, 1, 0, 1, 1, 1;

int * data = (int*)trainingLabels.data;

for (int i = 0; i < 10; i++) {
    sprintf(matRow + strlen(matRow), "%d ", data[i]); // You can use data from row you need by accessing trainingLabels.row(...).data
}
android_log_print(ANDROID_LOG_ERROR, "SEARCH FOR THIS TAG", "%s", matRow);