Field 'distance' could not be resolved when using OpenCV4Android - android

I was tring to use ORB feature in Android apps,and create a project.When I use BruteForceMatcher and write the code:dist=matches[i].distance, My IDE notice me that "Field 'distance' could not be resolved". Why this happened?
#include <jni.h>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/legacy/legacy.hpp>
#include <iostream>
#include <vector>
using namespace std;
using namespace cv;
extern "C" {
JNIEXPORT int JNICALL Java_org_opencv_samples_tutorial2_Tuturial2Activity_CompareFeatures(JNIEnv*, jobject, jlong addrGray, jlong addrRgba);
JNIEXPORT int JNICALL Java_org_opencv_samples_tutorial2_Tuturial2Activity_CompareFeatures(JNIEnv*, jobject, jlong addrGray, jlong addrRgba)
{
char img_filename1[]="/sdcard/src.jpg";
char img_filename2[]="/sdcard/demo.jpg";
Mat src1,src2;
src1=imread(img_filename1,CV_LOAD_IMAGE_GRAYSCALE);
src2=imread(img_filename2,CV_LOAD_IMAGE_GRAYSCALE);
ORB orb;
vector<KeyPoint> keys1,keys2;
Mat descriptors1,descriptors2;
orb(src1,Mat(),keys1,descriptors1);
orb(src2,Mat(),keys2,descriptors2);
BruteForceMatcher<HammingLUT> matcher;
vector<DMatch> matches;
matcher.match(descriptors1,descriptors2,matches);
double max_dist=0; double min_dist=255;
//--Quick calculation of max and min distances between keypoints
for (int i=0;i<descriptors1.rows;i++)
{
double dist=matches[i].distance;
}
return 0;
}
}

Goto Project Properties -> C/C++ General -> GNU C++ ->
Edit change 4.x to 4.4.3 in ${NDKROOT}/sources/cxx-stl/gnu-libstdc++/4.x....

Related

OPENSSL Rand DRBG Andorid crashed when is used many times: A/libc: Fatal signal 7 (SIGBUS)

I'm trying the use the OpenSSL Rand library with DRBG in an android app, this library is implemented in native code of NDK. At starting, the OpenSSL works fine but in many times the app crashed and don't show any throw message. Here is the only error message that showwing:
A/libc: Fatal signal 7 (SIGBUS), code 1 (BUS_ADRALN), fault addr 0xb1aedca6f2d64adf in tid 22101 (RenderThread), pid 22075 (android.example)
My code is the follow:
libnative.cpp
#include <jni.h>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <openssl/rand.h>
#include <openssl/rand_drbg.h>
#include <android/log.h>
#include <future>
#ifndef TAG
#define TAG "OpenSslApi"
#endif
#ifndef DEFAULT_VALUE_ERROR
#define DEFAULT_VALUE_ERROR 0
#endif
void thread_handler(union sigval sv) {}
extern "C"
JNIEXPORT void JNICALL
Java_com_android_random_OpenSslApi_initDrbgRandom(
JNIEnv * env,
jclass clazz) {
RAND_DRBG * randDrbgInstance = RAND_DRBG_new(NID_aes_256_ctr, RAND_DRBG_FLAG_CTR_NO_DF, nullptr);
RAND_DRBG_instantiate(randDrbgInstance, nullptr, 0);
RAND_DRBG_set_reseed_time_interval(randDrbgInstance, 0);
RAND_DRBG_set_reseed_interval(randDrbgInstance, 0);
}
std::pair < jint * , jint > generateRandomIntDrbg(jint * secureRandom, jint sizeKey) {
jint myStatus = RAND_DRBG_bytes(
RAND_DRBG_get0_public(),
(unsigned char * ) secureRandom,
sizeKey * sizeof(int)
);
return std::make_pair(secureRandom, myStatus);
}
extern "C"
JNIEXPORT jint JNICALL
Java_com_android_random_OpenSslApi_intDrbgGenerateSecureRandom(
JNIEnv * env,
jclass clazz,
jintArray empty_array,
jint size_key) {
struct sigevent sev {};
timer_t timerid;
memset( & sev, 0, sizeof(sev));
sev.sigev_notify = SIGEV_THREAD;
sev.sigev_notify_function = & thread_handler;
sev.sigev_value.sival_ptr = & timerid;
timer_create(CLOCK_MONOTONIC, & sev, & timerid);
JavaVM * javaVm = nullptr;
env -> GetJavaVM( & javaVm);
javaVm -> AttachCurrentThread( & env, (void ** ) & env);
jintArray array = env -> NewIntArray(size_key);
if (array == nullptr) {
return DEFAULT_VALUE_ERROR;
}
jint * secureRandomIntArray = env -> GetIntArrayElements(array, nullptr);
if (secureRandomIntArray == nullptr) {
return DEFAULT_VALUE_ERROR;
}
std::future < std::pair < jint * , jint >> futureIntRandom = std::async (generateRandomIntDrbg, secureRandomIntArray, size_key);
std::pair < jint * , jint > result = futureIntRandom.get();
jint * resultSecureRandom = std::get < jint * > (result);
if (resultSecureRandom == nullptr) {
return DEFAULT_VALUE_ERROR;
}
memcpy(secureRandomIntArray, empty_array, size_key);
env -> ReleaseIntArrayElements(empty_array, secureRandomIntArray, 0);
return std::get < jint > (result);
}
OpenSslApi.java
static {
System.loadLibrary("libnative");
}
public OpenSslApi() {
initDrbgRandom();
}
public static native void initDrbgRandom();
public static native int intDrbgGenerateSecureRandom(
int[] emptyArray,
final int sizeKey
);
Thanks for either suggestions about the solution of this error.

