How to set up opencl in opencv for android - android

everyone, I want to use opencl module in opencv in my android application. I use native C++ and the version of opencv is 3.1.0. I first run the following code in win32 console application on my computer, the opencl module works, but when I directly use this code on android platform, I find that "haveOpenCL" return 0. The question is:
How can opencv find the openCL lib path, for example, the path of libopenCL.so in my android device is
/vendor/lib/libOpenCL.so
Where can I set this lib path in my program?
Is any tutorial for building opencl module on opencv for android?
Any suggestions are welcome!
#include <opencv2/opencv.hpp>
#include <opencv2/core/ocl.hpp>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
cv::ocl::setUseOpenCL(true);
cout << cv::ocl::haveOpenCL() << endl;
if (cv::ocl::haveOpenCL())
{
cout << "OpenCL IS avaiable ..." << endl;
//return 0;
}
UMat img;
img = imread("test.jpg").
getUMat(ACCESS_READ);
UMat result;
cv::Mat H = cv::Mat::eye(3, 3, CV_64FC1);
for (int i=0;i<800;i++)
{
warpPerspective(img, result, H, img.size());
}
return 0;
}

Related

Providing a JVM for MediaStreamer2 Console Application on Android

I am working on an Android firmware for an embedded device which streams an encoded video signal using rtp. The underlying library is MediaStreamer2 because it comes with Android support, various codecs and libortp. Therefore I integrated libmediastreamer and its dependencies into my firmware build process.
As a second step, I wrote a simple Android command line application as a PoC which streams audio or video through the network. Unfortunatly, the first call to ms_init() fails due to:
bctbx-fatal-Calling ms_get_jni_env() while no jvm has been set using ms_set_jvm()
Digging a little deeper into the problem, it seems Androids version of libmediastreamer was designed from an NDK point of view: It can be called as a part of an Android app and therefore automatically gets a reference to the JVM (DVM?). Unfortunatly, this is not my use case.
I tried to to remove the dependencies (Querying Sdk version, hardware echo cancelation support, etc.) without success. So my next approach would be starting a VM manually and passing it to the library. I tried Oracles APIs like:
JNIEnv env;
JavaVM vm;
JavaVMInitArgs vm_args;
JavaVMOption options[4];
options[0].optionString = "-Djava.compiler=NONE";
options[1].optionString = "-verbose:jni";
vm_args.version = JNI_VERSION_1_2;
vm_args.options = options;
vm_args.nOptions = 4;
vm_args.ignoreUnrecognized = TRUE;
jint res = JNI_CreateJavaVM(&vm, (void **)&env, &vm_args);
But the application quits with a simple "aborted". Nevertheless, I am not sure whether this is a way to go because its Android and Dalvik world.
Any suggestions?
It is possible to build executable for shell on Android on both rooted and non-rooted devices, see reference How to build an executable for Android shell
.
Try below code and build it using NDK to get an executable:
#include <jni.h>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char **argv) {
JavaVMOption jvmopt[1];
jvmopt[0].optionString = "-Djava.class.path=" + ".";
JavaVMInitArgs vmArgs;
vmArgs.version = JNI_VERSION_1_2;
vmArgs.nOptions = 1;
vmArgs.options = jvmopt;
vmArgs.ignoreUnrecognized = JNI_TRUE;
// Create the JVM
JavaVM *javaVM;
JNIEnv *jniEnv;
long flag = JNI_CreateJavaVM(&javaVM, (void**)
&jniEnv, &vmArgs);
if (flag == JNI_ERR) {
cout << "Error creating VM. Exiting...\n";
return 1;
}
/** ----------------------------------------------
* Put your own JNI related code from here if any.
* -----------------------------------------------
**/
javaVM->DestroyJavaVM();
return 0;
}
Do a check on <jni.h> about the interfaces you can use, e.g.
/*
* VM initialization functions.
*
* Note these are the only symbols exported for JNI by the VM.
*/
jint JNI_GetDefaultJavaVMInitArgs(void*);
jint JNI_CreateJavaVM(JavaVM**, JNIEnv**, void*);
jint JNI_GetCreatedJavaVMs(JavaVM**, jsize, jsize*);
You can refer to below to see if they are helpful:
how-to-create-a-jvm-instance-in-jni
https://calebfenton.github.io/2017/04/05/creating_java_vm_from_android_native_code/

Is Qt QSharedMemory supported on Android?

I am trying to use QSharedMemory. It works fine under Windows, not in Android. The very first create call fails. It is not linked to the key, as I have used different values.
My main.cpp is as follows:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QSharedMemory>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
QSharedMemory _sharedMemory("QtMemTest", nullptr);
if (!_sharedMemory.create(sizeof(int), QSharedMemory::ReadWrite))
qDebug() << "Failed " << _sharedMemory.errorString();
else
qDebug() << "Size " << QString::number(_sharedMemory.size());
return app.exec();
}
On Windows, I get:
Size "4096"
On Android:
main.cpp:17 (int main(int, char**)): Failed "QSharedMemory::create:
unable to lock"
This is with a single process, just trying to create the shared memory block.
This really looks like a basic problem... Do I need a specific Android permission? Is this available in Android?
I am using Qt Creator 4.6.2, am targeting gcc 4.9, Qt 5.9.6 for Android armv7.
I have tried on Android devices running 6.0 and 8.0.
Edit
Tried this with Qt 5.11.1, the problem is still the same.
Also, while running this, I get a series of warnings on the application output console:
W libsharedExample.so: (null):0 ((null)): Unimplemented code.
Commenting out the create call removes 6 such warnings. How to figure out what features are implemented in a given Qt release? If this is not implemented for Android, how is an app supposed to communicate with its service (which is a different process)?
So this class is not implemented on Android. The unimplemented code warning is genuine.
A reliable way to move data between processes on Android seems to be with QtRemoteObject.

