Created
March 9, 2016 11:04
-
-
Save tzutalin/1a34619bf897117bb30a to your computer and use it in GitHub Desktop.
BuildAnroidUsingOpencvCMake
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
OPENCV_PREBUILT=/home/darrenl/developments/RobotVisionLib/ext/android.x86_32/opencv/sdk/native/jni | |
OUR_TOOLCHAIN=/home/darrenl/developments/RobotVisionLib/build-scripts/make-cmake-manual-config/android.toolchain-simple.cmake | |
OPENCV_TOOLCHAIN=/home/darrenl/library/opencv/platforms/android/android.toolchain.cmake | |
OPENCV_INSTALL=/home/darrenl/library/opencv/install/sdk/native/jni | |
cmake -DANDROID_ABI=x86 -DOpenCV_DIR=${OPENCV_INSTALL} -DCMAKE_TOOLCHAIN_FILE=${OPENCV_TOOLCHAIN} $@ . | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CMAKE_MINIMUM_REQUIRED( VERSION 2.8 ) | |
######################################################### | |
# Set project name | |
######################################################### | |
IF( NOT PROJECT_NAME ) | |
IF ( NOT "x$ENV{PROJECT_NAME}" STREQUAL "x" ) | |
SET( PROJECT_NAME $ENV{PROJECT_NAME} ) | |
ELSE() | |
SET( PROJECT_NAME hello-android ) | |
ENDIF() | |
ENDIF() | |
SET( PROJECT_NAME ${PROJECT_NAME} CACHE STRING "The name of your project") | |
PROJECT( ${PROJECT_NAME} ) | |
######################################################### | |
# Find OpenCV | |
######################################################### | |
if (ANDROID) | |
message(STATUS "Hello from Android build!!!!!!!!") | |
endif() | |
find_package(ZLIB) | |
FIND_PACKAGE(OpenCV REQUIRED) | |
######################################################### | |
# c/c++ flags, includes and lib dependencies | |
######################################################### | |
#notice the "recycling" of CMAKE_C_FLAGS | |
#this is necessary to pick up android flags | |
SET( CMAKE_C_FLAGS " -pie -fPIE ${CMAKE_C_FLAGS}" ) | |
SET( CMAKE_CPP_FLAGS " -pie -fPIE -fexceptions -frtti -std=c++11 ${CMAKE_CPP_FLAGS}" ) | |
SET( CMAKE_CXX_FLAGS " -pie -fPIE -fexceptions -frtti -std=c++11 ${CMAKE_CXX_FLAGS}" ) | |
SET( CMAKE_EXE_LINKER_FLAGS " -pie -fPIE ${CMAKE_EXE_LINKER_FLAGS}" ) | |
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}) | |
SET( LIBRARY_DEPS ${OpenCV_LIBS} ${ZLIB_LIBS}) | |
SET( LIBRARY_DEPS ${LIBRARY_DEPS} log dl m z) | |
######################################################### | |
# source files | |
######################################################### | |
FILE( GLOB hdrs "*.h*" ) | |
FILE( GLOB srcs "*.cpp" ) | |
ADD_EXECUTABLE( ${PROJECT_NAME} ${srcs} ) | |
TARGET_LINK_LIBRARIES( ${PROJECT_NAME} ${LIBRARY_DEPS} ) | |
######################################################### | |
# Summary report | |
######################################################### | |
message( STATUS "") | |
message( STATUS "General configuration for ${PROJECT_NAME} =====================================") | |
message( STATUS "") | |
message( STATUS " OpenCV path: ${OpenCV_DIR}") | |
message( STATUS " Compiler: ${CMAKE_CXX_COMPILER}") | |
message( STATUS " C++ flags (Release): ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_RELEASE}") | |
message( STATUS " C++ flags (Debug): ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_DEBUG}") | |
if(WIN32) | |
message( STATUS " Linker flags (Release): ${CMAKE_EXE_LINKER_FLAGS} ${CMAKE_EXE_LINKER_FLAGS_RELEASE}") | |
message( STATUS " Linker flags (Debug): ${CMAKE_EXE_LINKER_FLAGS} ${CMAKE_EXE_LINKER_FLAGS_DEBUG}") | |
else() | |
message( STATUS " Linker flags (Release): ${CMAKE_SHARED_LINKER_FLAGS} ${CMAKE_SHARED_LINKER_FLAGS_RELEASE}") | |
message( STATUS " Linker flags (Debug): ${CMAKE_SHARED_LINKER_FLAGS} ${CMAKE_SHARED_LINKER_FLAGS_DEBUG}") | |
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <opencv2/core.hpp> | |
#include <opencv2/imgproc.hpp> | |
#include <opencv2/imgcodecs.hpp> | |
#include <opencv2/highgui.hpp> | |
#include <iostream> | |
#include <iostream> | |
#include <memory> | |
#include <thread> | |
#include <chrono> | |
#include <mutex> | |
struct Base | |
{ | |
Base() { std::cout << " Base::Base()\n"; } | |
// Note: non-virtual destructor is OK here | |
~Base() { std::cout << " Base::~Base()\n"; } | |
}; | |
struct Derived: public Base | |
{ | |
Derived() { std::cout << " Derived::Derived()\n"; } | |
~Derived() { std::cout << " Derived::~Derived()\n"; } | |
}; | |
void thr(std::shared_ptr<Base> p) | |
{ | |
std::this_thread::sleep_for(std::chrono::seconds(1)); | |
std::shared_ptr<Base> lp = p; // thread-safe, even though the | |
// shared use_count is incremented | |
{ | |
static std::mutex io_mutex; | |
std::lock_guard<std::mutex> lk(io_mutex); | |
std::cout << "local pointer in a thread:\n" | |
<< " lp.get() = " << lp.get() | |
<< ", lp.use_count() = " << lp.use_count() << '\n'; | |
} | |
} | |
using namespace cv; | |
const char* message = "Hello Android!"; | |
int main(int argc, char* argv[]) | |
{ | |
(void)argc; (void)argv; | |
// print message to console | |
printf("%s\n", message); | |
// put message to simple image | |
Size textsize = getTextSize(message, FONT_HERSHEY_COMPLEX, 3, 5, 0); | |
Mat img(textsize.height + 20, textsize.width + 20, CV_32FC1, Scalar(230,230,230)); | |
putText(img, message, Point(10, img.rows - 10), FONT_HERSHEY_COMPLEX, 3, Scalar(0, 0, 0), 5); | |
std::cout << img.cols; | |
printf("%s\n", "start to save"); | |
// save\show resulting image | |
imwrite("img.jpg", img); | |
cv::Mat image = imread(argv[1], CV_LOAD_IMAGE_COLOR); // Read the file | |
// | |
std::shared_ptr<Base> p = std::make_shared<Derived>(); | |
std::cout << "Created a shared Derived (as a pointer to Base)\n" | |
<< " p.get() = " << p.get() | |
<< ", p.use_count() = " << p.use_count() << '\n'; | |
std::thread t1(thr, p), t2(thr, p), t3(thr, p); | |
p.reset(); // release ownership from main | |
std::cout << "Shared ownership between 3 threads and released\n" | |
<< "ownership from main:\n" | |
<< " p.get() = " << p.get() | |
<< ", p.use_count() = " << p.use_count() << '\n'; | |
t1.join(); t2.join(); t3.join(); | |
std::cout << "All threads completed, the last one deleted Derived\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment