Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
How to Make Android Native Application
Noritsuna Imamura
noritsuna@siprop.org

©SIProp Project, 2006-2008

1
Agenda
How to Make NativeActivity Application
NativeActivityGlue

How to Draw Something
How to Use Library(OpenCV)

How to...
How to Make Native Application
with Eclipse

©SIProp Project, 2006-2008

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

Call Stack
APK F...
Make Android Project

©SIProp Project, 2006-2008

5
Setup App Name & Target API

©SIProp Project, 2006-2008

6
Enable JNI

©SIProp Project, 2006-2008

7
Enable JNI

©SIProp Project, 2006-2008

8
SampleNativeActivity.cpp

1. #include <jni.h>

©SIProp Project, 2006-2008

9
Android.mk

1. LOCAL_PATH := $(call my-dir)
2. include $(CLEAR_VARS)
3. LOCAL_MODULE := SampleNativeActivity
4. LOCAL_SRC_...
LifeCycle Diagram
Activity is Event Driven Arch
Main Event
onCreate()
Start Activity
Initialize Objects

onStart()
Finish ...
LifeCycle in NativeActivity
[ndk dir]/platforms/android-19/archarm/usr/include/android
native-activity.h
1.
2.
3.
4.
5.
6....
NativeActivity CallBacks
NativeActivity

Effect

onStart

= Activity.onStart()

onResume

= Activity.onResume()

onSaveIns...
Problem Point
You MUST write it as Non-Block Function.
In other word, you MUST use “Thread”.
Thread Programing is sooooo h...
NativeActivityGlue CallBacks
NativeActivity

NativeActivityGlue

onStart

APP_CMD_START

onResume

APP_CMD_RESUME

onSaveI...
NativeActivityGlue
[NDK dir]/sources/android/native_app_glue
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18...
Sample of NativeActivityGlue
Don’t Need to Use “thread” Funcs.

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.

static void engine_handle_...
Functions that you CAN USE
Build-in Libs: How to Use: -lxx
[NDK DIR]/platforms/android-19/arch-arm/usr/lib
libandroid.so
N...
Edit jni/Android.mk 8/8
Add Loading Lib & OpenCV Lib
LOCAL_LDLIBS
-lc -ldl -lz

