so I am new to using the JNI for Android so sorry in advance if this is something silly. I have installed the SDK and in Eclipse have added it as a library for the project. After finishing up everything, I have tried to run the ndk-build function but get this error:
Compile++ thumb : face_detect_rec <= jni_part.cpp
In file included from jni/face_detect_rec.h:11:0,
from jni/jni_part.cpp:3:
/Users/Justin/Documents/Android/opencv-2.4.3.2-android-sdk/sdk/native/jni/include/opencv2/core/core.hpp:56:21: fatal error: algorithm: No such file or directory
compilation terminated.
make: *** [obj/local/armeabi/objs/face_detect_rec/jni_part.o] Error 1
That file location is where the core.hpp is so I am not sure why this is a problem. I'll post my code below for reference, thanks guys!
jni_part.cpp:
#include <jni.h>
#include "face_detect_rec.h"
using namespace std;
using namespace cv;
extern "C" {
JNIEXPORT void JNICALL Java_com_example_opencvandroidtest_MainActivity_detectFaces(
JNIEnv* env, jclass mClass, jstring filePath)
{
detectFaces(filePath);
}
}
face_detect_rec.h
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <float.h>
#include <limits.h>
#include <time.h>
#include <ctype.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/contrib/contrib.hpp>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace cv;
using namespace std;
static void detectFaces(string filePath);
face_detect_rec.cpp:
#include "face_detect_rec.h"
// Create a string that contains the exact cascade name
string faceCascade_name =
"/Users/Justin/Documents/OpenCV/data/haarcascades/haarcascade_frontalface_alt2.xml";
/* "haarcascade_profileface.xml";*/
string eyeCascade_name =
"/Users/Justin/Documents/OpenCV/data/haarcascades/haarcascade_mcs_lefteye.xml";
//string rightEyeCascade_name =
// "/Users/Justin/Documents/OpenCV/data/haarcascades/haarcascade_mcs_righteye.xml";
// Function to detect and draw any faces that is present in an image
static void detectFaces(string filePath)
{
// Create a new Haar classifier
CascadeClassifier faceCascade;
Mat img = imread(filePath);
//int scale = 1;
// Load the HaarClassifierCascade
faceCascade.load(faceCascade_name);
// Check whether the cascade has loaded successfully. Else report and error and quit
if( faceCascade.empty() )
{
cout << "ERROR: Could not load classifier cascade\n";
return;
}
// There can be more than one face in an image. So create a growable sequence of faces.
// Detect the objects and store them in the sequence
vector<Rect> faces;
faceCascade.detectMultiScale(img, faces, 1.1, 2, CV_HAAR_SCALE_IMAGE, cvSize(70, 70));
// Loop the number of faces found.
for( int i=0; i<faces.size(); i++ )
{
//save image
Mat faceROI = img(faces[i]);
stringstream s;
s << "/mnt/sdcard/Pictures/TagSense" << i << ".jpg";
imwrite(s.str(), faceROI);
}
}
}
Do you have a so-called "Application.mk" file in your "jni" directory? If you don't, try to create it with, for instance, the following code:
Application.mk:
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi-v7a
APP_PLATFORM := android-8
Hope this will help.
Related
I'm trying to integrate OpenCV in Android Java native interface using C++ language. I have placed the OpenCV inside the jni folder, where I'm writing my C++ code. I have included the Opencv header files in my HelloNative.c file. But I'm still getting this error while trying to access Mat object.
"Can't resolve variable Mat".
I have tried using namespace cv, but it gives an error to predeclare using and namespace; which is not a solution. I'm posting my code below, please somebody have a look at it and let me know what I'm doing wrong.
#include <string.h>
#include <jni.h>
#include <stdio.h>
#include <stdlib.h>
#include <android/asset_manager.h>
#include <android/asset_manager_jni.h>
#include <android/log.h>
#include <malloc.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <OpenCV/include/opencv2/opencv.hpp>
#include <OpenCV/include/opencv/cv.h>
#include <OpenCV/modules/core/include/opencv2/core.hpp>
#include <OpenCV/modules/core/include/opencv2/core/core_c.h>
#include <OpenCV/modules/core/include/opencv2/core/mat.hpp>
#include <OpenCV/modules/imgproc/include/opencv2/imgproc/imgproc.hpp>
#include <OpenCV/modules/imgproc/include/opencv2/imgproc/imgproc_c.h>
#include <OpenCV/modules/legacy/include/opencv2/legacy/legacy.hpp>
#include <OpenCV/modules/videostab/include/opencv2/videostab/motion_stabilizing.hpp>
#include <OpenCV/modules/videostab/include/opencv2/videostab/global_motion.hpp>
JNIEXPORT jint JNICALL
Java_com_example_soimporttest_MainActivity_getStringFromJNI(JNIEnv *env, jobject thisObj,
jstring imagepath) {
const char *jnamestr = (*env)->GetStringUTFChars(env, imagepath, 0);
int width, height, gray;
FILE *fp = fopen(jnamestr, "rb");
if (fp) {
//Read .pgm file
int scan = fscanf(fp, "P5%d%d%d", &width, &height, &gray);
if (scan != 3 || &gray > 256 || &width > 0xfffff || &height > 0xfffff || &gray <= 1 ||
&width < 32 || &height < 32) {
fclose(fp);
return 9;
} else {
return 0;
}
} else {
return -1;
}
}
JNIEXPORT void JNICALL
Java_com_example_soimporttest_MainActivity_displayImage(JNIEnv *env, jobject thisObj, jclass type, jlong inpAddr, jlong outAddr) {
::Mat &src = *(Mat*)inpAddr;
Mat &dst = *(Mat*)outAddr;
applyFilter(src , dst);
}
There are two functions in this file, the first function getStringFromJNI is working perfectly fine.
Thank you.
After a lot of research, I finally managed to find out a good solution for my question. Please follow below mentioned tutorial:
https://github.com/leadrien/opencv_native_androidstudio
You'll be facing some issues while integrating it. You can post those solutions in the comments and I will guide you through each.
Thanks.
I am trying to read a file in c using NDK. When i debug the code, i see that i get a NULL pointer in f(file reading pointer). Also, my file which i need to read is in the same folder under cpp including all other header files. I have the read and write permissions for external storage in my app. Any help is appreciated.
C Code:
#ifndef client_h
#define client_h
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <pthread.h>
#include "utility.h"
#define SENDBUFSIZE 1399
#define RECVBUFSIZE 1399
void pingServer(char *hostname, int portno)
{
//Some other functionality which works correctly
// File Read operations
FILE *f = fopen("somefile.csv", "r"); //Get the null pointer here
if (f == NULL)
perror("Could not open input file");
char *currentRow[3];
char *nextRow[3];
int rows = 3;
for (int i=0; i<rows; i++)
{
currentRow[i] = (char*)malloc(20 * sizeof(char));
nextRow[i] = (char*)malloc(20 * sizeof(char));
}
double current, next;
double elapsed;
int isNextRow, isCurentRow;
readCsvLine(f,currentRow,rows); //Get the headers
int counter = 2;
isCurentRow = readCsvLine(f,currentRow,rows);
//sends the data
//Free the memory
for (int i=0; i<rows; i++)
{
free(currentRow[i]);
free(nextRow[i]);
}
free(buf);
return;
}
char* printName(){
char* ptr = "hello From C";
return ptr;
}
#endif
Java code for calling the c methods:
#include <jni.h>
#include <string>
#include "client.h"
extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_habbas_mobiledifferentiation_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
char *hostname = "127.0.0.1";
int portno = 1254;
pingServer(hostname, portno);
std::string hello = printName();
return env->NewStringUTF(hello.c_str());
}
I'm developing an image processing application using Opencv and android Ndk.
First I wrote my code in visual studio and there was no problem.
I have header roi.hpp and roi.cpp :
roi.hpp:
#ifndef ROI
#define ROI
#include <..\..\imgproc.hpp>
#include <..\..\opencv.hpp>
using namespace cv;
class My_ROI{
public:
My_ROI();
My_ROI(const My_ROI &my_roi);
My_ROI(Point upper_corner, Point lower_corner,Mat src);
Point upper_corner, lower_corner;
Mat roi_ptr;
Scalar color;
int border_thickness;
void draw_rectangle(Mat src);
};
#endif
roi.cpp:
#include "myImage.hpp"
#include <..\..\imgproc.hpp>
#include <..\..\opencv.hpp>
#include <..\..\highgui.hpp>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include "roi.hpp"
using namespace cv;
using namespace std;
My_ROI::My_ROI(const My_ROI &my_roi)
{
int i;
// allocate variables
My_ROI();
// copy values
operator = (my_roi);
}
My_ROI::My_ROI(){
upper_corner=Point(0,0);
lower_corner=Point(0,0);
}
My_ROI::My_ROI(Point u_corner, Point l_corner, Mat src){
upper_corner=u_corner;
lower_corner=l_corner;
color=Scalar(0,255,0);
border_thickness=2;
roi_ptr=src(Rect(u_corner.x, u_corner.y, l_corner.x- u_corner.x,l_corner.y- u_corner.y));
}
void My_ROI::draw_rectangle(Mat src){
rectangle(src,upper_corner,lower_corner,color,border_thickness);
}
and in another class I made a vector of My_ROI and use it with no problem, Like this:
vector <My_ROI> roi;
for (int j = 0; j<NSAMPLES; j++){
roi[j].draw_rectangle(m->src);
}
Then I ported my project to android I use roi.hpp and roi.cpp as external c++ files ,this is my android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
OPENCV_CAMERA_MODULES := on
OPENCV_INSTALL_MODULES := on
OPENCV_LIB_TYPE:=STATIC
include ../../jni/OpenCV.mk
LOCAL_MODULE := hello-jni
LOCAL_SRC_FILES := hello-jni.cpp
LOCAL_SRC_FILES += ../../external_cpp/makegray.cpp
LOCAL_SRC_FILES += ../../external_cpp/roi.cpp
LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../external_cpp
LOCAL_LDLIBS += -llog -ldl
include $(BUILD_SHARED_LIBRARY)
application.mk:
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi-v7a
APP_PLATFORM := android-8
And finally in hello-jni.cpp file I want to use vector of roi:
#include <jni.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <android/log.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <opencv2/opencv.hpp>
#include "../../external_cpp/roi.hpp"
vector <My_ROI> roi;
void fillROIArray(MyImage *m,Mat& mRgb,Mat& mGray)
{
m->src=mRgb;
flip(m->src,m->src,1);//reverse the order of rows or cols in a matrix
// My_ROI *f1 = new My_ROI;
// roi.push_back(*f1);
roi.push_back(My_ROI(Point(m->src.cols/3, m->src.rows/6),Point(m->src.cols/3+square_len,m->src.rows/6+square_len),m->src));
roi.push_back(My_ROI(Point(m->src.cols/4, m->src.rows/2),Point(m->src.cols/4+square_len,m->src.rows/2+square_len),m->src));
roi.push_back(My_ROI(Point(m->src.cols/3, m->src.rows/1.5),Point(m->src.cols/3+square_len,m->src.rows/1.5+square_len),m->src));
roi.push_back(My_ROI(Point(m->src.cols/2, m->src.rows/2),Point(m->src.cols/2+square_len,m->src.rows/2+square_len),m->src));
roi.push_back(My_ROI(Point(m->src.cols/2.5, m->src.rows/2.5),Point(m->src.cols/2.5+square_len,m->src.rows/2.5+square_len),m->src));
roi.push_back(My_ROI(Point(m->src.cols/2, m->src.rows/1.5),Point(m- >src.cols/2+square_len,m->src.rows/1.5+square_len),m->src));
roi.push_back(My_ROI(Point(m->src.cols/2.5, m->src.rows/1.8),Point(m->src.cols/2.5+square_len,m->src.rows/1.8+square_len),m->src));
mGray=m->src;
}
void myMethod(Image &m)
{
for (int j = 0; j<NSAMPLES; j++){
roi[j].draw_rectangle(m->src);
}
}
but it makes error :
method draw_rectangle(...) couldn't be resolved!
I can access my roi object all members but when I make vector of this class I don't have access to its members!
I searched a lot about making vectors of custom class and It's several days I'm working to solve this problem but I couldn't.
any help really appreciate.
regards.
Finally I found solution ... maybe it's usefull for others .
I can access to vector's members like this :
for (int j = 0; j<NSAMPLES; j++){
((My_ROI)roi[j]).draw_rectangle(m->src);
}
I can access any vector's objects from any custom classes now ! :)
So apparently im not too sure how to include Open GL in my SDL application
I have:
if _ANDROID_
#include <GLES2/gl2.h>
#include <GLES2/glext.h>
but when I use GLUint I get
GLuint does not name a type
My android.mk includes
LOCAL_LDLIBS := -lGLESv1_CM -lGLESv2 -llog
and i am building for android platform 10 (** **)
APP_PLATFORM := android-10
APP_ABI := armeabi-v7a armeabi x86
So i am not too sure what i am doing wrong
(Added the rest of the file that is having problems:)
#pragma once
#include <stdlib.h>
#include <iostream>
#include <random>
#include <cassert>
#include "vec2.h"
#include "vec3.h"
#include "vec4.h"
#include "mat4.h"
#include "transformations.h"
#if defined(_MSC_VER)
#include <windows.h>
#include <glew.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <SDL.h>
#include <SDL_ttf.h>
#include <SDL_image.h>
#include <SDL_mixer.h>
#include <SDL_opengl.h>
#include <stdio.h>
#include <string>
#endif
#if __APPLE__
#include <SDL2/SDL.h>
#include <SDL2_image/SDL_image.h>
#include <SDL2_ttf/SDL_ttf.h>
#include <SDL2_mixer/SDL_mixer.h>
#endif
#if __ANDROID__
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include <SDL_mixer.h>
#endif
// vec2 *screen;
/*const*/ // int sw;
/*const*/ // int sh;
#if __IPHONEOS__ || __ANDROID__
#define MOBILE 1
#endif
#if __MACOSX__ || __WINDOWS__ || __LINUX__
#define PC 1
#endif
#if __IPHONEOS__
#include <OpenGLES/ES2/gl.h>
#include <OpenGLES/ES2/glext.h>
inline void glBindVertexArray(GLuint id1) {
glBindVertexArrayOES(id1);
}
inline void glGenVertexArrays(GLsizei n, GLuint *ids) {
glGenVertexArraysOES(n, ids);
}
inline void glDeleteVertexArrays(GLsizei n, const GLuint *ids) {
glDeleteVertexArraysOES(n, ids);
}
#elif TARGET_OS_MAC
#include <OpenGL/gl.h>
#include <OpenGL/glext.h>
inline void glBindVertexArray(GLuint id1) {
glBindVertexArrayAPPLE(id1);
}
inline void glGenVertexArrays(GLsizei n, GLuint *ids) {
glGenVertexArraysAPPLE(n, ids);
}
inline void glDeleteVertexArrays(GLsizei n, const GLuint *ids) {
glDeleteVertexArraysAPPLE(n, ids);
}
#elif __ANDROID__
#include <GLES3/gl3.h>
#include <GLES3/gl3ext.h>
inline void glBindVertexArray(GLuint id1) {
glBindVertexArrayOES(id1);
}
inline void glGenVertexArrays(GLsizei n, GLuint *ids) {
glGenVertexArraysOES(n, ids);
}
inline void glDeleteVertexArrays(GLsizei n, const GLuint *ids) {
glDeleteVertexArraysOES(n, ids);
}
#endif // ANDROID
inline string get_path(string filename) {
char *base = SDL_GetBasePath();
string path(base + filename);
SDL_free(base);
//cout << "getting path " << path << endl;
return path;
}
using namespace std;
I solved it by using
APP_STL := c++_static
instead of
#APP_STL := gnustl_static
I'm using Eclipse ADT with: CDT. Along with NDK interfacing with JNI, to c/c++, compiling c/c++ with Cygwin through Eclipse. Everything should be running the latest versions as this was just setup over the last two weeks.
When Building I get the following.
jni/testSocketClass.hpp:33:1: error: unknown type name 'class'
jni/testSocketClass.hpp:33:17: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
jni/ndkfoo.c:13:1: error: unknown type name 'mYNewClass'
JNI C file
#include <string.h>
#include <jni.h>
#include "testSocketClassWrapper.hpp"
void *m_GBLpmyCSocket;
ndkfoo.c
jstring Java_com_example_hydrobedcontrol1_MainActivity_inintNativeClass(JNIEnv * env, jobject object){
m_GBLpmyCSocket = MyClass_create();
MyClass_sendCommandToSerialDevice(m_GBLpmyCSocket, 0, 0, 0);
return (*env)->NewStringUTF(env, "started");
}
Class Wrapper .hpp
//file testSocketClassWrapper.hpp
#ifndef _MY_SOCKETCLASS_WRAPPER_H
#define _MY_SOCKETCLASS_WRAPPER_H
#include"testSocketClass.hpp"//<<<<<<<<<<<<<<<<<<<<<Wrong inclusion
#ifdef __cplusplus
extern "C" void* MyClass_create() {
return new mYNewClass;
}
extern "C" void MyClass_release(void* myclass) {
delete static_cast<mYNewClass*>(myclass);
}
extern "C" void MyClass_sendCommandToSerialDevice(void* myclass, int cmd, int params, int id) {
static_cast<mYNewClass*>(myclass)->sendCommandToSerialDevice(cmd,params,id);
}
#endif
#endif /* _MY_SOCKETCLASS_WRAPPER_H_INCLUDED */
Class wrapper .cpp
//file testSocketClassWrapper.cpp
#include "testSocketClassWrapper.hpp"
Class .h
// file testSocketClass.hpp
class mYNewClass{///////////////////////ERROR HERE////////////////////////////////
//public:
void sendCommandToSerialDevice(int Command, int Parameters, int DeviceID);
//int sockfd;
};
Class .cpp
// file testSocketClass.cpp
#include "testSocketClass.hpp"
void mYNewClass::sendCommandToSerialDevice(int Command, int Parameters, int DeviceID){
char testc[100];
sprintf(testc, "%d, %d, %d", Command, Parameters, DeviceID);
}
I've read countless questions on this topic and have had quite a few issues getting this far with Eclipse configurations too. But from what I've pulled together from other questions on this topic, I have hit a wall and do not know how to proceed at this point. Suggestions?
EDIT BELOW - ANSWER
After review with a colleague(sometimes a second pair of eyes helps bring things out), we located my error. See the line labeled improper inclusion in the Class wrapper .hpp. That header should be relocated as follows in the Class wrapper .cpp NOTE: the functions where also moved to the .cpp and the .hpp is now empty.
//file testSocketClassWrapper.cpp
#include "testSocketClassWrapper.hpp"
#include "testSocketClass.hpp"
extern "C" void* MyClass_create() {
return new mYNewClass;
}
extern "C" void MyClass_release(void* myclass) {
delete static_cast<mYNewClass*>(myclass);
}
extern "C" void MyClass_sendCommandToSerialDevice(void* myclass, int cmd, int params, int id) {
static_cast<mYNewClass*>(myclass)->sendCommandToSerialDevice(cmd,params,id);
}
Also for completeness as this has been no walk in the park, are the MK files.
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
# Here we give our module name and source file(s)
LOCAL_MODULE := ndkfoo
LOCAL_SRC_FILES := ndkfoo.c testSocketClassWrapper.cpp testSocketClass.cpp
include $(BUILD_SHARED_LIBRARY)
Application.mk
APP_STL:=stlport_static
Thank you for the prompt reply krsteeve. And yet another way to do this. Will keep it in mind.
testSockedClass.hpp is being included from a .c file, so it's being compiled with a C-compiler. class doesn't exist in C. Change ndkfoo.c to a .cpp file.
You will have to format things a bit differently in C++:
extern "C"
{
jstring Java_com_example_hydrobedcontrol1_MainActivity_inintNativeClass(JNIEnv * env, jobject object);
}
jstring Java_com_example_hydrobedcontrol1_MainActivity_inintNativeClass(JNIEnv * env, jobject object){
m_GBLpmyCSocket = MyClass_create();
MyClass_sendCommandToSerialDevice(m_GBLpmyCSocket, 0, 0, 0);
return env->NewStringUTF("started");
}
Note the form of the NewStringUTF call in C++ vs C.