Android studio C++ ndk camera - android

I created C/C++ project in android studio and I want to use NDK camera.
I wrote in my cpp file
#include <camera/NdkCameraMetadata.h>
#include <camera/NdkCameraManager.h>
#include <camera/NdkCameraDevice.h>
#include <jni.h>
#include <string>
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_test_MainActivity_listDevices(JNIEnv* env, jobject)
{
std::string text;
ACameraIdList *camList;
ACameraManager *camManager;
camManager = ACameraManager_create();
camera_status_t result = ACameraManager_getCameraIdList(camManager, &camList);
if (result == ACAMERA_OK)
{
text = "Error List devices";
}
else
{
text = "Device listed";
}
return env->NewStringUTF(text.c_str());;
}
but Android studio is writing
"Can't resolve type ACameraIdList"
What I'm doing wrong? I just added this code into .cpp file, nothing else. Idid not changed any other files.

As mentioned in the comments, you need to set the minSDK to 24 or higher.
The NdkCameraDevice.h header files detects the minSDK using the __ANDROID_API__ identifier:
#if __ANDROID_API__ >= 24
...
typedef struct ACameraIdList {
int numCameras; ///< Number of camera device Ids
const char** cameraIds; ///< list of camera device Ids
} ACameraIdList;
...
#endif
This means that if the minSDK is below 24 the C preprocessor will omit the declaration of ACameraIdList and other related structures and functions. This leads to the "Can't resolve type ACameraIdList" error.

Related

NDK need gnustl_static std::string instead of std::_ndk1::string

I am hooking an existing library that is compiled using gnustl std::string instead of libc++ std::_ndk1::string. If I try to set or access these strings I just get garbage. How do I convert my std::string to std::_ndk1::string and vice versa in my hook application?
I cannot compile my hook with "-DANDROID_STL=gnustl_shared" because it no longer exists and other libraries in use require libc++.
The documentation mentions this https://developer.android.com/ndk/guides/cpp-support "The various STLs are not compatible with one another. As an example, the layout of std::string in libc++ is not the same as it is in gnustl" which is exactly the problem.
To use gnustl, you could compile all your native code with NDK r.17 or older. This is a dangerous path, because many important bugs, including security-related, have been fixed since then.
Another unsupported (and not recommended) way to deal with your problem is to get the gnustl sources from NDK r.17 and compile them with the latest NDK version.
Your best option is to have all your dependencies rebuilt with a recent version of NDK and its c++_shared runtime library.
Here's what I came up with (for now, really not ideal):
StringsProxy.cc
#include "StringsProxy.h"
#include <iostream>
#include <string>
using namespace std;
__attribute__((visibility("default")))
extern "C" StringsProxy::StringsProxy(const char* contents)
{
set_string = std::string(contents);
}
__attribute__((visibility("default")))
extern "C" StringsProxy::StringsProxy(uintptr_t str) {
set_string = *reinterpret_cast<proxy_string*>(str);
}
__attribute__((visibility("default")))
extern "C" const char* StringsProxy::c_str() {
return set_string.c_str();
}
__attribute__((visibility("default")))
extern "C" const uintptr_t* StringsProxy::ptr() {
return reinterpret_cast<uintptr_t *>(&set_string);
}
__attribute__((visibility("default")))
extern "C" StringsProxy::~StringsProxy() {
}
StringsProxy.h
#ifndef __STRINGSPROXY_H__
#define __STRINGSPROXY_H__
#include <string>
typedef std::basic_string<char> proxy_string;
class StringsProxy
{
public:
/* Initialize StringsProxy with a pointer to an existing string */
StringsProxy(uintptr_t str);
/* Initialize StringsProxy with a new string */
StringsProxy(const char* str);
/* Get C string */
virtual const char* c_str();
/* Get pointer to string for injection */
const virtual uintptr_t* ptr();
private:
proxy_string set_string;
};
#endif
Compile this into a shared object using the old NDK with -DCMAKE_ANDROID_STL_TYPE=gnustl_static
Then link this shared object to the hooking program (in CMakeLists):
target_link_libraries(${TARGET_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/abiproxy/build/armeabi-v7a/libabiproxy.so)
Then in the hooking program, can be used like this:
#include "abiproxy/StringsProxy.h"
void *setUriDebug(uintptr_t a1, uintptr_t stri) {
auto y = StringsProxy(stri);
LOGI("URI CALLED %s", y.c_str());
return setUriDebugOld(a1, stri);
}
Or in reverse:
StringsProxy assetNameBaseProxy = StringsProxy("https://example.com/");
void setResourceUrl(uintptr_t* a1, int a2) {
*(a1 + 1) = *assetNameBaseProxy.ptr();
}
This isn't by any means a good solution, but it works for my use-case.

OpenCV and C++: "Can't resolve variable 'Mat`"

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.

Can't use typedef enum

I have a header file with definition:
typedef enum acamera_metadata_enum_android_lens_facing {
// enumeration
} acamera_metadata_enum_android_lens_facing_t;
The problem is when I am trying to declare this enum as my class'es member the compiler can't find definition (header is found).
../../../../src/main/cpp/include/camera_manager.h:41:9: error: unknown type name 'acamera_metadata_enum_android_lens_facing_t'
acamera_metadata_enum_android_lens_facing_t facing;
This is my class header:
#ifndef DAVINCI_CAMERA_MANAGER_H
#define DAVINCI_CAMERA_MANAGER_H
#include <map>
#include <string>
#include <camera/NdkCameraManager.h>
#include <camera/NdkCameraError.h>
#include <camera/NdkCameraDevice.h>
#include <camera/NdkCameraMetadataTags.h> // The enumeration is defined here
#include <media/NdkImageReader.h>
namespace DaVinci {
class CameraId;
class CameraManager {
struct ACameraManager *_manager;
std::map<std::string, CameraId> _cameras;
std::string _activeCameraId;
int32_t _cameraFacing;
int32_t _cameraOrientation;
bool _valid;
public:
CameraManager();
~CameraManager();
};
// helper classes to hold enumerated camera
class CameraId {
public:
struct ACameraDevice *device;
std::string id;
acamera_metadata_enum_android_lens_facing_t facing;
bool available; // free to use ( no other apps are using
bool owner; // we are the owner of the camera
explicit CameraId(const char *id);
explicit CameraId();
};
};
#endif //DAVINCI_CAMERA_MANAGER_H
Where can the problem be?
P.S. I am using c++ 14 if it's important.
UPDATED
I created a repository with my project: https://bitbucket.org/ghostman2013/davinci_test
In your project's app/build.gradle you have minSdkVersion set to 21.
The native camera APIs were added in API level 24.
So you can either A) increase your minSdkVersion to 24 or higher, or B) Not use the native camera APIs in your library.

