1 #include "EngineCommon.h"
2 #include "OpenCVEngine.h"
3 #include "HardwareDetector.h"
4 #include "StringUtils.h"
14 using namespace android;
16 const int OpenCVEngine::Platform = DetectKnownPlatforms();
17 const int OpenCVEngine::CpuID = GetCpuID();
18 const int OpenCVEngine::KnownVersions[] = {2040000, 2040100, 2040200, 2040300, 2040301, 2040302, 2040400, 2040500, 2040600};
20 bool OpenCVEngine::ValidateVersion(int version)
22 for (size_t i = 0; i < sizeof(KnownVersions)/sizeof(int); i++)
23 if (KnownVersions[i] == version)
29 int OpenCVEngine::NormalizeVersionString(std::string version)
38 std::vector<std::string> parts = SplitStringVector(version, '.');
40 // Use only 4 digits of the version, i.e. 1.2.3.4.
41 // Other digits will be ignored.
43 parts.erase(parts.begin()+4, parts.end());
45 int multiplyer = 1000000;
46 for (std::vector<std::string>::const_iterator it = parts.begin(); it != parts.end(); ++it)
48 int digit = atoi(it->c_str());
49 result += multiplyer*digit;
53 if (!ValidateVersion(result))
59 OpenCVEngine::OpenCVEngine(IPackageManager* PkgManager):
60 PackageManager(PkgManager)
65 int32_t OpenCVEngine::GetVersion()
67 return OPEN_CV_ENGINE_VERSION;
70 String16 OpenCVEngine::GetLibPathByVersion(android::String16 version)
72 std::string std_version(String8(version).string());
76 LOGD("OpenCVEngine::GetLibPathByVersion(%s) impl", String8(version).string());
78 norm_version = NormalizeVersionString(std_version);
80 if (0 != norm_version)
82 path = PackageManager->GetPackagePathByVersion(norm_version, Platform, CpuID);
85 LOGI("Package OpenCV of version \"%s\" (%d) is not installed. Try to install it :)", String8(version).string(), norm_version);
94 LOGE("OpenCV version \"%s\" (%d) is not supported", String8(version).string(), norm_version);
97 return String16(path.c_str());
100 android::String16 OpenCVEngine::GetLibraryList(android::String16 version)
102 std::string std_version = String8(version).string();
105 norm_version = NormalizeVersionString(std_version);
107 if (0 != norm_version)
109 std::string tmp = PackageManager->GetPackagePathByVersion(norm_version, Platform, CpuID);
112 tmp += (std::string("/") + LIB_OPENCV_INFO_NAME);
114 LOGD("Trying to load info library \"%s\"", tmp.c_str());
117 InfoFunctionType info_func;
119 handle = dlopen(tmp.c_str(), RTLD_LAZY);
125 info_func = (InfoFunctionType)dlsym(handle, "GetLibraryList");
126 if ((error = dlerror()) == NULL)
128 result = String16((*info_func)());
133 LOGE("Library loading error: \"%s\"", error);
138 LOGI("Info library not found in package");
143 LOGI("Package OpenCV of version \"%s\" (%d) is not installed. Try to install it :)", std_version.c_str(), norm_version);
148 LOGE("OpenCV version \"%s\" is not supported", std_version.c_str());
154 bool OpenCVEngine::InstallVersion(android::String16 version)
156 std::string std_version = String8(version).string();
160 LOGD("OpenCVEngine::InstallVersion() begin");
162 norm_version = NormalizeVersionString(std_version);
164 if (0 != norm_version)
166 LOGD("PackageManager->InstallVersion call");
167 result = PackageManager->InstallVersion(norm_version, Platform, CpuID);
171 LOGE("OpenCV version \"%s\" (%d) is not supported", std_version.c_str(), norm_version);
174 LOGD("OpenCVEngine::InstallVersion() end");
179 bool OpenCVEngine::FixPermissions(const std::string& path)
181 LOGD("Fixing permissions for folder: \"%s\"", path.c_str());
182 chmod(path.c_str(), S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
184 DIR* dir = opendir(path.c_str());
187 LOGD("Fixing permissions error");
191 dirent* files = readdir(dir);
194 LOGD("Fix permissions for \"%s\"", files->d_name);
195 chmod((path + std::string("/") + std::string(files->d_name)).c_str(), S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
196 files = readdir(dir);