opencv/platforms/android/service/engine/jni/BinderComponent/OpenCVEngine.cpp

203 lines
5.3 KiB
C++
Raw Normal View History

2012-06-21 22:50:05 +08:00
#include "EngineCommon.h"
#include "OpenCVEngine.h"
#include "HardwareDetector.h"
#include "StringUtils.h"
#include <utils/Log.h>
#include <assert.h>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <dlfcn.h>
using namespace android;
const int OpenCVEngine::Platform = DetectKnownPlatforms();
const int OpenCVEngine::CpuID = GetCpuID();
const int OpenCVEngine::KnownVersions[] = {2040000, 2040100, 2040200, 2040300, 2040301, 2040302, 2040400, 2040500, 2040600, 2040700, 2040701, 2040800, 2040900};
2012-06-21 22:50:05 +08:00
bool OpenCVEngine::ValidateVersion(int version)
2012-06-21 22:50:05 +08:00
{
for (size_t i = 0; i < sizeof(KnownVersions)/sizeof(int); i++)
if (KnownVersions[i] == version)
return true;
2012-06-21 22:50:05 +08:00
return false;
2012-06-21 22:50:05 +08:00
}
int OpenCVEngine::NormalizeVersionString(std::string version)
2012-06-21 22:50:05 +08:00
{
int result = 0;
2012-06-21 22:50:05 +08:00
if (version.empty())
{
return result;
2012-06-21 22:50:05 +08:00
}
std::vector<std::string> parts = SplitStringVector(version, '.');
// Use only 4 digits of the version, i.e. 1.2.3.4.
// Other digits will be ignored.
if (parts.size() > 4)
parts.erase(parts.begin()+4, parts.end());
int multiplyer = 1000000;
for (std::vector<std::string>::const_iterator it = parts.begin(); it != parts.end(); ++it)
2012-06-21 22:50:05 +08:00
{
int digit = atoi(it->c_str());
result += multiplyer*digit;
multiplyer /= 100;
2012-06-21 22:50:05 +08:00
}
if (!ValidateVersion(result))
result = 0;
2012-06-21 22:50:05 +08:00
return result;
}
OpenCVEngine::OpenCVEngine(IPackageManager* PkgManager):
PackageManager(PkgManager)
{
assert(PkgManager);
}
int32_t OpenCVEngine::GetVersion()
{
return OPEN_CV_ENGINE_VERSION;
2012-06-21 22:50:05 +08:00
}
String16 OpenCVEngine::GetLibPathByVersion(android::String16 version)
{
std::string std_version(String8(version).string());
int norm_version;
2012-06-21 22:50:05 +08:00
std::string path;
LOGD("OpenCVEngine::GetLibPathByVersion(%s) impl", String8(version).string());
norm_version = NormalizeVersionString(std_version);
if (0 != norm_version)
2012-06-21 22:50:05 +08:00
{
path = PackageManager->GetPackagePathByVersion(norm_version, Platform, CpuID);
if (path.empty())
{
LOGI("Package OpenCV of version \"%s\" (%d) is not installed. Try to install it :)", String8(version).string(), norm_version);
}
else
{
FixPermissions(path);
}
2012-06-21 22:50:05 +08:00
}
else
{
LOGE("OpenCV version \"%s\" (%d) is not supported", String8(version).string(), norm_version);
2012-06-21 22:50:05 +08:00
}
return String16(path.c_str());
}
android::String16 OpenCVEngine::GetLibraryList(android::String16 version)
{
std::string std_version = String8(version).string();
int norm_version;
2012-06-21 22:50:05 +08:00
String16 result;
norm_version = NormalizeVersionString(std_version);
if (0 != norm_version)
2012-06-21 22:50:05 +08:00
{
std::string tmp = PackageManager->GetPackagePathByVersion(norm_version, Platform, CpuID);
if (!tmp.empty())
2012-10-17 07:18:30 +08:00
{
tmp += (std::string("/") + LIB_OPENCV_INFO_NAME);
LOGD("Trying to load info library \"%s\"", tmp.c_str());
void* handle;
InfoFunctionType info_func;
handle = dlopen(tmp.c_str(), RTLD_LAZY);
if (handle)
{
const char* error;
dlerror();
info_func = (InfoFunctionType)dlsym(handle, "GetLibraryList");
if ((error = dlerror()) == NULL)
{
result = String16((*info_func)());
dlclose(handle);
}
else
{
LOGE("Library loading error: \"%s\"", error);
}
}
else
{
LOGI("Info library not found in package");
}
2012-10-17 07:18:30 +08:00
}
else
{
LOGI("Package OpenCV of version \"%s\" (%d) is not installed. Try to install it :)", std_version.c_str(), norm_version);
2012-10-17 07:18:30 +08:00
}
2012-06-21 22:50:05 +08:00
}
else
{
LOGE("OpenCV version \"%s\" is not supported", std_version.c_str());
2012-06-21 22:50:05 +08:00
}
return result;
}
bool OpenCVEngine::InstallVersion(android::String16 version)
{
std::string std_version = String8(version).string();
int norm_version;
2012-06-21 22:50:05 +08:00
bool result = false;
LOGD("OpenCVEngine::InstallVersion() begin");
2012-06-21 22:50:05 +08:00
norm_version = NormalizeVersionString(std_version);
if (0 != norm_version)
2012-10-17 07:18:30 +08:00
{
LOGD("PackageManager->InstallVersion call");
result = PackageManager->InstallVersion(norm_version, Platform, CpuID);
}
else
{
LOGE("OpenCV version \"%s\" (%d) is not supported", std_version.c_str(), norm_version);
2012-06-21 22:50:05 +08:00
}
LOGD("OpenCVEngine::InstallVersion() end");
return result;
}
bool OpenCVEngine::FixPermissions(const std::string& path)
{
LOGD("Fixing permissions for folder: \"%s\"", path.c_str());
chmod(path.c_str(), S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
DIR* dir = opendir(path.c_str());
if (!dir)
{
LOGD("Fixing permissions error");
return false;
2012-06-21 22:50:05 +08:00
}
dirent* files = readdir(dir);
while (files)
{
LOGD("Fix permissions for \"%s\"", files->d_name);
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);
files = readdir(dir);
2012-06-21 22:50:05 +08:00
}
closedir(dir);
return true;
}