简体   繁体   中英

Opencv4Android: How to use with C++

I am trying too build a samples which using opencv for android. Here is my C++ code:

  • Header file:

     #ifdef __cplusplus extern "C" { #endif #include "jni.h" #include "opencv2/core/core.hpp" namespace openCVFuncs { cv::Mat contrastFilter(cv::Mat inputMatm, float contrastValue); } #ifdef __cplusplus } #endif 
  • Cpp file:

     namespace openCVFuncs { cv::Mat contrastFilter(cv::Mat inputMat, float contrastValue) { contrastValue = pow(2,contrastValue); cv::Mat outMat = inputMat.clone(); uchar* data_img_in=(uchar*)inputMat.data; uchar* data_img_out=(uchar*)outMat.data; int temp = 0; for(int i=0;i<inputMat.size().height;i++) for(int j=0;j<inputMat.size().width;j++) for (int c=0;c<inputMat.channels();c++) { temp = (data_img_in+inputMat.step[0]*i)[j*inputMat.channels()+c]; temp = (int)((temp - 128.0) * contrastValue) +128; if (temp < 5) temp = 5; if (temp > 255) temp = 255; (data_img_out+outMat.step[0]*i)[j*outMat.channels()+c] = temp; } return outMat; }; } 

And I got many errors like that :

/opt/android-ndk-r9/sources/cxx-stl/gnu-libstdc++/4.6/include/bits/valarray_before.h:652:3: error: template with C linkage

What wrong with my code ?

When using an "extern C"-block, you can only use the things that are available to C, so that rules out function overloading/polymorphism, namespaces, amongst other things.

In the header file you posted you include a .hpp file (which could possibly include one of the unusable definitions) and define a namespace.

This page gives some nice pointers on the subject on what you can and cannot do and how you can wrap calls to C++ namespaces / overloaded functions for use in a library compiled by a C compiler, see "Accessing C++ Code from Within C Source":

http://www.oracle.com/technetwork/articles/servers-storage-dev/mixingcandcpluspluscode-305840.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM