How to Add Original Library to Android NDK
Noritsuna Imamura
noritsuna@siprop.org

©SIProp Project, 2006-2008

1
Agenda
How to Add Original Library to Android NDK
Build OpenCV
Only C/C++ World on Android

©SIProp Project, 2006-2008

2
How to Add New Library

©SIProp Project, 2006-2008

3
Library Layer
Linux Software & Linux Library
aac
android-clat
android-mock
ant-glob
antlr
apache-harmony
apache-http
apach...
How to Develop?
NDK wo/ADT
Standard Android
Application for C/C++
Only C/C++ on Limited
Library Layer

Call Stack
APK File...
How to Build OpenCV for Android

©SIProp Project, 2006-2008

6
Download OpenCV & NDK tool
›

›

wget
https://github.com/Itseez/opencv/archive/
2.4.7.tar.gz
tar zxfv 2.4.7.tar.gz

› mkdi...
Edit Setup File
Add Android Source Code directory.
~/opencv2.4.7/platforms/scripts/cmake_android_service.sh
-DANDROID_SOUR...
Set Path to SDK/NDK

›
›

export ANDROID_NDK=~/android/androidndk-r8e
export ANDROID_SDK=~/android/androidsdk-linux

©SIPr...
Make Version File
Require “AndroidCamera” Func.
›

cp -v
~/nexus_work/android/development/sdk/pla
tform_source.prop_templa...
Copy some haeder files & so Libs
Require “AndroidCamera” Func.
›

cp -aR ~/android/android-ndk-r8e/platforms/android14/arc...
Modify Compile Option
~/opencv2.4.7/modules/androidcamera/CMakeLists.txt
›
›
›
›
›
›
›
›
›
›
›
›
›
›
›
›
›