LOCAL_STATIC_LIBRARIES
$(foreach mod, $(OP...
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 6/8
Make Loading AndroidCamera Libs Function
1. define add_opencv_camera_module
2.
include $(CLEAR_VAR...
How to Use NativeActivityGlue

©SIProp Project, 2006-2008

22
Edit AndroidManifest.xml
Replace “application” Section
1.

<application android:label="@string/app_name"
android:hasCode="...
CPP Code of Basicary Format
1.

#include <android_native_app_glue.h>

2. void android_main() {
3.
4.
// Init Parameters
5....
Show Android Event
jni/SampleNativeActivity.cpp
#include <android_native_app_glue.h>
#include <android/log.h>

#define LOG...
Show Android Event
jni/SampleNativeActivity.cpp
struct Engine {
struct android_app* app;
};

void android_main(android_app...
Show Android Event
jni/SampleNativeActivity.cpp
// Loop
while(1) {
// Read all pending events.
int ident;
int events;
andr...
Show Android Event
jni/SampleNativeActivity.cpp
static void engine_handle_cmd(android_app* app, int32_t cmd) {
Engine* eng...
Show Android Event
Add the Red Lines to Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := Samp...
©SIProp Project, 2006-2008

30
How to Draw Text

©SIProp Project, 2006-2008

31
How to Draw Text?
NDK doesn’t have this function!!!

You CAN USE Bitmap File Only!!!
Use Open GL/ES
Use OpenCV
.

©SIProp ...
How to Draw Text with OpenCV

©SIProp Project, 2006-2008

33
Setup OpenCV Libs
Libs
libs_opencv

include Files
jni/include

©SIProp Project, 2006-2008

34
Edit jni/Android.mk 1/5
After “LOCAL_PATH := $(call my-dir)” Line

1.

include $(CLEAR_VARS)

2.

OPENCV_MODULES:=contrib ...
Edit jni/Android.mk 2/5

1.
2.
3.
4.
5.
6.

define add_opencv_module
include $(CLEAR_VARS)
LOCAL_MODULE:=opencv_$1
LOCAL_S...
Edit jni/Android.mk 3/5

1.
2.
3.
4.

include $(CLEAR_VARS)
LOCAL_MODULE:=opencv_info
LOCAL_SRC_FILES:=../libs_opencv/libo...
Edit jni/Android.mk 4/5

1.
2.

OPENCV_INCLUDE_DIR:=$(LOCAL_DIR)/include
LOCAL_C_INCLUDES+=$(OPENCV_INCLUDE_DIR)

©SIProp ...
Edit jni/Android.mk 5/5
Add Loading Lib & OpenCV Lib
LOCAL_LDLIBS
-lm -lc -ldl -lz

LOCAL_STATIC_LIBRARIES
$(foreach mod, ...
Application.mk
Create “Application.mk”

1.
2.
3.
4.

APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI ...
SampleNativeActivity.cpp
Init Buffer
case APP_CMD_INIT_WINDOW:
LOGI("APP_CMD_INIT_WINDOW");
{
int view_width = ANativeWind...
SampleNativeActivity.cpp
New Engine

1.
2.

struct Engine {
struct android_app* app;

3.
4.

int init_window;
};

©SIProp ...
1.
static void engine_draw_frame(struct Engine*
SampleNativeActivity.cpp engine, const cv::Mat&
frame) {
2.
3.
4.
5.

if (...
SampleNativeActivity.cpp

1.
2.
3.
4.
5.
6.

// Loop
while(1) {
// Read all pending events.
int ident;
int events;
android...
SampleNativeActivity.cpp

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.

12.
13.
14.
15.

//Do your Program Code
if (engine.init_wind...
How to Camera with OpenCV

©SIProp Project, 2006-2008

46
AndroidManifest.xml
Add “Camera” Permissions
1.

<uses-permission
android:name="android.permission.CAMERA"/>

2.

<uses-fe...
Edit jni/Android.mk 1/5
After “LOCAL_PATH := $(call my-dir)” Line

1.

OPENCV_CAMERA_MODULES:= native_camera_r2.2.0
native...
Edit jni/Android.mk 3/5

1.
2.

LOCAL_SRC_FILES:=../libs_opencv/libopencv_info.so
include $(PREBUILT_SHARED_LIBRARY)

3.

...
case APP_CMD_INIT_WINDOW:
LOGI("APP_CMD_INIT_WINDOW");
if (app->window != NULL)
{
LOGI("APP_CMD_INIT_WINDOW");

SampleNati...
SampleNativeActivity.cpp
if ((camera_resolution.width != 0) && (camera_resolution.height !=

0))

{
Init Camera
engine->ca...
SampleNativeActivity.cpp
New Engine
1.
2.
3.
4.

struct Engine {
struct android_app* app;
cv::Ptr<cv::VideoCapture> captur...
SampleNativeActivity.cpp
1.
cv::Mat drawing_frame;
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21....
How to Use Debugger

©SIProp Project, 2006-2008

54
Setup Debug Option

©SIProp Project, 2006-2008

55
Build as Debug Mode

©SIProp Project, 2006-2008

56
Enjoy Debugging!

©SIProp Project, 2006-2008

57
How to Use Profiler

©SIProp Project, 2006-2008

58
Android SDK Tools 8/12
lint
Optimize Android Applications.
lint | Android Developers
http://developer.android.com/tools/he...
Run lint

©SIProp Project, 2006-2008

60
Android SDK Tools 6/12
hprof-conv
Convert Android Original Format Heap Memory Dump
to Standard Format. Ex. Eclipse Memory ...
Setup Eclipse Memory Analyzer (MAT)

©SIProp Project, 2006-2008

62
Setup Eclipse Memory Analyzer (MAT)

©SIProp Project, 2006-2008

63
Show HeapMemory

©SIProp Project, 2006-2008

64
Add Memory Leak Code

1.
2.
3.
4.
5.
6.
7.

char *str = (char *)malloc(10000);
if(engine.counter < 65535) {
engine.leak[en...
But,,,Java Heap Only

©SIProp Project, 2006-2008

66
Android SDK Tools 11/12
Systrace
Profiling Tool. Use with Android Device Monitor.
Analyzing Display and Performance with S...
Systrace & TraceView

©SIProp Project, 2006-2008

68
View from Traceview

©SIProp Project, 2006-2008

69
Android SDK Tools 4/12
dmtracedump
Create Graphical Diagram from SysTrace Data
dmtracedump | Android Developers
http://dev...
Setup More Tools
Python
http://www.python.org/

Graphviz
http://www.graphviz.org
sudo apt-get install graphviz

©SIProp Pr...
How to Use
sdk/tools
dmtracedump
-g [Graph Image Name] [Trace Data]

©SIProp Project, 2006-2008

72
sdk/platform-tools/systrace/systrace.py
Options
--time=[s]

4.2 and lower

4.3 and Upper

--list-categories
--cpu-freq
gfx...
How to User on 4.2
./systrace.py
--cpu-freq --cpu-idle --cpu-load --no-cpu-sched -settags=gfx,input,view,webview,wm,am,syn...
If it doesn’t work…
Check these Configuration
kernel config
Kernel hacking ---> Tracers ---> Scheduling Latency
Tracer

in...
Appendix

©SIProp Project, 2006-2008

76
ARM DS-5 Development Studio
Functions
Debugger for Linux/Android™/RTOS-aware
The ARM Streamline system-wide performance an...
IDE

©SIProp Project, 2006-2008

78
Analyzer

©SIProp Project, 2006-2008

79
Upcoming SlideShare
Loading in …5
×

How to Make Android Native Application

6,393 views

Published on

How to Make NativeActivity Application
NativeActivityGlue
How to Draw Something
How to Use Library(OpenCV)
How to Debug NativeActivity Application
Debug
Profiling

Published in: Technology
  • Be the first to comment

How to Make Android Native Application

  1. 1. How to Make Android Native Application Noritsuna Imamura noritsuna@siprop.org ©SIProp Project, 2006-2008 1
  2. 2. Agenda How to Make NativeActivity Application NativeActivityGlue How to Draw Something How to Use Library(OpenCV) How to Debug NativeActivity Application Debug Profiling ©SIProp Project, 2006-2008 2
  3. 3. How to Make Native Application with Eclipse ©SIProp Project, 2006-2008 3
  4. 4. 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 4
  5. 5. Make Android Project ©SIProp Project, 2006-2008 5
  6. 6. Setup App Name & Target API ©SIProp Project, 2006-2008 6
  7. 7. Enable JNI ©SIProp Project, 2006-2008 7
  8. 8. Enable JNI ©SIProp Project, 2006-2008 8
  9. 9. SampleNativeActivity.cpp 1. #include <jni.h> ©SIProp Project, 2006-2008 9
  10. 10. Android.mk 1. LOCAL_PATH := $(call my-dir) 2. include $(CLEAR_VARS) 3. LOCAL_MODULE := SampleNativeActivity 4. LOCAL_SRC_FILES := SampleNativeActivity.cpp 5. include $(BUILD_SHARED_LIBRARY) ©SIProp Project, 2006-2008 10
  11. 11. 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 11
  12. 12. LifeCycle in NativeActivity [ndk dir]/platforms/android-19/archarm/usr/include/android native-activity.h 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. typedef struct ANativeActivityCallbacks { void (*onStart)(ANativeActivity* activity); void (*onResume)(ANativeActivity* activity); void* (*onSaveInstanceState)(ANativeActivity* activity, size_t* outSize); void (*onPause)(ANativeActivity* activity); void (*onStop)(ANativeActivity* activity); void (*onDestroy)(ANativeActivity* activity); ©SIProp Project, 2006-2008 12
  13. 13. NativeActivity CallBacks NativeActivity Effect onStart = Activity.onStart() onResume = Activity.onResume() onSaveInstanceState = Activity.onSaveInstanceState() onPause = Activity.onPause() onStop = Activity.onStop() onDestroy = Activity.onDestroy() onWindowsFocusChanged Focus of Window was Changed onNativeWindowCreated Native Windows was Created onNativeWindowResized Native Window was Re-sized onNativeWindowRedrawNeeded Native Window was Required Re-Draw onNativeWindowDestroyed Native Window was Destroyed onInputQueueCreated InputQueue was Created onInputQueueDestroyed InputQueue was Destroyed onContentRectChanged Drawable Area was Changed (Ex.Keyboard) onConfiguratinChanged System Configuration was Changed onLowMemory System Memory was Low ©SIProp Project, 2006-2008 13
  14. 14. Problem Point You MUST write it as Non-Block Function. In other word, you MUST use “Thread”. Thread Programing is sooooo hard… Why? Go to Next Page! ©SIProp Project, 2006-2008 14
  15. 15. NativeActivityGlue CallBacks NativeActivity NativeActivityGlue onStart APP_CMD_START onResume APP_CMD_RESUME onSaveInstanceState APP_CMD_SAVE_STATE onPause APP_CMD_PAUSE onStop APP_CMD_STOP onDestroy APP_CMD_DESTROY onWindowsFocusChanged APP_CMD_GAINED_FOCUS APP_CMD_LOST_FOCUS onNativeWindowCreated APP_CMD_INIT_WINDOW onNativeWindowResized APP_CMD_WINDOW_RESIZED onNativeWindowRedrawNeeded APP_CMD_WINDOW_REDRAW_NEEDED onNativeWindowDestroyed APP_CMD_TERM_WINDOW onInputQueueCreated onInputQueueDestroyed APP_CMD_INPUT_CHANGED onContentRectChanged APP_CMD_CONTENT_RECT_CHANGED onConfiguratinChanged APP_CMD_CONFIG_CHANGED onLowMemory APP_CMD_LOW_MEMORY ©SIProp Project, 2006-2008 15
  16. 16. NativeActivityGlue [NDK dir]/sources/android/native_app_glue 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. void android_app_pre_exec_cmd(struct android_app* android_app, int8_t cmd) { switch (cmd) { case APP_CMD_INPUT_CHANGED: LOGV("APP_CMD_INPUT_CHANGEDn"); pthread_mutex_lock(&android_app->mutex); if (android_app->inputQueue != NULL) { AInputQueue_detachLooper(android_app->inputQueue); } android_app->inputQueue = android_app>pendingInputQueue; if (android_app->inputQueue != NULL) { LOGV("Attaching input queue to looper"); AInputQueue_attachLooper(android_app->inputQueue, android_app->looper, LOOPER_ID_INPUT, NULL, &android_app->inputPollSource); } pthread_cond_broadcast(&android_app->cond); pthread_mutex_unlock(&android_app->mutex); break; ©SIProp Project, 2006-2008 16
  17. 17. Sample of NativeActivityGlue Don’t Need to Use “thread” Funcs. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. static void engine_handle_cmd(android_app* app, int32_t cmd) { Engine* engine = (Engine*)app->userData; switch (cmd) { case APP_CMD_INIT_WINDOW: if (app->window != NULL) LOGI("APP_CMD_INIT_WINDOW"); break; case APP_CMD_TERM_WINDOW: LOGI("APP_CMD_TERM_WINDOW"); break; ©SIProp Project, 2006-2008 17
  18. 18. Functions that you CAN USE Build-in Libs: How to Use: -lxx [NDK DIR]/platforms/android-19/arch-arm/usr/lib libandroid.so NativeActivity Lib libc.so libstdc++.so libc libdl.so Dynamic Load Lib libEGL.so libGLESv1_CM.so libGLESv2.so libGLESv3.so libjnigraphics.so liblog.so Logging libm.so Math Lib libOpenMAXAL.so Media Lib libOpenSLES.so Sound Lib libthread_db.so thread libz.so Zip Lib Graphic Funcs ©SIProp Project, 2006-2008 18
  19. 19. 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 19
  20. 20. 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 20
  21. 21. 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 21
  22. 22. How to Use NativeActivityGlue ©SIProp Project, 2006-2008 22
  23. 23. 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=”SampleNativeActivity" /> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> ©SIProp Project, 2006-2008 23
  24. 24. CPP Code of Basicary Format 1. #include <android_native_app_glue.h> 2. void android_main() { 3. 4. // Init Parameters 5. 6. // Loop 7. while(1) { 8. 9. //Do your Program Code 10. 11. //Drawing to Android Display 12. 13. if(finish == TRUE) { 14. // Processing closing 15. return; 16. } 17. } 18. } ©SIProp Project, 2006-2008 24
  25. 25. Show Android Event jni/SampleNativeActivity.cpp #include <android_native_app_glue.h> #include <android/log.h> #define LOG_TAG "MyApp:SampleNativeActivity" #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__) #define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__) #define LOGW(...) __android_log_print(ANDROID_LOG_WARN,LOG_TAG,__VA_ARGS__) #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__) ©SIProp Project, 2006-2008 25
  26. 26. Show Android Event jni/SampleNativeActivity.cpp struct Engine { struct android_app* app; }; void android_main(android_app* app) { // Init Parameters struct Engine engine; // It's magic func for NativeActivityGlue. app_dummy(); // Set UserData memset(&engine, 0, sizeof(engine)); app->userData = &engine; app->onAppCmd = engine_handle_cmd; engine.app = app; ©SIProp Project, 2006-2008 26
  27. 27. Show Android Event jni/SampleNativeActivity.cpp // Loop while(1) { // Read all pending events. int ident; int events; android_poll_source* source; // Process system events while ((ident=ALooper_pollAll(0, NULL, &events, (void**)&source)) >= 0) { // Process this event. if (source != NULL) { source->process(app, source); } //Do your Program Code //Drawing to Android Display } } } ©SIProp Project, 2006-2008 27
  28. 28. Show Android Event jni/SampleNativeActivity.cpp static void engine_handle_cmd(android_app* app, int32_t cmd) { Engine* engine = (Engine*)app->userData; switch (cmd) { case APP_CMD_START: LOGI("APP_CMD_START"); break; case APP_CMD_RESUME: LOGI("APP_CMD_RESUME"); break; case APP_CMD_SAVE_STATE: LOGI("APP_CMD_SAVE_STATE"); break; case APP_CMD_PAUSE: LOGI("APP_CMD_PAUSE"); break; case APP_CMD_STOP: LOGI("APP_CMD_STOP"); break; case APP_CMD_DESTROY: LOGI("APP_CMD_DESTROY"); break; ©SIProp Project, 2006-2008 28
  29. 29. Show Android Event Add the Red Lines to Android.mk LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := SampleNativeActivity LOCAL_SRC_FILES := SampleNativeActivity.cpp LOCAL_LDLIBS += -llog -landroid LOCAL_STATIC_LIBRARIES := android_native_app_glue include $(BUILD_SHARED_LIBRARY) $(call import-module,android/native_app_glue) ©SIProp Project, 2006-2008 29
  30. 30. ©SIProp Project, 2006-2008 30
  31. 31. How to Draw Text ©SIProp Project, 2006-2008 31
  32. 32. How to Draw Text? NDK doesn’t have this function!!! You CAN USE Bitmap File Only!!! Use Open GL/ES Use OpenCV . ©SIProp Project, 2006-2008 32
  33. 33. How to Draw Text with OpenCV ©SIProp Project, 2006-2008 33
  34. 34. Setup OpenCV Libs Libs libs_opencv include Files jni/include ©SIProp Project, 2006-2008 34
  35. 35. Edit jni/Android.mk 1/5 After “LOCAL_PATH := $(call my-dir)” Line 1. include $(CLEAR_VARS) 2. OPENCV_MODULES:=contrib legacy ml stitching objdetect ts videostab video photo calib3d features2d highgui imgproc flann core OPENCV_3RDPARTY_COMPONENTS:=tbb libjpeg libpng libtiff libjasper IlmImf 3. ©SIProp Project, 2006-2008 35
  36. 36. Edit jni/Android.mk 2/5 1. 2. 3. 4. 5. 6. define add_opencv_module include $(CLEAR_VARS) LOCAL_MODULE:=opencv_$1 LOCAL_SRC_FILES:=../libs_opencv/libopencv_$1.a include $(PREBUILT_STATIC_LIBRARY) endef 7. define add_opencv_3rdparty_component 8. include $(CLEAR_VARS) 9. LOCAL_MODULE:=$1 10. LOCAL_SRC_FILES:=../libs_opencv/lib$1.a 11. include $(PREBUILT_STATIC_LIBRARY) 12. endef ©SIProp Project, 2006-2008 36
  37. 37. Edit jni/Android.mk 3/5 1. 2. 3. 4. include $(CLEAR_VARS) LOCAL_MODULE:=opencv_info LOCAL_SRC_FILES:=../libs_opencv/libopencv_info.so include $(PREBUILT_SHARED_LIBRARY) 5. $(foreach module,$(OPENCV_MODULES),$(eval $(call add_opencv_module,$(module)))) $(foreach module,$(OPENCV_3RDPARTY_COMPONENTS),$(eval $(call add_opencv_3rdparty_component,$(module)))) 6. ©SIProp Project, 2006-2008 37
  38. 38. Edit jni/Android.mk 4/5 1. 2. OPENCV_INCLUDE_DIR:=$(LOCAL_DIR)/include LOCAL_C_INCLUDES+=$(OPENCV_INCLUDE_DIR) ©SIProp Project, 2006-2008 38
  39. 39. Edit jni/Android.mk 5/5 Add Loading Lib & OpenCV Lib LOCAL_LDLIBS -lm -lc -ldl -lz LOCAL_STATIC_LIBRARIES $(foreach mod, $(OPENCV_MODULES), opencv_$(mod)) $(OPENCV_3RDPARTY_COMPONENTS) 1. LOCAL_LDLIBS += -llog -landroid –lm -lc -ldl -lz 2. LOCAL_STATIC_LIBRARIES := android_native_app_glue $(foreach mod, $(OPENCV_MODULES), opencv_$(mod)) $(OPENCV_3RDPARTY_COMPONENTS) ©SIProp Project, 2006-2008 39
  40. 40. Application.mk Create “Application.mk” 1. 2. 3. 4. APP_STL := gnustl_static APP_CPPFLAGS := -frtti -fexceptions APP_ABI := armeabi-v7a APP_PLATFORM := android-15 ©SIProp Project, 2006-2008 40
  41. 41. SampleNativeActivity.cpp Init Buffer case APP_CMD_INIT_WINDOW: LOGI("APP_CMD_INIT_WINDOW"); { int view_width = ANativeWindow_getWidth(app->window); int view_height = ANativeWindow_getHeight(app->window); // Init Framebuffer if (ANativeWindow_setBuffersGeometry(app->window, view_width, view_height, WINDOW_FORMAT_RGBA_8888) < 0) { LOGE("Cannot set pixel format!"); return; } LOGI("cv::Mat initialized at resolution %dx%d", view_width, view_height); } break; ©SIProp Project, 2006-2008 41
  42. 42. SampleNativeActivity.cpp New Engine 1. 2. struct Engine { struct android_app* app; 3. 4. int init_window; }; ©SIProp Project, 2006-2008 42
  43. 43. 1. static void engine_draw_frame(struct Engine* SampleNativeActivity.cpp engine, const cv::Mat& frame) { 2. 3. 4. 5. if (engine->app->window == NULL) { // No window. return; } 6. 7. 8. 9. 10. ANativeWindow_Buffer buffer; if (ANativeWindow_lock(engine->app->window, &buffer, NULL) < 0) { LOGW("Unable to lock window buffer"); return; } 11. 12. { 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. uint16_t* pixels = (uint16_t*)buffer.bits; for( int y = 0; y < frame.size().height; y ++ ) { for( int x = 0; x < frame.size().width; x ++ ) { cv::Vec3b bgr = frame.at< cv::Vec3b >( y, x ); pixels[x] = make565( bgr[2], bgr[1], bgr[0] ); } pixels = (uint16_t*)pixels + buffer.stride; } } ANativeWindow_unlockAndPost(engine->app->window); } ©SIProp Project, 2006-2008 43
  44. 44. SampleNativeActivity.cpp 1. 2. 3. 4. 5. 6. // Loop while(1) { // Read all pending events. int ident; int events; android_poll_source* source; 7. 8. // Process system events while ((ident=ALooper_pollAll(0, NULL, &events, (void**)&source)) >= 0) { // Process this event. if (source != NULL) { source->process(app, source); } } } 9. 10. 11. 12. 13. 14. ©SIProp Project, 2006-2008 44
  45. 45. SampleNativeActivity.cpp 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. //Do your Program Code if (engine.init_window) { // Init Mat int view_width = ANativeWindow_getWidth(app->window); int view_height = ANativeWindow_getHeight(app->window); cv::Mat img = cv::Mat::zeros(view_width, view_height, CV_8UC3); char buffer[256]; sprintf(buffer, "Show Display Size: %dx%d", img.cols, img.rows); cv::putText(img, std::string(buffer), cv::Point(256,128), cv::FONT_HERSHEY_COMPLEX_SMALL, 1, cv::Scalar(255,255,255)); //Drawing to Android Display engine_draw_frame(&engine, img); } } ©SIProp Project, 2006-2008 45
  46. 46. How to Camera with OpenCV ©SIProp Project, 2006-2008 46
  47. 47. AndroidManifest.xml Add “Camera” Permissions 1. <uses-permission android:name="android.permission.CAMERA"/> 2. <uses-feature android:name="android.hardware.camera" android:required="false"/> <uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/> <uses-feature android:name="android.hardware.camera.front" android:required="false"/> <uses-feature android:name="android.hardware.camera.front.autofocus" android:required="false"/> 3. 4. 5. ©SIProp Project, 2006-2008 47
  48. 48. Edit jni/Android.mk 1/5 After “LOCAL_PATH := $(call my-dir)” Line 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 1. 2. 3. 4. 5. 6. define add_opencv_camera_module include $(CLEAR_VARS) LOCAL_MODULE:=$1 LOCAL_SRC_FILES:=../libs_opencv/lib$1.so include $(PREBUILT_SHARED_LIBRARY) endef ©SIProp Project, 2006-2008 48
  49. 49. Edit jni/Android.mk 3/5 1. 2. LOCAL_SRC_FILES:=../libs_opencv/libopencv_info.so include $(PREBUILT_SHARED_LIBRARY) 3. $(foreach module,$(OPENCV_MODULES),$(eval $(call add_opencv_module,$(module)))) $(foreach module,$(OPENCV_3RDPARTY_COMPONENTS),$(eval $(call add_opencv_3rdparty_component,$(module)))) $(foreach module,$(OPENCV_CAMERA_MODULES),$(eval $(call add_opencv_camera_module,$(module)))) 4. 5. ©SIProp Project, 2006-2008 49
  50. 50. case APP_CMD_INIT_WINDOW: LOGI("APP_CMD_INIT_WINDOW"); if (app->window != NULL) { LOGI("APP_CMD_INIT_WINDOW"); SampleNativeActivity.cpp Init Camera engine->capture = new cv::VideoCapture(0); union {double prop; const char* name;} u; u.prop = engine->capture>get(CV_CAP_PROP_SUPPORTED_PREVIEW_SIZES_STRING); int view_width = ANativeWindow_getWidth(app->window); int view_height = ANativeWindow_getHeight(app->window); cv::Size camera_resolution; if (u.name) camera_resolution = calc_optimal_camera_resolution(u.name, 640, 480); else { LOGE("Cannot get supported camera camera_resolutions"); camera_resolution = cv::Size(ANativeWindow_getWidth(app>window), ANativeWindow_getHeight(app->window)); } ©SIProp Project, 2006-2008 50
  51. 51. SampleNativeActivity.cpp if ((camera_resolution.width != 0) && (camera_resolution.height != 0)) { Init Camera engine->capture->set(CV_CAP_PROP_FRAME_WIDTH, camera_resolution.width); engine->capture->set(CV_CAP_PROP_FRAME_HEIGHT, camera_resolution.height); } float scale = std::min((float)view_width/camera_resolution.width, (float)view_height/camera_resolution.height); if (ANativeWindow_setBuffersGeometry(app->window, (int)(view_width/scale), int(view_height/scale), WINDOW_FORMAT_RGBA_8888) < 0) { LOGE("Cannot set pixel format!"); return; } LOGI("Camera initialized at resolution %dx%d", camera_resolution.width, camera_resolution.height); } break; ©SIProp Project, 2006-2008 51
  52. 52. SampleNativeActivity.cpp New Engine 1. 2. 3. 4. struct Engine { struct android_app* app; cv::Ptr<cv::VideoCapture> capture; }; ©SIProp Project, 2006-2008 52
  53. 53. SampleNativeActivity.cpp 1. cv::Mat drawing_frame; 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. // Loop while(1) { // Read all pending events. int ident; int events; android_poll_source* source; // Process system events while ((ident=ALooper_pollAll(0, NULL, &events, (void**)&source)) >= 0) { // Process this event. if (source != NULL) { source->process(app, source); } } //Do your Program Code // Capture frame from camera and draw it if (!engine.capture.empty()) { if (engine.capture->grab()) engine.capture->retrieve(drawing_frame, CV_CAP_ANDROID_COLOR_FRAME_RGBA); engine_draw_frame(&engine, drawing_frame); } } 53 ©SIProp Project, 2006-2008
  54. 54. How to Use Debugger ©SIProp Project, 2006-2008 54
  55. 55. Setup Debug Option ©SIProp Project, 2006-2008 55
  56. 56. Build as Debug Mode ©SIProp Project, 2006-2008 56
  57. 57. Enjoy Debugging! ©SIProp Project, 2006-2008 57
  58. 58. How to Use Profiler ©SIProp Project, 2006-2008 58
  59. 59. Android SDK Tools 8/12 lint Optimize Android Applications. lint | Android Developers http://developer.android.com/tools/help/lint.html ©SIProp Project, 2006-2008 59
  60. 60. Run lint ©SIProp Project, 2006-2008 60
  61. 61. Android SDK Tools 6/12 hprof-conv Convert Android Original Format Heap Memory Dump to Standard Format. Ex. Eclipse Memory Analyzer (MAT) HPROF Converter | Android Developers http://developer.android.com/tools/help/hprof-conv.html ©SIProp Project, 2006-2008 61
  62. 62. Setup Eclipse Memory Analyzer (MAT) ©SIProp Project, 2006-2008 62
  63. 63. Setup Eclipse Memory Analyzer (MAT) ©SIProp Project, 2006-2008 63
  64. 64. Show HeapMemory ©SIProp Project, 2006-2008 64
  65. 65. Add Memory Leak Code 1. 2. 3. 4. 5. 6. 7. char *str = (char *)malloc(10000); if(engine.counter < 65535) { engine.leak[engine.counter] = (int)str; LOGW("Leak Memory!!! %d", (int)str); } else { engine.counter = 0; } ©SIProp Project, 2006-2008 65
  66. 66. But,,,Java Heap Only ©SIProp Project, 2006-2008 66
  67. 67. Android SDK Tools 11/12 Systrace Profiling Tool. Use with Android Device Monitor. Analyzing Display and Performance with Systrace | Android Developers http://developer.android.com/tools/debugging/systrace.html traceview Show Graphical View from Systrace Data Traceview | Android Developers http://developer.android.com/tools/help/traceview.html deprecated Android Device Monitor ©SIProp Project, 2006-2008 67
  68. 68. Systrace & TraceView ©SIProp Project, 2006-2008 68
  69. 69. View from Traceview ©SIProp Project, 2006-2008 69
  70. 70. Android SDK Tools 4/12 dmtracedump Create Graphical Diagram from SysTrace Data dmtracedump | Android Developers http://developer.android.com/tools/help/dmtracedump.html ©SIProp Project, 2006-2008 70
  71. 71. Setup More Tools Python http://www.python.org/ Graphviz http://www.graphviz.org sudo apt-get install graphviz ©SIProp Project, 2006-2008 71
  72. 72. How to Use sdk/tools dmtracedump -g [Graph Image Name] [Trace Data] ©SIProp Project, 2006-2008 72
  73. 73. sdk/platform-tools/systrace/systrace.py Options --time=[s] 4.2 and lower 4.3 and Upper --list-categories --cpu-freq gfx - Graphics --cpu-idle input - Input --cpu-load view - View --no-cpu-sched webview - WebView --set-tags=<TAGS> wm - Window Manager gfx - Graphics am - Activity Manager input - Input audio - Audio view - View video - Video webview - WebView camera - Camera wm - Window Manager hal - Hardware Modules am - Activity Manager sync – Synchronization res - Resource Loading dalvik - Dalvik VM audio - Audio rs - RenderScript video - Video camera - Camera sched - CPU Scheduling freq - CPU Frequency membus - Memory Bus Utilization idle - CPU Idle disk - Disk input and output load - CPU Load sync - Synchronization Manager workq - Kernel Workqueues ©SIProp Project, 2006-2008 73
  74. 74. How to User on 4.2 ./systrace.py --cpu-freq --cpu-idle --cpu-load --no-cpu-sched -settags=gfx,input,view,webview,wm,am,sync,audio,video,c amera adb shell stop adb shell start ./systrace.py --time=10 -o mynewtrace.html ©SIProp Project, 2006-2008 74
  75. 75. If it doesn’t work… Check these Configuration kernel config Kernel hacking ---> Tracers ---> Scheduling Latency Tracer init.trace.rc mount debugfs /sys/kernel/debug /sys/kernel/debug ©SIProp Project, 2006-2008 75
  76. 76. Appendix ©SIProp Project, 2006-2008 76
  77. 77. ARM DS-5 Development Studio Functions Debugger for Linux/Android™/RTOS-aware The ARM Streamline system-wide performance analyzer Real-Time system model Simulators All conveniently Packaged in Eclipse. http://www.arm.com/products/tools/software-tools/ds5/index.php ©SIProp Project, 2006-2008 77
  78. 78. IDE ©SIProp Project, 2006-2008 78
  79. 79. Analyzer ©SIProp Project, 2006-2008 79

×