Android ndk undefined reference error

I have a C++ source file, and I want to call a method from my C++ file, but I dont know whats wrong.
Here is my cpp method:
static unsigned ALawEncode(uint8_t* dst, int16_t* src, size_t srcSize);
Here is where I would like to call my method:
#include <string.h>
#include <jni.h>
#include <stdint.h>
#include "G711.h"
extern "C" {
uint8_t* Java_com_example_qonclient_Codecs_encodeG711( JNIEnv* env, jobject thiz, int16_t* source ){
size_t size0 = sizeof(source);
G711 g711;
uint8_t *dst;
g711.ALawEncode(dst, source, size0);
return dst;
}
}
Could you help me?

Android NDK passing parameters to native methods

I am studying OpenCV4Android SDK, in the 2.4.5 version, with the NDK framework, using which I can use native code (written in C/C++) in the Android environment. But I don't exactly understand how parameters are passed from Android to C.
For example, in the 'mixedprocessing' sample, in the directory 'jni' there's a .cpp file named 'jni_part', whose code is:
#include <jni.h>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <vector>
using namespace std;
using namespace cv;
extern "C" {
JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial2_Tutorial2Activity_FindFeatures(JNIEnv*, jobject, jlong addrGray, jlong addrRgba);
JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial2_Tutorial2Activity_FindFeatures(JNIEnv*, jobject, jlong addrGray, jlong addrRgba) {
Mat& mGr = *(Mat*)addrGray;
Mat& mRgb = *(Mat*)addrRgba;
vector<KeyPoint> v;
FastFeatureDetector detector(50);
detector.detect(mGr, v);
for( unsigned int i = 0; i < v.size(); i++ ) {
const KeyPoint& kp = v[i];
circle(mRgb, Point(kp.pt.x, kp.pt.y), 10, Scalar(255,0,0,255));
}
}
}
In the MainActivity there's the method:
public native void FindFeatures(long matAddrGr, long matAddrRgba);
So it's passed as parameter the native address of a Mat object, but how does it become a matrix in C?
And which features are detected from a FastFeatureDetector object?
FindFeatures, in Java, calls its exact equivalent in C/C++:
JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial2_Tutorial2Activity_FindFeatures(JNIEnv*, jobject, jlong addrGray, jlong addrRgba){
Mat& mGr = *(Mat*)addrGray;
Mat& mRgb = *(Mat*)addrRgba;
...
This is where it becomes a cv::Mat. (Mat*) casts what is pointed by address addrGray (respectively addrRgba) to a "pointer to a cv::Mat". Then, the value pointed by this newly created pointer is put in mGr (respectively mRgb), which is a cv::Mat.
In other words, you only give to C/C++ an address in the memory, and you have to make sure that what's there actually is a valid cv::Mat.
About your second question, the FAST detector detects points of interest in the image (i.e. points that contain a lot of information). The idea is to be able to identify those points on multiple different images. To simplify, you can consider a feature detected by FAST as a corner in the image.

Create an element of a c++ class in JNI

I want create an element of a own c++ class.
My JNI function:
JNIEXPORT void JNICALL Java_com_example_telo3_Process_inicializar_1nativo
(JNIEnv *, jobject){
Almacena almacena;
}
And Almacena is a c++ class:
#pragma once
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
class Almacena
{
private:
std::vector<Rect> myfaces;
std::vector<Rect> myeyes_r;
std::vector<Rect> myeyes_l;
std::vector<Rect> mynoses;
std::vector<Rect> mymouths;
Point2f P0;
Point2f P1;
Point2f P2;
Point2f P3;
public:
Almacena(void);
~Almacena(void);
void set_faces(std::vector<Rect> faces);
void set_eyes_r(std::vector<Rect> eyes_r);
void set_eyes_l(std::vector<Rect> eyes_l);
void set_noses(std::vector<Rect> noses);
void set_mouths(std::vector<Rect> mouths);
void set_P0(Point2f aux);
void set_P1(Point2f aux1);
void set_P2(Point2f aux2);
void set_P3(Point2f aux3);
std::vector<Rect> get_faces();
std::vector<Rect> get_eyes_r();
std::vector<Rect> get_eyes_l();
std::vector<Rect> get_noses();
std::vector<Rect> get_mouths();
Point2f get_P0();
Point2f get_P1();
Point2f get_P2();
Point2f get_P3();
};
How Can I do? Because if I do like in my code, appears these errors:
undefined reference to 'Almacena::~Almacena()' telo3 line 41, external location: C:\Users\telo\Desktop\ANDROID\android-ndk-r8d\toolchains\arm-linux-androideabi-4.6\prebuilt\windows\arm-linux-androideabi\bin\ld.exe: .\obj\local\armeabi\objs\nativo\nativo.o: in function Java_com_example_telo3_Process_inicializar_1nativo:jni\nativo.cpp C/C++ Problem
undefined reference to 'Almacena::Almacena()' telo3 line 41, external location: C:\Users\telo\Desktop\ANDROID\android-ndk-r8d\toolchains\arm-linux-androideabi-4.6\prebuilt\windows\arm-linux-androideabi\bin\ld.exe: .\obj\local\armeabi\objs\nativo\nativo.o: in function Java_com_example_telo3_Process_inicializar_1nativo:jni\nativo.cpp C/C++ Problem
In your Android.mk you should have
LOCAL_SRC_FILES := nativo.cpp \
Almacena.cpp \
... all other cpp files

how to generate random number jni in android

hi I m trying to convert my java code to c code for better speed ,
and I want to generate a random number in c code using jni(android)
in java code,
public int getRandomNumberFor()
{
Random random ;
random = new Random();
return random.nextInt(0xFF);
}
I don't know what code work for c
I tried finding example, but unfortunately I don't get it. can any one help me in this.?
Change your code as using C Programming in NDK :
#include <stdio.h>
#include <stdlib.h>
JNIEXPORT jint JNICALL Java_com_imrantestndk_androiddemo_NativeLib_Randomnum
(JNIEnv * env, jobject this){
int n;
n = rand()%100 + 1;
return n;
}
Or
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
JNIEXPORT jint JNICALL Java_com_imrantestndk_androiddemo_NativeLib_Randomnum
(JNIEnv * env, jobject this){
int random;
randomize();
random = random(100);
return random;
}

Categories

Resources