function(repla...
Include new Include File Dir
Android 4.3’s Include File Dir was separated.
~/opencv2.4.7/platforms/android/service/engine/...
Delete "DetectTegra“ Func
This Android System doesn't have "Tegra“.
~/opencv2.4.7/platforms/android/service/engine/jni/Bin...
Make Define Log Func
Android Logging System was Changed…
Make 『FakeLog.h』 file
~/opencv2.4.7/platform/android/service/engi...
Include Fake Log Header
~/opencv-2.4.7/platform/android/service/engine/jni/
BinderComponent/BnOpenCVEngine.h
BinderCompone...
Execute Auto Config & make
This Script make Makefile for OpenCV.
In “~/opencv2.4.7/platforms/scripts/build_android_service...
Where are Made Files?
OpenCV Libs.
In “~/opencv2.4.7/platforms/build_android_service/lib”
highgui
core
etc...

3rd Party L...
How to Build Android Camera

©SIProp Project, 2006-2008

19
Fix CMakeList.txt File 1/2
[OpenCV]/modules/androidcamera/CMakeList.t
xt
Required “-fno-rtti” Option
›
›
›
›
›
›
›
›
›
›
›...
Fix CMakeList.txt File 2/2
Add 2 Options
Please change “ANDROID_VERSION” to your Version.

›
›

SET(BUILD_ANDROID_CAMERA_W...
Modify Android Camera Source Code
Android Camera Wrapper
[OpenCV
Source]modules/androidcamera/camera_wrapper/
camera_wrapp...
How to Use OpenCV for Android

©SIProp Project, 2006-2008

23
Native Application
NDK wo/ADT
Standard Android
Application for C/C++
Only C/C++ on Limited
Library Layer

Call Stack
APK F...
Use NativeActivity of OpenCV
This Sample uses Only C/C++ Code.
~/opencv-2.4.7/samples/android/native-activity

Structure o...
Edit AndroidManifest.xml
Replace “application” Section
1.

<application android:label="@string/app_name"
android:hasCode="...
About AndroidManifest.xml
API Level
1.

<!-- This is the platform API where
NativeActivity was introduced. -->
2.
<uses-sd...
Edit jni/Application.xml
Change STL Lib
1. APP_STL := gnustl_static
↓
1. APP_STL := stlport_static

API Level
1. APP_PLATF...
Edit jni/Android.mk 1/8
Delete
1. include ../../sdk/native/jni/OpenCV.mk

Set OpenCV Libs
1. OPENCV_MODULES:=contrib legac...
Edit jni/Android.mk 2/8
Make Loading OpenCV Libs Function
1. define add_opencv_module
2.
include $(CLEAR_VARS)
3.
LOCAL_MO...
Edit jni/Android.mk 3/8

Set 3rd Party Libs for OpenCV

1. OPENCV_3RDPARTY_COMPONENTS:=libjpe
g libpng libtiff libjasper I...
Edit jni/Android.mk 4/8
Make Loading OpenCV Libs Function
1. define add_opencv_3rdparty_component
2.
include $(CLEAR_VARS)...
Edit jni/Android.mk 5/8

Set AndroidCamera Libs for OpenCV
1. OPENCV_CAMERA_MODULES:=
native_camera_r2.2.0 native_camera_r...
Edit jni/Android.mk 6/8
Make Loading AndroidCamera Libs Function
1. define add_opencv_camera_module
2.
include $(CLEAR_VAR...
Edit jni/Android.mk 7/8
Add Magic Word for Reset
1. include $(CLEAR_VARS)

Set Include Dir
1.
2.

OPENCV_INCLUDE_DIR:=$(LO...
Edit jni/Android.mk 8/8
Add Loading Lib & OpenCV Lib
LOCAL_LDLIBS
-lc -ldl -lz

LOCAL_STATIC_LIBRARIES
$(foreach mod, $(OP...
Setup/Make Build.xml 1/2
Making Setting File for Android using ANT
Today, Development on CUI Only!!!
Don’t use Eclipse!
›
...
Setup/Make Build.xml 2/2
Make Build.xml
› android update project --path . --target 2

©SIProp Project, 2006-2008

38
Build NativeActivity
Build C/C++ Source Codes
› ndk-build [clean all]

Make APK File
› ant [debug|release]

©SIProp Projec...
Install NativeActivity.apk
Install APK File
› adb install bin/NativeActivity-debug.apk

UnInstall APK File
› adb uninstall...
adb(Android Debug Bridge)
Functions
Shell on Android Device
adb shell

Show Connected Android Devices
adb devices

Install...
How About NativeActivity

©SIProp Project, 2006-2008

42
Quick Explanation to NativeActivity
How to Use Camera
1.
2.
3.
4.
5.
6.
7.
8.

struct Engine
{
android_app* app;
cv::Ptr<c...
Quick Explanation to NativeActivity
Event Driven Code for Android
1.
2.
3.
4.
5.
6.

static void engine_handle_cmd(android...
LifeCycle Diagram
Activity is Event Driven Arch
Main Event
onCreate()
Start Activity
Initialize Objects

onStart()
Finish ...
But…
Some Android Camera doesn’t work…
#if !defined(ANDROID_r2_2_0) &&
!defined(ANDROID_r2_3_3) &&
!defined(ANDROID_r3_0_1...
Android Camera for Java
android.hardware.Camera
Android Framework’s Class

libcameraservice.so
libcamera_client.so
Absorpt...
Android Camera for OpenCV
Directly Call
Libcamera_client.so

Maybe Because…
They avoid overhead in
libcameraservice.so
lib...
How to Add New Driver

©SIProp Project, 2006-2008

49
Kernel/Driver Layer
Android Source Tree
Ex. Nexus7(2013)
Device
Asus
flo
flo-kernel

Hardware
Qcom
audio
bt
camera
display...
How to Add V4L for OpenCV

©SIProp Project, 2006-2008

51
Why?
OpenCV for Android doesn’t have “V4L”.
It use Android build-in Camera.

Except Phone/Table don’t have Camera…
You MUS...
Add New UVC Driver
drivers/media/video/uvc/uvc_driver.c
Add after “static struct usb_device_id uvc_ids[] = {“
1.
2.
3.
4.
...
Download & Build kernel
Download & Build kernel
› cd ~/nexus_work
› export ARCH=arm
› export
CROSS_COMPILE=~/nexus_work/an...
Change Permission
android/device/asus/flo/ueventd.flo.rc
Change “/dev/video*” Permission

› /dev/video*
camera

0666 syste...
Re-Make OTA image
Re-Make OTA image
Require “clean”, because “init.*” doesn’t update.
›
›
›
›
›

cd ~/nexus_work/android/
...
How to Build OpenCV for

V4L

Almost Same in “for Android”
Same Pages are Gray Out

©SIProp Project, 2006-2008

57
Download OpenCV & NDK tool
›

›

wget
https://github.com/Itseez/opencv/archive/
2.4.7.tar.gz
tar zxfv 2.4.7.tar.gz

› mkdi...
Edit Setup File
Add Android Source Code directory.
~/opencv2.4.7/platforms/scripts/cmake_android_service.sh
-DANDROID_SOUR...
Set Path to SDK/NDK

›
›

export ANDROID_NDK=~/android/androidndk-r8e
export ANDROID_SDK=~/android/androidsdk-linux

©SIPr...
Make Version File
Require “AndroidCamera” Func.
›

cp -v
~/nexus_work/android/development/sdk/pla
tform_source.prop_templa...
Copy some haeder files & so Libs
Require “AndroidCamera” Func.
›

cp -aR ~/android/android-ndk-r8e/platforms/android14/arc...
Include new Include File Dir
Android 4.3’s Include File Dir was separated.
~/opencv2.4.7/platforms/android/service/engine/...
Delete "DetectTegra“ Func
This Android System doesn't have "Tegra“.
~/opencv2.4.7/platforms/android/service/engine/jni/Bin...
Make Define Log Func
Android Logging System was Changed…
Make 『FakeLog.h』 file
~/opencv2.4.7/platform/android/service/engi...
Include Fake Log Header
~/opencv-2.4.7/platform/android/service/engine/jni/
BinderComponent/BnOpenCVEngine.h
BinderCompone...
Comment Out AndroidCamera 1/2
OpenCV for Android ONLY use AndroidCamera.
~/opencv-2.4.7/modules/highgui/CMakeLists.txt
Cha...
Comment Out AndroidCamera 2/2
OpenCV for Android ONLY use AndroidCamera.
~/opencv2.4.7/modules/androidcamera/CMakeLists.tx...
Comment in V4L 1/2
Enabel V4L in highgui.
~/opencv-2.4.7/modules/highgui/CMakeLists.txt
Add:

1. list(APPEND highgui_srcs ...
Comment in V4L 2/2
-DWITH_V4L=ON doesn't work...
~/opencv-2.4.7/modules/highgui/src/cap.cpp
~/opencv-2.4.7/modules/highgui...
Copy V4L Header Files
›
›
›
›

cp -v /usr/include/libv4l1.h ~/opencv2.4.7/modules/highgui/include/
cp -v /usr/include/libv...
Delete Java Lib
This Lib require AndroidCamera
› rm -fr ~/opencv-2.4.7/modules/java/

©SIProp Project, 2006-2008

72
Execute Auto Config & make
This Script make Makefile for OpenCV.
In “~/opencv2.4.7/platforms/scripts/build_android_service...
Where are Made Files?
OpenCV Libs.
In “~/opencv2.4.7/platforms/build_android_service/lib”
highgui
core
etc...

3rd Party L...
How to Use OpenCV for V4L
Almost Same in “for Android”
Same Pages are Gray Out

©SIProp Project, 2006-2008

75
Use NativeActivity of OpenCV
This Sample uses Only C/C++ Code.
~/opencv-2.4.7/samples/android/native-activity

Structure o...
Edit AndroidManifest.xml
Replace “application” Section
1.

<application android:label="@string/app_name"
android:hasCode="...
About AndroidManifest.xml
API Level
1.

<!-- This is the platform API where
NativeActivity was introduced. -->
2.
<uses-sd...
Edit jni/Application.xml
Change STL Lib
1. APP_STL := gnustl_static
↓
1. APP_STL := stlport_static

API Level
1. APP_PLATF...
Edit jni/Android.mk 1/8
Delete
1. include ../../sdk/native/jni/OpenCV.mk

Set OpenCV Libs
1. OPENCV_MODULES:=contrib legac...
Edit jni/Android.mk 2/8
Make Loading OpenCV Libs Function
1. define add_opencv_module
2.
include $(CLEAR_VARS)
3.
LOCAL_MO...
Edit jni/Android.mk 3/8

Set 3rd Party Libs for OpenCV

1. OPENCV_3RDPARTY_COMPONENTS:=libjpe
g libpng libtiff libjasper I...
Edit jni/Android.mk 4/8
Make Loading OpenCV Libs Function
1. define add_opencv_3rdparty_component
2.
include $(CLEAR_VARS)...
Edit jni/Android.mk 7/8
Add Magic Word for Reset
1. include $(CLEAR_VARS)

Set Include Dir
1.
2.

OPENCV_INCLUDE_DIR:=$(LO...
Edit jni/Android.mk 8/8
Add Loading Lib & OpenCV Lib
LOCAL_LDLIBS
-lc -ldl -lz

LOCAL_STATIC_LIBRARIES
$(foreach mod, $(OP...
Setup libv4l for Android
Download libv4l Lib for Android
›

wget
http://www.noritsuna.com/download/libv4l_f
or_android.tar...
Edit jni/Android.mk Ext-1
Add libv4l Lib in:
1. LOCAL_STATIC_LIBRARIES :=
android_native_app_glue $(foreach mod,
$(OPENCV_...
Edit jni/Android.mk Ext-2
If you edited about “AndroidCamera” Libs,
Please Delete it!
Delete AndroidCamera Libs for OpenCV...
Edit jni/Android.mk Ext-3
Delete Loading AndroidCamera Libs Function
1. define add_opencv_camera_module
2.
include $(CLEAR...
Setup/Make Build.xml 1/2
Making Setting File for Android using ANT
Today, Development on CUI Only!!!
Don’t use Eclipse!
›
...
Setup/Make Build.xml 2/2
Make Build.xml
› android update project --path . --target 2

©SIProp Project, 2006-2008

91
Build NativeActivity
Build C/C++ Source Codes
› ndk-build [clean all]

Make APK File
› ant [debug|release]

©SIProp Projec...
Install NativeActivity.apk
Install APK File
› adb install bin/NativeActivity-debug.apk

UnInstall APK File
› adb uninstall...
How to Add New Native App

©SIProp Project, 2006-2008

94
How to Develop?
As Linux Application
Non-Standard Android
Application
Full Linux C/C++ on
Library Layer

Call Stack
ELF Fi...
Make Execute File
Make Dir
hello-exe/
hello-exe/jni

Make Source Code: hello-exe/jni/hello-exe.c
1. #include <stdio.h>
2. ...
Make Execute File
Make Android.mk
1. jni/Android.mk
2. LOCAL_PATH := $(call my-dir)
3. include $(CLEAR_VARS)
4. LOCAL_MODU...
Build Execute File
Build C/C++ Source Codes
› ndk-build [clean all]

Setup & Execute Execute File
›
›
›
›
›

adb push obj/...
Setup Exe File with OTA-Build
init.*.rc in Nexus7(2013)
~/nexus_work/android/device/asus/flo/init.flo.rc

Copy your progra...
Init Script on Android
init.*.rc
Auto Run on Boot Format:
service [program name] [program path] [program option]…
[service...
Boot Up Flow Kernel~Android System
Init
Set Permission
Mount Disks
Load Driver

Demons
Linux App

Zygote
Sharing
JavaResou...
Appendix: How to Setup so File
If you want to use your so file.
How to Setup:
Copy so file to:
PRODUCT_COPY_FILES += 
exte...
Upcoming SlideShare
Loading in...5
×

How to Add Original Library to Android NDK

3,699

Published on

How to Add Original Library to Android NDK
Build OpenCV
Only C/C++ World on Android

Published in: Technology, Education
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,699
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
99
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Transcript of "How to Add Original Library to Android NDK"

  1. 1. How to Add Original Library to Android NDK Noritsuna Imamura noritsuna@siprop.org ©SIProp Project, 2006-2008 1
  2. 2. Agenda How to Add Original Library to Android NDK Build OpenCV Only C/C++ World on Android ©SIProp Project, 2006-2008 2
  3. 3. How to Add New Library ©SIProp Project, 2006-2008 3
  4. 4. Library Layer Linux Software & Linux Library aac android-clat android-mock ant-glob antlr apache-harmony apache-http apache-qp apache-xml arduino bison blktrace bluetooth bouncycastle bsdiff bzip2 ceres-solver checkpolicy chromium chromium-libpac chromium-trace chromium_org clang compiler-rt dexmaker dhcpcd dnsmasq doclava droiddriver dropbear e2fsprogs easymock eclipse-basebuilder eclipse-windowbuilder eigen elfutils embunit emma esd expat eyes-free fdlibm flac freetype fsck_msdos ganymed-ssh2 gcc-demangle genext2fs giflib google-diff-match-patch grub gtest guava hamcrest harfbuzz harfbuzz_ng hyphenation icu4c iproute2 ipsec-tools iptables iputils jack javasqlite javassist jdiff jhead jmdns jmonkeyengine jpeg jsilver jsr305 junit kernel-headers libcap-ng libffi libgsm liblzf libmtp libnfc-nci libnfc-nxp libnl-headers libogg libpcap libphonenumber libpng libppp libselinux libsepol libusb libusb-compat libvorbis libvpx libxml2 libxslt libyuv linux-tools-perf littlemock llvm lzma marisa-trie markdown mdnsresponder mesa3d mksh mockito mockwebserver mp4parser mtpd naver-fonts netcat netperf neven nist-pkits nist-sip noto-fonts oauth objenesis okhttp open-vcdiff opencv openfst openssh openssl tinycompress oprofile tinyxml pixman tinyxml2 ppp tremolo proguard v8 protobuf valgrind qemu webp qemu-pc-bios webrtc regex-re2 wpa_supplicant_8 replicaisland xmlwriter robolectric xmp_toolkit safe-iop yaffs2 scrypt zlib sepolicy zxing sfntly sil-fonts skia smack smali sonivox speex sqlite srec srtp stlport strace stressapptest svox tagsoup tcpdump timezonepicker-support tinyalsa ©SIProp Project, 2006-2008 4
  5. 5. How to Develop? NDK wo/ADT Standard Android Application for C/C++ Only C/C++ on Limited Library Layer Call Stack APK File(Your Application) (C/C++) Advantage Only C/C++ DirectCall C/C++ API Dis-Advantage Use a few Android Tools A few Docs from Google Developer Site & Blogs Call as C/C++ APIs Library Layer (C/C++) Call as SysCall(C/ASM Kernel/Driver Layer (C/ASM)Project, 2006-2008 ©SIProp 5
  6. 6. How to Build OpenCV for Android ©SIProp Project, 2006-2008 6
  7. 7. Download OpenCV & NDK tool › › wget https://github.com/Itseez/opencv/archive/ 2.4.7.tar.gz tar zxfv 2.4.7.tar.gz › mkdir ~/android/ › cd ~/android/ › wget http://dl.google.com/android/androidsdk_r22.3-linux.tgz › wget http://dl.google.com/android/ndk/androidndk-r8e-linux-x86_64.tar.bz2 › tar zxfv android-sdk_r22.3-linux.tgz › tar jxfv android-ndk-r8e-linuxx86_64.tar.bz2 ©SIProp Project, 2006-2008 7
  8. 8. Edit Setup File Add Android Source Code directory. ~/opencv2.4.7/platforms/scripts/cmake_android_service.sh -DANDROID_SOURCE_TREE=~/nexus_work/android/ › cmake DCMAKE_TOOLCHAIN_FILE=../android/android.toolchain.c make -DANDROID_TOOLCHAIN_NAME="arm-linuxandroideabi-4.4.3" -DANDROID_STL=stlport_static DANDROID_STL_FORCE_FEATURES=OFF DBUILD_ANDROID_SERVICE=ON DANDROID_SOURCE_TREE=~/Projects/AndroidSource/Se rviceStub/ $@ ../.. ©SIProp Project, 2006-2008 8
  9. 9. Set Path to SDK/NDK › › export ANDROID_NDK=~/android/androidndk-r8e export ANDROID_SDK=~/android/androidsdk-linux ©SIProp Project, 2006-2008 9
  10. 10. Make Version File Require “AndroidCamera” Func. › cp -v ~/nexus_work/android/development/sdk/pla tform_source.prop_template ~/nexus_work/android/development/sdk/pla tform_source.properties Edit Version Number › Platform.Version=4.3.1 ©SIProp Project, 2006-2008 10
  11. 11. Copy some haeder files & so Libs Require “AndroidCamera” Func. › cp -aR ~/android/android-ndk-r8e/platforms/android14/arch-arm/usr/include/EGL ~/opencv2.4.7/modules/androidcamera/include/ › cp -v ~/nexus_work/android/out/target/product/flo/system/lib/l ibbinder.so $(ANDROID_NDK)/platforms/android-8/archarm/usr/lib/ cp -v ~/nexus_work/android/out/target/product/flo/system/lib/l ibutils.so $(ANDROID_NDK)/platforms/android-8/archarm/usr/lib/ cp -v ~/nexus_work/android/out/target/product/flo/system/lib/l ibandroid_runtime.so $(ANDROID_NDK)/platforms/android8/arch-arm/usr/lib/ › › ©SIProp Project, 2006-2008 11
  12. 12. Modify Compile Option ~/opencv2.4.7/modules/androidcamera/CMakeLists.txt › › › › › › › › › › › › › › › › › function(replace_compiler_option var old new) # Replaces a compiler option or switch old in var by new. # If old is not in var, appends new to var. # Example: replace_compiler_option(CMAKE_CXX_FLAGS_RELEASE "-O3" "-O2") # If the option already is on the variable, don't add it: if( "${${var}}" MATCHES "(^| )${new}($| )" ) set(n "") else() set(n "${new}") endif() if( "${${var}}" MATCHES "(^| )${old}($| )" ) string( REGEX REPLACE "(^| )${old}($| )" " ${n} " ${var} "${${var}}" ) else() set( ${var} "${${var}} ${n}" ) endif() set( ${var} "${${var}}" PARENT_SCOPE ) endfunction(replace_compiler_option) › › replace_compiler_option(CMAKE_CXX_FLAGS "-frtti" "-fno-rtti") status("camera_wrapper C++ flags):" ${CMAKE_CXX_FLAGS}) ©SIProp Project, 2006-2008 12
  13. 13. Include new Include File Dir Android 4.3’s Include File Dir was separated. ~/opencv2.4.7/platforms/android/service/engine/CMakeLists. txt › include_directories(SYSTEM "${ANDROID_SOURCE_TREE}/frameworks/base/incl ude" "${ANDROID_SOURCE_TREE}/system/core/include" ) "${ANDROID_SOURCE_TREE}/frameworks/native/inc lude/" ©SIProp Project, 2006-2008 13
  14. 14. Delete "DetectTegra“ Func This Android System doesn't have "Tegra“. ~/opencv2.4.7/platforms/android/service/engine/jni/BinderCo mponent/TegraDetector.h › int DetectTegra(){}; ↓ › int DetectTegra(){return NOT_TEGRA;}; ©SIProp Project, 2006-2008 14
  15. 15. Make Define Log Func Android Logging System was Changed… Make 『FakeLog.h』 file ~/opencv2.4.7/platform/android/service/engine/jni/include/ 1. 2. 3. 4. 5. 6. #ifndef __FAKE_LOG_H__ #define __FAKE_LOG_H__ #define LOGD ALOGD #define LOGI ALOGI #define LOGE ALOGE #endif ©SIProp Project, 2006-2008 15
  16. 16. Include Fake Log Header ~/opencv-2.4.7/platform/android/service/engine/jni/ BinderComponent/BnOpenCVEngine.h BinderComponent/HardwareDetector.h JNIWrapper/JavaBasedPackageManager.h JNIWrapper/OpenCVLibraryInfo.h 1. #include "FakeLog.h" ©SIProp Project, 2006-2008 16
  17. 17. Execute Auto Config & make This Script make Makefile for OpenCV. In “~/opencv2.4.7/platforms/scripts/build_android_service” › cd ~/opencv-2.4.7/platforms/scripts/ › ./cmake_android_service.sh Make !!! › › cd ~/opencv2.4.7/platforms/scripts/build_android_servic e/ make ©SIProp Project, 2006-2008 17
  18. 18. Where are Made Files? OpenCV Libs. In “~/opencv2.4.7/platforms/build_android_service/lib” highgui core etc... 3rd Party Libs for OpenCV. In “~/opencv2.4.7/platforms/build_android_service/3rdparty/lib” libIlmImf libjasper libjpeg libpng libtiff ©SIProp Project, 2006-2008 18
  19. 19. How to Build Android Camera ©SIProp Project, 2006-2008 19
  20. 20. Fix CMakeList.txt File 1/2 [OpenCV]/modules/androidcamera/CMakeList.t xt Required “-fno-rtti” Option › › › › › › › › › › › › › function(replace_compiler_option var old new) if( "${${var}}" MATCHES "(^| )${new}($| )" ) set(n "") else() set(n "${new}") endif() if( "${${var}}" MATCHES "(^| )${old}($| )" ) string( REGEX REPLACE "(^| )${old}($| )" " ${n} " ${var} "${${var}}" ) else() set( ${var} "${${var}} ${n}" ) endif() set( ${var} "${${var}}" PARENT_SCOPE ) endfunction(replace_compiler_option) › › replace_compiler_option(CMAKE_CXX_FLAGS "-frtti" "-fno-rtti") ©SIProp status("camera_wrapper C++ flags):" ${CMAKE_CXX_FLAGS}) Project, 2006-2008 20
  21. 21. Fix CMakeList.txt File 2/2 Add 2 Options Please change “ANDROID_VERSION” to your Version. › › SET(BUILD_ANDROID_CAMERA_WRAPPER ON) SET(ANDROID_VERSION "4.3.0") ©SIProp Project, 2006-2008 21
  22. 22. Modify Android Camera Source Code Android Camera Wrapper [OpenCV Source]modules/androidcamera/camera_wrapper/ camera_wrapper.cpp If your camera doesn’t work, you should check these source code: Camera Service for Android Framework [Android Source]/frameworks/av/services/camera/libcameraservice / [Android Source]/frameworks/base/core/jni/ CameraHAL [Android Source]/hardware/libhardware/modules/camera/ [Android Source]/hardware/[Vendor Name]/ ©SIProp Project, 2006-2008 22
  23. 23. How to Use OpenCV for Android ©SIProp Project, 2006-2008 23
  24. 24. Native Application NDK wo/ADT Standard Android Application for C/C++ Only C/C++ on Limited Library Layer Call Stack APK File(Your Application) (C/C++) Advantage Only C/C++ DirectCall C/C++ API Dis-Advantage Use a few Android Tools A few Docs from Google Developer Site & Blogs Call as C/C++ APIs Library Layer (C/C++) Call as SysCall(C/ASM Kernel/Driver Layer (C/ASM)Project, 2006-2008 ©SIProp 24
  25. 25. Use NativeActivity of OpenCV This Sample uses Only C/C++ Code. ~/opencv-2.4.7/samples/android/native-activity Structure of NativeActivity AndroidManifest.xml <- Property File for Android res <- Directory that has Resource file(Image,Voice) jni <- Directory that has files for NDK Android.mk <- make file of NDK Application.mk <- property file of NDK native.cpp <- Sample App Other Files/Dir, don’t use. ©SIProp Project, 2006-2008 25
  26. 26. Edit AndroidManifest.xml Replace “application” Section 1. <application android:label="@string/app_name" android:hasCode="false"> 2. 3. 4. <activity android:name="android.app.NativeActivity" android:label="@string/app_name" 5. 6. 7. 8. 9. 10. 11. 12. 13. android:configChanges="orientation|keyboardHidden"> <!-- Tell NativeActivity the name of or .so --> <meta-data android:name="android.app.lib_name" android:value="native_activity" /> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> ©SIProp Project, 2006-2008 26
  27. 27. About AndroidManifest.xml API Level 1. <!-- This is the platform API where NativeActivity was introduced. --> 2. <uses-sdk android:minSdkVersion=“9" /> Permission 1. <uses-permission android:name="android.permission.CAMERA "/> ©SIProp Project, 2006-2008 27
  28. 28. Edit jni/Application.xml Change STL Lib 1. APP_STL := gnustl_static ↓ 1. APP_STL := stlport_static API Level 1. APP_PLATFORM := android-9 ©SIProp Project, 2006-2008 28
  29. 29. Edit jni/Android.mk 1/8 Delete 1. include ../../sdk/native/jni/OpenCV.mk Set OpenCV Libs 1. OPENCV_MODULES:=contrib legacy ml stitching objdetect ts videostab video photo calib3d features2d highgui imgproc flann core androidcamera ©SIProp Project, 2006-2008 29
  30. 30. Edit jni/Android.mk 2/8 Make Loading OpenCV Libs Function 1. define add_opencv_module 2. include $(CLEAR_VARS) 3. LOCAL_MODULE:=opencv_$1 4. LOCAL_SRC_FILES:=../../opencv2.4.7/platforms/build_android_service/lib/ar meabi-v7a/libopencv_$1.a 5. include $(PREBUILT_STATIC_LIBRARY) 6. endef 1. $(foreach module,$(OPENCV_MODULES),$(eval $(call add_opencv_module,$(module)))) ©SIProp Project, 2006-2008 30
  31. 31. Edit jni/Android.mk 3/8 Set 3rd Party Libs for OpenCV 1. OPENCV_3RDPARTY_COMPONENTS:=libjpe g libpng libtiff libjasper IlmImf ©SIProp Project, 2006-2008 31
  32. 32. Edit jni/Android.mk 4/8 Make Loading OpenCV Libs Function 1. define add_opencv_3rdparty_component 2. include $(CLEAR_VARS) 3. LOCAL_MODULE:=$1 4. LOCAL_SRC_FILES:=../../opencv2.4.7/platforms/build_android_service/3rdpar ty/lib/armeabi-v7a/lib$1.a 5. include $(PREBUILT_STATIC_LIBRARY) 6. endef 1. $(foreach module,$(OPENCV_3RDPARTY_COMPONEN TS),$(eval $(call add_opencv_3rdparty_component,$(module)))) ©SIProp Project, 2006-2008 32
  33. 33. Edit jni/Android.mk 5/8 Set AndroidCamera Libs for OpenCV 1. OPENCV_CAMERA_MODULES:= native_camera_r2.2.0 native_camera_r2.3.3 native_camera_r3.0.1 native_camera_r4.0.0 native_camera_r4.0.3 native_camera_r4.1.1 native_camera_r4.2.0 native_camera_r4.3.0 ©SIProp Project, 2006-2008 33
  34. 34. Edit jni/Android.mk 6/8 Make Loading AndroidCamera Libs Function 1. define add_opencv_camera_module 2. include $(CLEAR_VARS) 3. LOCAL_MODULE:=$1 4. LOCAL_SRC_FILES:=../../opencv2.4.7/platforms/build_android_service/lib/ar meabi-v7a/lib$1.so 5. include $(PREBUILT_SHARED_LIBRARY) 6. endef 1. $(foreach module,$(OPENCV_CAMERA_MODULES),$(e val $(call add_opencv_camera_module,$(module)))) ©SIProp Project, 2006-2008 34
  35. 35. Edit jni/Android.mk 7/8 Add Magic Word for Reset 1. include $(CLEAR_VARS) Set Include Dir 1. 2. OPENCV_INCLUDE_DIR:=$(LOCAL_DIR)/../opencv-2.4.7/include LOCAL_C_INCLUDES+=$(OPENCV_INCLUDE_DIR)/opencv $(OPENCV_INCLUDE_DIR)/opencv2 ../opencv2.4.7/modules/stitching/include ../opencv-2.4.7/modules/objdetect/include ../opencv2.4.7/modules/photo/include ../opencv-2.4.7/modules/flann/include ../opencv2.4.7/modules/core/include ../opencv-2.4.7/modules/ts/include ../opencv2.4.7/modules/highgui/include ../opencv-2.4.7/modules/contrib/include ../opencv2.4.7/modules/video/include ../opencv-2.4.7/modules/imgproc/include ../opencv2.4.7/modules/legacy/include ../opencv-2.4.7/modules/ocl/include ../opencv2.4.7/modules/features2d/include ../opencv-2.4.7/modules/calib3d/include ../opencv2.4.7/modules/ml/include ../opencv-2.4.7/modules/videostab ../opencv2.4.7/modules/nonfree/include ../opencv-2.4.7/modules/androidcamera/include ©SIProp Project, 2006-2008 35
  36. 36. Edit jni/Android.mk 8/8 Add Loading Lib & OpenCV Lib LOCAL_LDLIBS -lc -ldl -lz LOCAL_STATIC_LIBRARIES $(foreach mod, $(OPENCV_MODULES), opencv_$(mod)) $(OPENCV_3RDPARTY_COMPONENTS) 1. LOCAL_LDLIBS += -lm -llog -landroid -lc -ldl -lz 2. LOCAL_STATIC_LIBRARIES := android_native_app_glue $(foreach mod, $(OPENCV_MODULES), opencv_$(mod)) $(OPENCV_3RDPARTY_COMPONENTS) ©SIProp Project, 2006-2008 36
  37. 37. Setup/Make Build.xml 1/2 Making Setting File for Android using ANT Today, Development on CUI Only!!! Don’t use Eclipse! › android list target › › › › › › › › › › › › › › › › › › Available Android targets: ---------id: 1 or "android-9" Name: Android 2.3.1 Type: Platform API level: 9 Revision: 2 Skins: WQVGA400, QVGA, HVGA, WVGA800 (default), WVGA854 ABIs : armeabi ---------id: 2 or "android-18" Name: Android 4.3 Type: Platform API level: 18 Revision: 2 Skins: WXGA800, WQVGA400, QVGA, WQVGA432, WXGA800-7in, WVGA800 (default), HVGA, WXGA720, WSVGA, WVGA854 ABIs : armeabi-v7a ---------©SIProp Project, 2006-2008 37
  38. 38. Setup/Make Build.xml 2/2 Make Build.xml › android update project --path . --target 2 ©SIProp Project, 2006-2008 38
  39. 39. Build NativeActivity Build C/C++ Source Codes › ndk-build [clean all] Make APK File › ant [debug|release] ©SIProp Project, 2006-2008 39
  40. 40. Install NativeActivity.apk Install APK File › adb install bin/NativeActivity-debug.apk UnInstall APK File › adb uninstall com.example.native_activity Show Log › adb logcat ©SIProp Project, 2006-2008 40
  41. 41. adb(Android Debug Bridge) Functions Shell on Android Device adb shell Show Connected Android Devices adb devices Install/Uninstall APK File adb install/uninstall [App Name]/[Package Name] Send/Get File to/from Android Device adb push/pull [from File Path] [to File Path] Show Log (DDMS) adb logcat ©SIProp Project, 2006-2008 41
  42. 42. How About NativeActivity ©SIProp Project, 2006-2008 42
  43. 43. Quick Explanation to NativeActivity How to Use Camera 1. 2. 3. 4. 5. 6. 7. 8. struct Engine { android_app* app; cv::Ptr<cv::VideoCapture> capture; }; (snip) cv::Mat drawing_frame; (snip) 9. engine.capture->retrieve(drawing_frame, CV_CAP_ANDROID_COLOR_FRAME_RGBA); 10. 11. char buffer[256]; sprintf(buffer, "Display performance: %dx%d @ %.3f", drawing_frame.cols, drawing_frame.rows, fps); cv::putText(drawing_frame, std::string(buffer), cv::Point(8,64), cv::FONT_HERSHEY_COMPLEX_SMALL, 1, cv::Scalar(0,255,0,255)); engine_draw_frame(&engine, drawing_frame); 12. 13. 14. ©SIProp Project, 2006-2008 43
  44. 44. Quick Explanation to NativeActivity Event Driven Code for Android 1. 2. 3. 4. 5. 6. static void engine_handle_cmd(android_app* app, int32_t cmd) { Engine* engine = (Engine*)app->userData; switch (cmd) { (snip) 7. 8. 9. void android_main(android_app* app) { Engine engine; 10. 11. 12. 13. 14. 15. 16. 17. // Make sure glue isn't stripped. app_dummy(); size_t engine_size = sizeof(engine); memset((void*)&engine, 0, engine_size); app->userData = &engine; app->onAppCmd = engine_handle_cmd; engine.app = app; (snip) ©SIProp Project, 2006-2008 44
  45. 45. LifeCycle Diagram Activity is Event Driven Arch Main Event onCreate() Start Activity Initialize Objects onStart() Finish Initialized onPause() Other Activity Start onResume() Back from Other Activity onStop() Don’t back long time ©SIProp Project, 2006-2008 45
  46. 46. But… Some Android Camera doesn’t work… #if !defined(ANDROID_r2_2_0) && !defined(ANDROID_r2_3_3) && !defined(ANDROID_r3_0_1) && !defined(ANDROID_r4_0_0) && !defined(ANDROID_r4_0_3) && !defined(ANDROID_r4_1_1) && !defined(ANDROID_r4_2_0) && !defined(ANDROID_r4_3_0) # error Building camera wrapper for your version of Android is not supported by OpenCV. You need to modify OpenCV sources in order to compile camera wrapper for your version of Android. #endif ©SIProp Project, 2006-2008 46
  47. 47. Android Camera for Java android.hardware.Camera Android Framework’s Class libcameraservice.so libcamera_client.so Absorption for Camera CameraHardwareInterface Hardware Camera android.hardware.Camera (Java) Call as Android API libCameraService.so (C/C++) Call as Shared Lib libCamera_Client.so (C/C++) Call as ko Lib CameraHardwareInterface (C/ASM) ©SIProp Project, 2006-2008 47
  48. 48. Android Camera for OpenCV Directly Call Libcamera_client.so Maybe Because… They avoid overhead in libcameraservice.so libcamera_client.so However… Their Coding is original, Some Devices don’t work. native_camera_rx.x.x.so (C/C++) Call as Shared Lib libCamera_Client.so (C/C++) Call as ko Lib CameraHardwareInterface (C/ASM) ©SIProp Project, 2006-2008 48
  49. 49. How to Add New Driver ©SIProp Project, 2006-2008 49
  50. 50. Kernel/Driver Layer Android Source Tree Ex. Nexus7(2013) Device Asus flo flo-kernel Hardware Qcom audio bt camera display keymaster media msm8960 msm8x74 power sensors wlan ©SIProp Project, 2006-2008 50
  51. 51. How to Add V4L for OpenCV ©SIProp Project, 2006-2008 51
  52. 52. Why? OpenCV for Android doesn’t have “V4L”. It use Android build-in Camera. Except Phone/Table don’t have Camera… You MUST customize OpenCV for Android. ©SIProp Project, 2006-2008 52
  53. 53. Add New UVC Driver drivers/media/video/uvc/uvc_driver.c Add after “static struct usb_device_id uvc_ids[] = {“ 1. 2. 3. 4. 5. 6. 7. 8. 9. /* Etron Tech */ { .match_flags = USB_DEVICE_ID_MATCH_DEVICE | USB_DEVICE_ID_MATCH_INT_INFO, .idVendor = 0x1e4e, .idProduct = 0x0104, .bInterfaceClass = USB_CLASS_VIDEO, .bInterfaceSubClass = 1, .bInterfaceProtocol = 0, .driver_info = UVC_QUIRK_PROBE_MINMAX }, ©SIProp Project, 2006-2008 53
  54. 54. Download & Build kernel Download & Build kernel › cd ~/nexus_work › export ARCH=arm › export CROSS_COMPILE=~/nexus_work/android/prebuilts/gc c/linux-x86/arm/arm-eabi-4.6/bin/arm-eabi› git clone https://android.googlesource.com/kernel/msm.git -b android-msm-flo-3.4-jb-mr2 kernel/nexus72 › cd kernel/nexus72 › make flo_defconfig › make menuconfig › make › cp arch/arm/boot/zImage ~/nexus_work/android/device/asus/flo-kernel/kernel ©SIProp Project, 2006-2008 54
  55. 55. Change Permission android/device/asus/flo/ueventd.flo.rc Change “/dev/video*” Permission › /dev/video* camera 0666 system ©SIProp Project, 2006-2008 55
  56. 56. Re-Make OTA image Re-Make OTA image Require “clean”, because “init.*” doesn’t update. › › › › › cd ~/nexus_work/android/ source build/envsetup.sh lunch aosp_flo-user make clean make otapackage This file name is aosp_flo-ota-eng.[your Linux’s User name].zip ©SIProp Project, 2006-2008 56
  57. 57. How to Build OpenCV for V4L Almost Same in “for Android” Same Pages are Gray Out ©SIProp Project, 2006-2008 57
  58. 58. Download OpenCV & NDK tool › › wget https://github.com/Itseez/opencv/archive/ 2.4.7.tar.gz tar zxfv 2.4.7.tar.gz › mkdir ~/android/ › cd ~/android/ › wget http://dl.google.com/android/androidsdk_r22.3-linux.tgz › wget http://dl.google.com/android/ndk/androidndk-r8e-linux-x86_64.tar.bz2 › tar zxfv android-sdk_r22.3-linux.tgz › tar jxfv android-ndk-r8e-linuxx86_64.tar.bz2 ©SIProp Project, 2006-2008 58
  59. 59. Edit Setup File Add Android Source Code directory. ~/opencv2.4.7/platforms/scripts/cmake_android_service.sh -DANDROID_SOURCE_TREE=~/nexus_work/android/ › cmake DCMAKE_TOOLCHAIN_FILE=../android/android.toolchain.c make -DANDROID_TOOLCHAIN_NAME="arm-linuxandroideabi-4.4.3" -DANDROID_STL=stlport_static DANDROID_STL_FORCE_FEATURES=OFF DBUILD_ANDROID_SERVICE=ON DANDROID_SOURCE_TREE=~/Projects/AndroidSource/Se rviceStub/ $@ ../.. ©SIProp Project, 2006-2008 59
  60. 60. Set Path to SDK/NDK › › export ANDROID_NDK=~/android/androidndk-r8e export ANDROID_SDK=~/android/androidsdk-linux ©SIProp Project, 2006-2008 60
  61. 61. Make Version File Require “AndroidCamera” Func. › cp -v ~/nexus_work/android/development/sdk/pla tform_source.prop_template ~/nexus_work/android/development/sdk/pla tform_source.properties Edit Version Number › Platform.Version=4.3.1 ©SIProp Project, 2006-2008 61
  62. 62. Copy some haeder files & so Libs Require “AndroidCamera” Func. › cp -aR ~/android/android-ndk-r8e/platforms/android14/arch-arm/usr/include/EGL ~/opencv2.4.7/modules/androidcamera/include/ › cp -v ~/nexus_work/android/out/target/product/flo/system/lib/l ibbinder.so $(ANDROID_NDK)/platforms/android-8/archarm/usr/lib/ cp -v ~/nexus_work/android/out/target/product/flo/system/lib/l ibutils.so $(ANDROID_NDK)/platforms/android-8/archarm/usr/lib/ cp -v ~/nexus_work/android/out/target/product/flo/system/lib/l ibandroid_runtime.so $(ANDROID_NDK)/platforms/android8/arch-arm/usr/lib/ › › ©SIProp Project, 2006-2008 62
  63. 63. Include new Include File Dir Android 4.3’s Include File Dir was separated. ~/opencv2.4.7/platforms/android/service/engine/CMakeLists. txt › include_directories(SYSTEM "${ANDROID_SOURCE_TREE}/frameworks/base/incl ude" "${ANDROID_SOURCE_TREE}/system/core/include" ) "${ANDROID_SOURCE_TREE}/frameworks/native/inc lude/" ©SIProp Project, 2006-2008 63
  64. 64. Delete "DetectTegra“ Func This Android System doesn't have "Tegra“. ~/opencv2.4.7/platforms/android/service/engine/jni/BinderCo mponent/TegraDetector.h › int DetectTegra(){}; ↓ › int DetectTegra(){return NOT_TEGRA;}; ©SIProp Project, 2006-2008 64
  65. 65. Make Define Log Func Android Logging System was Changed… Make 『FakeLog.h』 file ~/opencv2.4.7/platform/android/service/engine/jni/include/ 1. 2. 3. 4. 5. 6. #ifndef __FAKE_LOG_H__ #define __FAKE_LOG_H__ #define LOGD ALOGD #define LOGI ALOGI #define LOGE ALOGE #endif ©SIProp Project, 2006-2008 65
  66. 66. Include Fake Log Header ~/opencv-2.4.7/platform/android/service/engine/jni/ BinderComponent/BnOpenCVEngine.h BinderComponent/HardwareDetector.h JNIWrapper/JavaBasedPackageManager.h JNIWrapper/OpenCVLibraryInfo.h 1. #include "FakeLog.h" ©SIProp Project, 2006-2008 66
  67. 67. Comment Out AndroidCamera 1/2 OpenCV for Android ONLY use AndroidCamera. ~/opencv-2.4.7/modules/highgui/CMakeLists.txt Change: 1. ocv_add_module(highgui opencv_imgproc OPTIONAL opencv_androidcamera) ↓ 1. ocv_add_module(highgui opencv_imgproc) Delete: 1. list(APPEND highgui_srcs src/cap_android.cpp) ©SIProp Project, 2006-2008 67
  68. 68. Comment Out AndroidCamera 2/2 OpenCV for Android ONLY use AndroidCamera. ~/opencv2.4.7/modules/androidcamera/CMakeLists.txt Delete: 1. ocv_module_disable(androidcamera) ©SIProp Project, 2006-2008 68
  69. 69. Comment in V4L 1/2 Enabel V4L in highgui. ~/opencv-2.4.7/modules/highgui/CMakeLists.txt Add: 1. list(APPEND highgui_srcs src/cap_libv4l.cpp) 2. list(APPEND highgui_srcs src/cap_v4l.cpp) ~/opencv-2.4.7/modules/highgui/src/cap.cpp Add in under Functions: “CV_IMPL CvCapture * cvCreateCameraCapture (int index)” 1. return cvCreateCameraCapture_V4L (index); ©SIProp Project, 2006-2008 69
  70. 70. Comment in V4L 2/2 -DWITH_V4L=ON doesn't work... ~/opencv-2.4.7/modules/highgui/src/cap.cpp ~/opencv-2.4.7/modules/highgui/src/cap_libv4l.cpp 1. #define HAVE_CAMV4L 2. #define HAVE_CAMV4L2 3. #define HAVE_LIBV4L ©SIProp Project, 2006-2008 70
  71. 71. Copy V4L Header Files › › › › cp -v /usr/include/libv4l1.h ~/opencv2.4.7/modules/highgui/include/ cp -v /usr/include/libv4l2.h ~/opencv2.4.7/modules/highgui/include/ cp -v /usr/include/libv4lconvert.h ~/opencv-2.4.7/modules/highgui/include/ cp -v /usr/include/libv4l1-videodev.h ~/opencv-2.4.7/modules/highgui/include/ ©SIProp Project, 2006-2008 71
  72. 72. Delete Java Lib This Lib require AndroidCamera › rm -fr ~/opencv-2.4.7/modules/java/ ©SIProp Project, 2006-2008 72
  73. 73. Execute Auto Config & make This Script make Makefile for OpenCV. In “~/opencv2.4.7/platforms/scripts/build_android_service” › cd ~/opencv-2.4.7/platforms/scripts/ › ./cmake_android_service.sh Make !!! › › cd ~/opencv2.4.7/platforms/scripts/build_android_servic e/ make ©SIProp Project, 2006-2008 73
  74. 74. Where are Made Files? OpenCV Libs. In “~/opencv2.4.7/platforms/build_android_service/lib” highgui core etc... 3rd Party Libs for OpenCV. In “~/opencv2.4.7/platforms/build_android_service/3rdparty/lib” libIlmImf libjasper libjpeg libpng libtiff ©SIProp Project, 2006-2008 74
  75. 75. How to Use OpenCV for V4L Almost Same in “for Android” Same Pages are Gray Out ©SIProp Project, 2006-2008 75
  76. 76. Use NativeActivity of OpenCV This Sample uses Only C/C++ Code. ~/opencv-2.4.7/samples/android/native-activity Structure of NativeActivity AndroidManifest.xml <- Property File for Android res <- Directory that has Resource file(Image,Voice) jni <- Directory that has files for NDK Android.mk <- make file of NDK Application.mk <- property file of NDK native.cpp <- Sample App Other Files/Dir, don’t use. ©SIProp Project, 2006-2008 76
  77. 77. Edit AndroidManifest.xml Replace “application” Section 1. <application android:label="@string/app_name" android:hasCode="false"> 2. 3. 4. <activity android:name="android.app.NativeActivity" android:label="@string/app_name" 5. 6. 7. 8. 9. 10. 11. 12. 13. android:configChanges="orientation|keyboardHidden"> <!-- Tell NativeActivity the name of or .so --> <meta-data android:name="android.app.lib_name" android:value="native_activity" /> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> ©SIProp Project, 2006-2008 77
  78. 78. About AndroidManifest.xml API Level 1. <!-- This is the platform API where NativeActivity was introduced. --> 2. <uses-sdk android:minSdkVersion=“9" /> Permission 1. <uses-permission android:name="android.permission.CAMERA "/> ©SIProp Project, 2006-2008 78
  79. 79. Edit jni/Application.xml Change STL Lib 1. APP_STL := gnustl_static ↓ 1. APP_STL := stlport_static API Level 1. APP_PLATFORM := android-9 ©SIProp Project, 2006-2008 79
  80. 80. Edit jni/Android.mk 1/8 Delete 1. include ../../sdk/native/jni/OpenCV.mk Set OpenCV Libs 1. OPENCV_MODULES:=contrib legacy ml stitching objdetect ts videostab video photo calib3d features2d highgui imgproc flann core androidcamera ©SIProp Project, 2006-2008 80
  81. 81. Edit jni/Android.mk 2/8 Make Loading OpenCV Libs Function 1. define add_opencv_module 2. include $(CLEAR_VARS) 3. LOCAL_MODULE:=opencv_$1 4. LOCAL_SRC_FILES:=../../opencv2.4.7/platforms/build_android_service/lib/ar meabi-v7a/libopencv_$1.a 5. include $(PREBUILT_STATIC_LIBRARY) 6. endef 1. $(foreach module,$(OPENCV_MODULES),$(eval $(call add_opencv_module,$(module)))) ©SIProp Project, 2006-2008 81
  82. 82. Edit jni/Android.mk 3/8 Set 3rd Party Libs for OpenCV 1. OPENCV_3RDPARTY_COMPONENTS:=libjpe g libpng libtiff libjasper IlmImf ©SIProp Project, 2006-2008 82
  83. 83. Edit jni/Android.mk 4/8 Make Loading OpenCV Libs Function 1. define add_opencv_3rdparty_component 2. include $(CLEAR_VARS) 3. LOCAL_MODULE:=$1 4. LOCAL_SRC_FILES:=../../opencv2.4.7/platforms/build_android_service/3rdpar ty/lib/armeabi-v7a/lib$1.a 5. include $(PREBUILT_STATIC_LIBRARY) 6. endef 1. $(foreach module,$(OPENCV_3RDPARTY_COMPONEN TS),$(eval $(call add_opencv_3rdparty_component,$(module)))) ©SIProp Project, 2006-2008 83
  84. 84. Edit jni/Android.mk 7/8 Add Magic Word for Reset 1. include $(CLEAR_VARS) Set Include Dir 1. 2. OPENCV_INCLUDE_DIR:=$(LOCAL_DIR)/../opencv-2.4.7/include LOCAL_C_INCLUDES+=$(OPENCV_INCLUDE_DIR)/opencv $(OPENCV_INCLUDE_DIR)/opencv2 ../opencv2.4.7/modules/stitching/include ../opencv-2.4.7/modules/objdetect/include ../opencv2.4.7/modules/photo/include ../opencv-2.4.7/modules/flann/include ../opencv2.4.7/modules/core/include ../opencv-2.4.7/modules/ts/include ../opencv2.4.7/modules/highgui/include ../opencv-2.4.7/modules/contrib/include ../opencv2.4.7/modules/video/include ../opencv-2.4.7/modules/imgproc/include ../opencv2.4.7/modules/legacy/include ../opencv-2.4.7/modules/ocl/include ../opencv2.4.7/modules/features2d/include ../opencv-2.4.7/modules/calib3d/include ../opencv2.4.7/modules/ml/include ../opencv-2.4.7/modules/videostab ../opencv2.4.7/modules/nonfree/include ../opencv-2.4.7/modules/androidcamera/include ©SIProp Project, 2006-2008 84
  85. 85. Edit jni/Android.mk 8/8 Add Loading Lib & OpenCV Lib LOCAL_LDLIBS -lc -ldl -lz LOCAL_STATIC_LIBRARIES $(foreach mod, $(OPENCV_MODULES), opencv_$(mod)) $(OPENCV_3RDPARTY_COMPONENTS) 1. LOCAL_LDLIBS += -lm -llog -landroid -lc -ldl -lz 2. LOCAL_STATIC_LIBRARIES := android_native_app_glue $(foreach mod, $(OPENCV_MODULES), opencv_$(mod)) $(OPENCV_3RDPARTY_COMPONENTS) ©SIProp Project, 2006-2008 85
  86. 86. Setup libv4l for Android Download libv4l Lib for Android › wget http://www.noritsuna.com/download/libv4l_f or_android.tar.gz Set libv4l Lib to NDK UnPacked & Copy to: 1. $(ANDROID_NDK)/source/opencv/ ©SIProp Project, 2006-2008 86
  87. 87. Edit jni/Android.mk Ext-1 Add libv4l Lib in: 1. LOCAL_STATIC_LIBRARIES := android_native_app_glue $(foreach mod, $(OPENCV_MODULES), opencv_$(mod)) $(OPENCV_3RDPARTY_COMPONENTS) opencv_libv4l Add import-module Func in Last Line 1. $(call import-module,opencv/libv4l) ©SIProp Project, 2006-2008 87
  88. 88. Edit jni/Android.mk Ext-2 If you edited about “AndroidCamera” Libs, Please Delete it! Delete AndroidCamera Libs for OpenCV 1. OPENCV_CAMERA_MODULES:= native_camera_r2.2.0 native_camera_r2.3.3 native_camera_r3.0.1 native_camera_r4.0.0 native_camera_r4.0.3 native_camera_r4.1.1 native_camera_r4.2.0 native_camera_r4.3.0 ©SIProp Project, 2006-2008 88
  89. 89. Edit jni/Android.mk Ext-3 Delete Loading AndroidCamera Libs Function 1. define add_opencv_camera_module 2. include $(CLEAR_VARS) 3. LOCAL_MODULE:=$1 4. LOCAL_SRC_FILES:=../../opencv2.4.7/platforms/build_android_service/lib/ar meabi-v7a/lib$1.so 5. include $(PREBUILT_SHARED_LIBRARY) 6. endef 1. $(foreach module,$(OPENCV_CAMERA_MODULES),$(e val $(call add_opencv_camera_module,$(module)))) ©SIProp Project, 2006-2008 89
  90. 90. Setup/Make Build.xml 1/2 Making Setting File for Android using ANT Today, Development on CUI Only!!! Don’t use Eclipse! › android list target › › › › › › › › › › › › › › › › › › Available Android targets: ---------id: 1 or "android-9" Name: Android 2.3.1 Type: Platform API level: 9 Revision: 2 Skins: WQVGA400, QVGA, HVGA, WVGA800 (default), WVGA854 ABIs : armeabi ---------id: 2 or "android-18" Name: Android 4.3 Type: Platform API level: 18 Revision: 2 Skins: WXGA800, WQVGA400, QVGA, WQVGA432, WXGA800-7in, WVGA800 (default), HVGA, WXGA720, WSVGA, WVGA854 ABIs : armeabi-v7a ---------©SIProp Project, 2006-2008 90
  91. 91. Setup/Make Build.xml 2/2 Make Build.xml › android update project --path . --target 2 ©SIProp Project, 2006-2008 91
  92. 92. Build NativeActivity Build C/C++ Source Codes › ndk-build [clean all] Make APK File › ant [debug|release] ©SIProp Project, 2006-2008 92
  93. 93. Install NativeActivity.apk Install APK File › adb install bin/NativeActivity-debug.apk UnInstall APK File › adb uninstall com.example.native_activity Show Log › adb logcat ©SIProp Project, 2006-2008 93
  94. 94. How to Add New Native App ©SIProp Project, 2006-2008 94
  95. 95. How to Develop? As Linux Application Non-Standard Android Application Full Linux C/C++ on Library Layer Call Stack ELF File(Your Application) (C/C++) Advantage Use Other Linux Libs Dis-Advantage Must Modify Android System How to Modify on next Lecture Call as C/C++ APIs Library Layer + Linux Libs (C/C++) Call as SysCall(C/ASM Kernel/Driver Layer (C/ASM)Project, 2006-2008 ©SIProp 95
  96. 96. Make Execute File Make Dir hello-exe/ hello-exe/jni Make Source Code: hello-exe/jni/hello-exe.c 1. #include <stdio.h> 2. int main(int argc, char ** argv) { 3. printf("Hello, world!n"); 4. return 0; 5. } ©SIProp Project, 2006-2008 96
  97. 97. Make Execute File Make Android.mk 1. jni/Android.mk 2. LOCAL_PATH := $(call my-dir) 3. include $(CLEAR_VARS) 4. LOCAL_MODULE := hello-exe 5. LOCAL_SRC_FILES := hello-exe.c 6. include $(BUILD_EXECUTABLE) ©SIProp Project, 2006-2008 97
  98. 98. Build Execute File Build C/C++ Source Codes › ndk-build [clean all] Setup & Execute Execute File › › › › › adb push obj/local/armeabi/hello-exe /bin/ adb shell $ cd bin $ chmod +x hello-exe ./hello-exe Required “root permission” ©SIProp Project, 2006-2008 98
  99. 99. Setup Exe File with OTA-Build init.*.rc in Nexus7(2013) ~/nexus_work/android/device/asus/flo/init.flo.rc Copy your program to OTA-Package ~/nexus_work/android/build/target/product/mini.mk PRODUCT_COPY_FILES += [local program path]:[system path] Ex. PRODUCT_COPY_FILES += external/myapp/bin/myapp:system/usr/bin/myapp ©SIProp Project, 2006-2008 99
  100. 100. Init Script on Android init.*.rc Auto Run on Boot Format: service [program name] [program path] [program option]… [service option] . Ex: service irsc_util /system/bin/logwrapper /system/bin/irsc_util "/etc/sec_config" class main <- service group user root <- run user or [group xxx] oneshot <- run once Change user/group/permission chown/chmod Ex: chmod 0777 /bin/myapp ©SIProp Project, 2006-2008 100
  101. 101. Boot Up Flow Kernel~Android System Init Set Permission Mount Disks Load Driver Demons Linux App Zygote Sharing JavaResource Manager Runtime Linux SO Driver ©SIProp Project, 2006-2008 101
  102. 102. Appendix: How to Setup so File If you want to use your so file. How to Setup: Copy so file to: PRODUCT_COPY_FILES += external/myso/bin/libmyso.so:system/lib/libmyso.so How to Use: With NDK. In “Android.mk”: LOCAL_LDLIBS := -lmyso ©SIProp Project, 2006-2008 102
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×