C++ regex matches on ideone.com but not in Android NDK build

I have the following program working correctly in ideone
#include <iostream>
#include <regex>
using namespace std;
int main() {
if (regex_match("test", regex("^[_a-z0-9]{3,12}$"))) {
cout << "match" << endl;
} else {
cout << "no match" << endl;
}
return 0;
}
It matches as expected. Just checking for a string containing between 3 and 12 alphanumeric characters or underscores.
However, the same code run in native code on Android (built using ndk-build with gnustl_shared) fails (does not match).
If regexes aren't properly supported under Android, shouldn't my build fail to compile? Am I missing something obvious here?
I have the same issue today,
and in my own experience, just change the "_" position in the expression.
for my example,
just replace
std::regex reg("[^. _A-Za-z0-9]");
with
std::regex reg("[^. A-Za-z0-9_]");
and it works fine, maybe it is because the gcc version is too old.

how to port cocos2dx game to android?

I am very much new to cocos2dx. I have a cocos2dx project made in xcode. for gesture recognizers I used cocoa touch's native code(UIGestureRecognizers) in cocos2dx using .mm files.
Now I want to build this project with android sdk too.
The Error i am getting by building the android project using build_native.sh file, in terminal is Like The Following
1
../android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: ./obj/local/armeabi/objs/cocos2dcpp_shared/__/__/Classes/HelloWorldScene.o: in function HelloWorld::menuCloseCallback(cocos2d::CCObject*):jni/../../Classes/HelloWorldScene.cpp:88: error: undefined reference to 'XBridge::doSth()'
2
../android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: ./obj/local/armeabi/objs/cocos2dcpp_shared/__/__/Classes/HelloWorldScene.o: in function HelloWorld::menuCloseCallback(cocos2d::CCObject*):jni/../../Classes/HelloWorldScene.cpp:85: error: undefined reference to 'XBridge::imageName'
collect2: ld returned 1 exit status
make: *** [obj/local/armeabi/libcocos2dcpp.so] Error 1
make: Leaving directory `../cocos2d-x/projects/MyGame/proj.android'
What Am I missing Here ? I just started learning cocos2dx.
Can i even use the native iOS code like this in an android project ? or i am just shooting blanks ?
My Code is as written below.
CODE IN MY HelloWorldScene.cpp
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
cocos2d::CCString * imageNameString = cocos2d::CCString::create("image.png");
XBridge::imageName = imageNameString->getCString();
XBridge::doSth();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
//exit(0);
#endif
#endif
}
Code In XBridge.h
#ifndef xbridge_XBridgeViewController_h
#define xbridge_XBridgeViewController_h
#include "cocos2d.h"
class XBridge {
public:
static std::string imageName;
static void doSth();
};
#endif
Code In XBridge.mm
#include "XBridge.h"
#include "AppController.h"
#include "RootViewController.h"
#include "SpriteVC.h"
using namespace cocos2d;
std::string XBridge::imageName;
void XBridge::doSth()
{
id sth = [[UIApplication sharedApplication] delegate];
if ([sth isKindOfClass:[AppController class]])
{
printf("XBridge::doSth imageName %s\n",imageName.c_str());
SpriteVC *SPVC = [[SpriteVC alloc] initWithNibName:nil bundle:nil];
SPVC.imageNameString = [NSString stringWithFormat:#"%s",imageName.c_str()];
[SPVC setUpImage];
NSLog(#"XBridge::doSth imageName == %#",[NSString stringWithFormat:#"%s",imageName.c_str()]);
//SPVC.imageView.frame = CGRectMake(480, 320, 333, 333);
AppController *controller = (AppController *)sth;
[controller.viewController.view addSubview:SPVC.photoImage];
}
}
You can't use iOS Cocoa Touch API for Android even though you actually can compile Objective-C and Objective-C++ code using Android NDK Clang toolchain. Because Android NDK doesn't provide iOS API at all like UIApplication, AppController, and so forth. Cocos2d-x just provides Cocos2d-x API in C++ and some plugins for Android.
If you want to use iOS API for Android, take a look at Apportable.

Does Android support thread?

Does Android support pthreads?
And why when i use -pthread option i see the linker error:
i686-android-linux/bin/ld: cannot find -lpthread
#include <pthread.h>
#include <cxxabi.h>
extern "C" int printf (const char *, ...);
int main()
{
try
{
pthread_exit (0);
}
catch (abi::__forced_unwind &)
{
printf ("caught forced unwind\n");
throw;
}
catch (...)
{
printf ("caught ...\n");
return 1;
}
}
As far as I could see in the docs you do not need to use "-pthread". Checkout following:
http://mobilepearls.com/labs/native-android-api/#pthreads
Info from NDK offical docs states (android-ndk-r8\docs\system\libc\OVERVIEW.html):
PThread implementation:
Bionic's C library comes with its own pthread implementation bundled in.
This is different from other historical C libraries which:
- place it in an external library (-lpthread)
- play linker tricks with weak symbols at dynamic link time
So keep in mind that Bionic includes directly pthread as opposed to standard way you are used to (with -lpthread).

Categories

Resources