From 6aea54e3085903ae25e343e20650a37fb321748e Mon Sep 17 00:00:00 2001 From: Andrey Kamaev Date: Wed, 11 May 2011 22:46:17 +0000 Subject: [PATCH] Added simple command line sample for Android --- android/apps/HelloAndroid/CMakeLists.txt | 45 +++++++++++++++++++ android/apps/HelloAndroid/cmake_android.cmd | 7 +++ android/apps/HelloAndroid/cmake_android.sh | 12 ++++++ android/apps/HelloAndroid/main.cpp | 26 +++++++++++ android/apps/HelloAndroid/run.cmd | 48 +++++++++++++++++++++ android/apps/HelloAndroid/run.sh | 15 +++++++ 6 files changed, 153 insertions(+) create mode 100644 android/apps/HelloAndroid/CMakeLists.txt create mode 100644 android/apps/HelloAndroid/cmake_android.cmd create mode 100644 android/apps/HelloAndroid/cmake_android.sh create mode 100644 android/apps/HelloAndroid/main.cpp create mode 100644 android/apps/HelloAndroid/run.cmd create mode 100644 android/apps/HelloAndroid/run.sh diff --git a/android/apps/HelloAndroid/CMakeLists.txt b/android/apps/HelloAndroid/CMakeLists.txt new file mode 100644 index 0000000000..14f34619d0 --- /dev/null +++ b/android/apps/HelloAndroid/CMakeLists.txt @@ -0,0 +1,45 @@ +CMAKE_MINIMUM_REQUIRED( VERSION 2.8 ) + +IF( NOT PROJECT_NAME ) + IF ( NOT "x$ENV{PROJECT_NAME}" STREQUAL "x" ) + SET( PROJECT_NAME $ENV{PROJECT_NAME} ) + ELSE() + SET( PROJECT_NAME HelloAndroid ) + ENDIF() +ENDIF() +SET( PROJECT_NAME ${PROJECT_NAME} CACHE STRING "The name of your project") + +PROJECT( ${PROJECT_NAME} ) + +######################################################### +# Find OpenCV +######################################################### + +SET( OpenCV_DIR ${CMAKE_SOURCE_DIR}/../../build CACHE PATH "The path where you built opencv for android" ) +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 "${CMAKE_C_FLAGS} -Wall -pedantic" ) +SET( CMAKE_CPP_FLAGS "${CMAKE_CPP_FLAGS} -Wall -pedantic" ) + +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}) + +SET( LIBRARY_DEPS ${OpenCV_LIBS} ) +IF( ANDROID ) + SET( LIBRARY_DEPS ${LIBRARY_DEPS} log dl ) +ENDIF() + +######################################################### +# source files +######################################################### + +FILE( GLOB hdrs "*.h*" ) +FILE( GLOB srcs "*.cpp" ) + +ADD_EXECUTABLE( ${PROJECT_NAME} ${srcs} ) +TARGET_LINK_LIBRARIES( ${PROJECT_NAME} ${LIBRARY_DEPS} ) diff --git a/android/apps/HelloAndroid/cmake_android.cmd b/android/apps/HelloAndroid/cmake_android.cmd new file mode 100644 index 0000000000..dd71ba84af --- /dev/null +++ b/android/apps/HelloAndroid/cmake_android.cmd @@ -0,0 +1,7 @@ +@ECHO OFF +SETLOCAL +PUSHD %~dp0 +SET PROJECT_NAME=HelloAndroid +CALL ..\..\scripts\build.cmd %* +POPD +ENDLOCAL \ No newline at end of file diff --git a/android/apps/HelloAndroid/cmake_android.sh b/android/apps/HelloAndroid/cmake_android.sh new file mode 100644 index 0000000000..1d34c5cbe6 --- /dev/null +++ b/android/apps/HelloAndroid/cmake_android.sh @@ -0,0 +1,12 @@ +#!/bin/sh +cd `dirname $0` + +BUILD_DIR=build_armeabi +opencv_android=`pwd`/../.. +opencv_build_dir=$opencv_android/$BUILD_DIR + +mkdir -p $BUILD_DIR +cd $BUILD_DIR + +cmake -DOpenCV_DIR=$opencv_build_dir -DARM_TARGET=armeabi -DCMAKE_TOOLCHAIN_FILE=$opencv_android/android.toolchain.cmake .. + diff --git a/android/apps/HelloAndroid/main.cpp b/android/apps/HelloAndroid/main.cpp new file mode 100644 index 0000000000..cfe6a301e7 --- /dev/null +++ b/android/apps/HelloAndroid/main.cpp @@ -0,0 +1,26 @@ +#include +#include + +using namespace cv; +const char* message = "Hello Android!"; + +int main(int argc, char* argv[]) +{ + // print message to console + printf("%s\n", message); + + // put message to simple image + Size textsize = getTextSize(message, CV_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), CV_FONT_HERSHEY_COMPLEX, 3, Scalar(0, 0, 0), 5); + + // save\show resulting image +#if ANDROID + imwrite("/mnt/sdcard/HelloAndroid.png", img); +#else + imshow("test", img); + waitKey(); +#endif + return 0; +} + diff --git a/android/apps/HelloAndroid/run.cmd b/android/apps/HelloAndroid/run.cmd new file mode 100644 index 0000000000..4175cb882a --- /dev/null +++ b/android/apps/HelloAndroid/run.cmd @@ -0,0 +1,48 @@ +:: this batch file copies compiled executable to the device, +:: runs it and gets resulting image back to the host +:: +:: Here is sample output of successful run: +:: +:: 204 KB/s (2887388 bytes in 13.790s) +:: Hello Android! +:: 304 KB/s (8723 bytes in 0.028s) + +@ECHO OFF + +:: enable command extensions +VERIFY BADVALUE 2>NUL +SETLOCAL ENABLEEXTENSIONS || (ECHO Unable to enable command extensions. & EXIT \B) + +PUSHD %~dp0 +SET PROJECT_NAME=HelloAndroid + +:: try to load config file +SET CFG_PATH=..\..\scripts\wincfg.cmd +IF EXIST %CFG_PATH% CALL %CFG_PATH% + +:: check if sdk path defined +IF NOT DEFINED ANDROID_SDK (ECHO. & ECHO You should set an environment variable ANDROID_SDK to the full path to your copy of Android SDK & GOTO end) +(PUSHD "%ANDROID_SDK%" 2>NUL && POPD) || (ECHO. & ECHO Directory "%ANDROID_SDK%" specified by ANDROID_SDK variable does not exist & GOTO end) +SET adb=%ANDROID_SDK%\platform-tools\adb.exe + +:: copy file to device (usually takes 10 seconds or more) +%adb% push .\bin\armeabi\%PROJECT_NAME% /data/bin/sample/%PROJECT_NAME% || GOTO end + +:: set execute permission +%adb% shell chmod 777 /data/bin/sample/%PROJECT_NAME% || GOTO end + +:: execute our application +%adb% shell /data/bin/sample/%PROJECT_NAME% || GOTO end + +:: get image result from device +%adb% pull /mnt/sdcard/HelloAndroid.png || GOTO end + +GOTO end + +:: cleanup (comment out GOTO above to enable cleanup) +%adb% shell rm /data/bin/sample/%PROJECT_NAME% +%adb% shell rm /mnt/sdcard/HelloAndroid.png + +:end +POPD +ENDLOCAL \ No newline at end of file diff --git a/android/apps/HelloAndroid/run.sh b/android/apps/HelloAndroid/run.sh new file mode 100644 index 0000000000..8ac62b0abf --- /dev/null +++ b/android/apps/HelloAndroid/run.sh @@ -0,0 +1,15 @@ +#!/bin/sh +cd `dirname $0` +PROJECT_NAME=HelloAndroid + +# copy file to device (usually takes 10 seconds or more) +adb push ./bin/armeabi/$PROJECT_NAME /data/bin/sample/$PROJECT_NAME || return + +# set execute permission +adb shell chmod 777 /data/bin/sample/$PROJECT_NAME || return + +# execute our application +adb shell /data/bin/sample/$PROJECT_NAME || return + +# get image result from device +adb pull /mnt/sdcard/HelloAndroid.png || return \ No newline at end of file