How to make Android application from from C++ program using OpenCV - android

I have a C++ program that starts like this:
#include "opencv2/highgui/highgui.hpp"
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <vector>
#include <stdio.h>
#include "linefinder.h"
#define PI 3.1415926
using namespace cv;
int main(int argc, char* argv[]) {
int houghVote = 200;
string arg = argv[1];
bool showSteps = argv[2];
string window_name = "Processed Video";
namedWindow(window_name, CV_WINDOW_KEEPRATIO); //resizable window;
VideoCapture capture(arg);
I want to make an Android app from it.
I have NDK installed but I don't know what I have to do now.
Do I have to change anything in the C++ main program so that it will run on Android, or can I compile and run it unchanged?

Introduction to Android
Android provides a rich application framework that allows you to build innovative apps and games for mobile devices in a Java language environment. ... Android apps are built as a combination of distinct components that can be invoked individually.
You need to construct your app using Java, then call your C++ code via JNI.
Also, Android NDK has this to say
you should only use the NDK if it is essential to your app—never because you simply prefer to program in C/C++
In particular, read Using the NDK to understand why you can't run your C++ main program without changing anything.

Related

How does Android source building #ifdefs work in compilation

I'm familiar with Android kernel programming but I'm a newbie at building the Android source. I'm wondering how to enable the #ifdefs in android source building. Are there any defconfig file in android source like in android kernel to choose what we want to compile in the compilation?.. How can i enable the codings defined with #ifdef to get compile during the Android source compilation?
Ex:
#ifdef USE_ION
int alloc_map_ion_memory(OMX_U32 buffer_size,
OMX_U32 alignment, struct ion_allocation_data *alloc_data,
struct ion_fd_data *fd_data,int flag);
void free_ion_memory(struct vdec_ion *buf_ion_info);
#else
bool align_pmem_buffers(int pmem_fd, OMX_U32 buffer_size,
OMX_U32 alignment);
#endif
I want to make sure the ion part is being compiled not the pmem part.
Try add the line:
#error "USE_ION"
after #ifdef USE_ION
Build again, if the build failed, USE_ION is defined.

How to link crypto shared library in Android JNI

I am writing a wrapper to use some functions of crypto.
I build crypto lib from openssl-android with Android-NDK. Now i have the libcrypto.so that i need, but i don´t know how to link it with my wrapper.
My project tree is like this
(proj root)
|
|->(src)
|->(src)-> com.package
|->(src)-> com.package->NativeCipher.java
|
|->(jni)
|->(jni)->Android.mk
|->(jni)->NativeCipher.c
NativeCipher.java
public class NativeCipher {
static {
System.loadLibrary("crypto");
System.loadLibrary("NativeCipher");
}
public static native byte[] AESEncrypt(byte[] in, byte[] key);
}
NativeCipher.c
#include <string.h>
#include <jni.h>
#include <aes.h>
jbyteArray Java_com_package_NativeCipher_AESEncrypt(JNIEnv* env, jobject this, jbyteArray in, jbyteArray key)
{
// All my code here
}
I need to use the functions of #include that crypto provides.
However, i don't know what to do with the .so files that NDK generates and how to make the Android.mk file to build.
Thanks in advance, i tried to be as specific as posible.
Native libraries go to the libs/armeabi or libs/armeabi-v7a of your Android project. You might want to rename the OpenSSL library though, because the system already has a libcrypto.so. As for your own JNI wrapper, just take the shared library sample from the NDK and modify to use your own files.

Eclipse CDT code analysis disagrees with GCC - why?

I like to build the Android NDK example "native-audio", supplied with the NDK.
The NDK-supplied compiler swallows it without complaint, but Eclipse is showing several errors in file native-audio-jni.c.
In the code excerpt below, all of the static variable declarations have red underlining, because Eclipse claims that the types cannot be resolved. They are defined right up there in the OpenSLES.h file, which Eclipse does find if I Ctrl + left click the file.
Note that I modified the lines where hello_clip and android_clip are included compared to the example source - I put the array declarations in those "header" files also (they contained just comma separated data), since Eclipse had a problem parsing the syntax, too:
static const char array[] = #include"blah.h";
I tried things suggested over the internet, like starting with eclipse.exe -clean, clean project / rebuild, refresh project, etc., none helps.
So, what could be the reason/solution for Eclipse not resolving those types?
#include <assert.h>
#include <jni.h>
#include <string.h>
// for __android_log_print(ANDROID_LOG_INFO, "YourApp", "formatted message");
// #include <android/log.h>
// for native audio
#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>
// for native asset manager
#include <sys/types.h>
#include <android/asset_manager.h>
#include <android/asset_manager_jni.h>
// pre-recorded sound clips, both are 8 kHz mono 16-bit signed little endian
// includes data arrays here
#include "hello_clip.h"
#include "android_clip.h"
// engine interfaces
static SLObjectItf engineObject = NULL;
static SLEngineItf engineEngine;
// output mix interfaces
static SLObjectItf outputMixObject = NULL;
static SLEnvironmentalReverbItf outputMixEnvironmentalReverb = NULL;
(the file is full of many more of those problems)

why lstat() not work in the fuse-mounted directory?

I have a directory mounted by fuse,and i am intented to use lstat() against this dirctory.But,when i run the following code,it just wait there and prompt nothing.
And by the way,i run the fuse in the android emulator.
the code is:
#include <sys/stat.h>
#include <stdio.h>
void main(){
printf("new test!!!");
char *path="/data/pwrite/test_12/";
struct stat *stbuf;
int res=12;
res=lstat(path, stbuf);
printf("%d",res);
}
And,"/data/pwrite/test_12/" is the fuse-mounted directory.What's more,when i try another dicrtory that share the same parent directory but not mounted by fuse,like /data/pwrite/test_13/,it works!
so,i definely sure it is leaded by fuse.But,i'm even more confused that whether it is due to the conflict between fuse and android.
Any idea?thx
Try without the bugs and see if that works better.
#include <sys/stat.h>
#include <stdio.h>
void main(){
printf("new test!!!\n");
const char *path="/data/pwrite/test_13/";
struct stat stbuf;
int res=12;
res=lstat(path, &stbuf);
printf("%d\n",res);
}
why it not work?because there runs several fuse-deamon currently.

Android Native OGLES2: esLoadProgram not declared

I have following commands:
// Load the shaders and get a linked program object
userData->programObject = esLoadProgram( vShaderStr, fShaderStr );
...
// Generate the vertex data
userData->numIndices = esGenCube( 1.0, &userData->vertices,NULL, NULL, &userData->indices );
The program is in native C++ for Android 4, using only NativeActivity. So, project has no /src and java classes.
I put the information to NDK OGLES 2.0 about the version running as EGL_OPENGL_ES2_BIT, and Android.mk was also setup to -lGLESv2. In the file is also included:
#include <GLES/gl.h>
#include <GLES/glext.h>
#include <EGL/egl.h>
#include <GLES2/gl2.h>
And also AndroidManifest was informed that it runs OGLES 2.0.
When asked to run, the program gives the following message:
'esLoadProgram' was not declared in this scope
'esGenCube' was not declared in this scope
For some reason these commands that belong to OGLES 2 are not visible. Any suggestion why this?
All comments are highly appreciated.
esLoadProgram and esGenCube are not a part of OpenGL ES or EGL. They are just a helper functions (probably from http://code.google.com/p/opengles-book-samples/)
PS. I would not suggest to mix also GLES and GLES2 headers. If you want GL ES 2.0, then include only from <GLES2/...> (not <GLES/...>)

Categories

Resources