How to get app package name or applicationId using JNI android

For the protection issue of the shared library I will try to get package name using JNI but it will give errors. So, is it possible to get package name or applicationId using JNI? If anyone have example or references for this problem then suggests. Because not any good tutorial or solution available. Else any other way suggest of the protection of the shared library.
Yes, it's possible. Android is based on Linux, we can obtain a lot of information in user space provided by kernel.
In your example, the information stored here /proc/${process_id}/cmdline
We can read this file, and get the application id.
See a simple example
#include <jni.h>
#include <unistd.h>
#include <android/log.h>
#include <stdio.h>
#define TAG "YOURAPPTAG"
extern "C"
JNIEXPORT void JNICALL
Java_com_x_y_MyNative_showApplicationId(JNIEnv *env, jclass type) {
pid_t pid = getpid();
__android_log_print(ANDROID_LOG_DEBUG, TAG, "process id %d\n", pid);
char path[64] = { 0 };
sprintf(path, "/proc/%d/cmdline", pid);
FILE *cmdline = fopen(path, "r");
if (cmdline) {
char application_id[64] = { 0 };
fread(application_id, sizeof(application_id), 1, cmdline);
__android_log_print(ANDROID_LOG_DEBUG, TAG, "application id %s\n", application_id);
fclose(cmdline);
}
}
This works for me:
static jstring get_package_name(
JNIEnv *env,
jobject jActivity
) {
jclass jActivity_class = env->GetObjectClass(jActivity);
jmethodID jMethod_id_pn = env->GetMethodID(
jActivity_class,
"getPackageName",
"()Ljava/lang/String;");
jstring package_name = (jstring) env->CallObjectMethod(
jActivity,
jMethod_id_pn);
return package_name;
}

"Not declared in this scope" error in Android JNI C++

I have this JNI code that uses C++.
#include <eigenfaces_jni.h>
#include <opencv2/core/core.hpp>
#include <opencv2/contrib/contrib.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
using namespace cv;
JNIEXPORT jstring JNICALL Java_com_example_facedetector_FaceTracker_nativeCreateObject
(JNIEnv *env, jclass, jstring JFileDir){
// These vectors hold the images and corresponding labels.
vector<Mat> images;
const char *str = env->GetStringUTFChars(jFileDir, NULL);
const char *buf;
/*Releasing the Java String once you have got the C string
is very important!!!*/
env->ReleaseStringUTFChars(jFileDir, str);
Mat im = imread(jFileDir);
if (im.empty())
{
cout << "Cannot load image!" << endl;
buf = "Can load image";
}
else{
buf = "Cannot load image";
}
return env->NewStringUTF("Hello");
}
However, there is always an error at
const char *str = env->GetStringUTFChars(jFileDir, NULL);
which states that
- Symbol 'jFileDir' could not be resolved
- 'jFileDir' was not declared in this scope
- Invalid arguments ' Candidates are: const char * GetStringUTFChars(_jstring *, unsigned
char *) '
Tried the solution in this link Type 'std::string' could not be resolved.
Still not working. Any help to solve this problem would be appreciated. Thank you.
You've declared a function parameter called JFileDir, but nothing called jFileDir. C++ is case-sensitive.
Well, since I don't have enough reputation points to commend, I have to write this as an answer. Check your function parameter list (JNIEnv *env, jclass, jstring JFileDir), what is jclass about, I feel something is missing here.

Categories

Resources