Added simple command line sample for Android

This commit is contained in:
Andrey Kamaev 2011-05-11 22:46:17 +00:00
parent 5e4ca22737
commit 6aea54e308
6 changed files with 153 additions and 0 deletions

View File

@ -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} )

View File

@ -0,0 +1,7 @@
@ECHO OFF
SETLOCAL
PUSHD %~dp0
SET PROJECT_NAME=HelloAndroid
CALL ..\..\scripts\build.cmd %*
POPD
ENDLOCAL

View File

@ -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 ..

View File

@ -0,0 +1,26 @@
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
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;
}

View File

@ -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

View File

@ -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