Android - after click on button execute c++ code(openCV) - android

I wrote 300 lines script (openCV) in c++ and I would like to execute the script after clicking on button in application for android. The script takes a image as an input and the output is edited image. I read about NDK but I am not very skilled in C++/Java, so I do not understand it. I am just learning how to do it. Can you advise me how to do it simple as possible?
Let's say I have simple UI with one button and after click it execute hello world written in c++.
I know how to code android application but i do not know how to put c++ with android together.
Thank you in advance!

Rather than complicating the issue with C++/JNI/NDK you could just rewrite your 300 lines in Java instead. OpenCV has a Java binding that is basically identical to the C++ calls. Java syntax is pretty close to C++ so you already have most of your coding work done. You will have to learn enough Java to handle the basic App framework and button interaction anyway. Go to the OpenCV site, http://OpenCV.org, and get a copy of the Android SDK and plug it into your development environment (Eclipse or Android Studio). I am using OpenCV-2.4.10-android-sdk, but I think 2.4.11 is out. In your IDE you just import as existing Android project. It has sample code built in that you can test right away and then modify to add your functionality.
There are some good tutorials available on setting up an IDE to add native code, NDK, JNI, but it is not really necessary to get OpenCV functionality.

Related

How to open my Android native application with Xamarin.forms?

I've been developing an Android application with Android Studio and now I need to be able to open it with Xamarin.forms in Visual Studio (it's a task that my teacher gave me, and it's because a classmate has been creating the iOS version and now we will combine them into one project).
I have to say that I'm pretty much a noob and been looking a few tutorials that talk about Dependency Service and other stuff, but don't seem to quite cover what I'm looking for (or, at least, not clearly enough).
If you have a precise video that explains simply how to do this, I would be grateful if you share it with me.
I thank you for your time and wish you a great day :)
It is impossible at even now in current version of Visual Srudio .
Every file in Xamain is different with Native Android . The Java code can not be directly copyed to use in Xamarin , you should translate them to C#.
Having a look at this document: Porting Java to C# for Xamarin.Android .
There are two ways to port Java code to C#. The first way is to port the code manually. ..The second porting methodology is to try and automate the process by using a code converter...There is often a non-trivial amount of work involved with bootstrapping an automated code conversion tool, and this may prove to be a barrier to use. In many cases, it may be simpler and easier to port Java to C# by hand.

How to execute Python script from Java code in Android

I'm trying to make an standard Android application execute a python script that could return values to Java, but I'm facing a lot of issues.
Jython doesnt support this in the Android environment, SL4A is a dead project, Kivi seems to be an full stack framework that do not use Java at all and QPython is SL4A that works on Android 5+ but almost don't have documentation.
I wanted something like:
// From INSIDE my Java Code
new PythonEngine().execute('a = 1 + 1').getInt('a')
With QPython I found an example that executes an arbitrary code, but I couldn't figure how to get the outcome and how to run the script without opening a new activity that waits for user input to come back.
Is that possible? The example can be found here QPython Java Sample
I don't think any of thoses projects will help. For example, Kivy drive the Python execution, even it it's started from Java.
If you have an application in Java, but want to start a Python Interpreter, i guess solution using https://code.google.com/p/android-python27/ might be a start. Unsure if they have a Java interface in top of the Python C-API.

Are there any simple way to use c/c++ source code directly on my android application?

I am currently working on an android application that evaluate images in different aspects, and I found that there are lots great open source algorithms can be used.
Problem 1: Most of the algorithms are designed on c/c++/matlab languages that cannot be applied directly.
I've search that NDK is a tool that allows us develop android application by other languages, but the setup procedures are quite complicated that I stuck for days. So before I go further on it, I would like to first ask whether I can include other's c/c++ source code directly like calling java library?
Problem 2: For example, I would like to use Point Matching algorithm's source code in my application, but there are lots files inside as it's just source code but not library/plugin. What are the steps to apply the require functions in my android application?
(the most desired way is to blindly input some images to the alogrithm, and it returns the results like calling functions, which I dont have to understand the working principle of it.)
You can't directly use other C++ libraries, you have to build them for Android first using NDK. If there is a version of the library built for Android, then, of course you can use it directly by linking to it using NDK.
You have two options here. First, you create a regular Java application for Android, write a C++ wrapper for handling calls to native side and build the necessary source files using NDK. Your java application will make calls to wrapper using JNI and the wrapper will call your actual C++ functions as in Java->JNI wrapper on C++->Your actual C++ source.
Second option is going fully native, which will leave out Java, JNI calls and the wrapper. You can access your source files directly as if you were writing a desktop C++ application. In the end you will have to build using NDK but this step is required in any case.
As a suggestion, you can also take a look at OpenCV for image processing purposes. It has libraries built for Android, all you will have to do is to link them.
Short version.
Download opencv4android library. Import it in eclipse and see if everything is fine (compile errors, output, etc.).
Secondly, try to import face detection application and try to understand how it works.
It has a java part and a native part.
In order to understand how it works you must understand how java interacts with C++, more or less how NDK works.
One important aspect is to learn how to create your interfaces in C++, based on the native ones defined in java. When you got there then you can try creating your own application.
After that you can come here and ask specific questions.

Porting Visual C/C++ to Android

In my recently started internship (I'm studying computer science), I have to port a somewhat big existing Project to Android. It is a Visual C/C++ Project which in the end should run on Android using of course the NDK. The problem is that I don't know a thing about porting software, since this is not covered in my studies...
I had a few days to get to know the project I will be working with a little.
A few words about the complexity:
It is a Visual Studio solution which consists of 28 projects of which most are kind of big...
And now I need to develop a concept for porting it to Android, and this is why I'm writing this. I have absolutely no idea know where to start, also I don't know how to get to a concept for porting it. I tried searching online for some stuff or for some books covering this, but I found nothing.
Of course I know, that basically I have to remove everything that is Visual/Windows specific and replace it with appropriate Android/Linux functions or implement something new if there is nothing corresponding on Android.
But since this project is somewhat huge, I can't just try to compile it with the correct compiler and work through the error messages, this will lead to nowhere, I guess...
So now my actual question:
Is anybody experienced about this or something similar and can give some tips how to start with it? As I said, I first need a concept. Something more general about how to start and deal with porting a software project of this size is also very welcome, since I don't know nothing about it...
I think that you're approaching the whole matter in the wrong way. There are several tools that will allow you to compile C# and visual C++ and other visual languages from their respected IDEs to the android platform. For example there is mono for android . Mono is a plugin for visual studio which helps you create android projects using visual studio and .net technologies. I think you should focus your research on tools like this because changing all of the code manually is nearly impossible and too time consuming. Here is a page that will help you get started with mono for android. Good luck.
See this answer Android NDK with Visual Studio.
Personally I don't use this, but It seems to be a nice solution to your problem.
Like you, in the past, we had to port a big Visual C++ library project to Android, we proceeded in this way:
Try to compile the main library interface (by creating the relative Android.mk)
By using linking errors, you should guess other projects to be compiled and create the mks for them
Cycle until you have no more linking errors.
You didn't tell about the source code size of the project (how many thousands of lines of source code).
If the code size is big enough (e.g. at least 100 KLOC), you could consider customizing the GCC compiler (assuming a recent 4.6 or 4.7 version), perhaps using MELT (a high level domain specific language to extend GCC), or painfully coding in C a GCC plugin. This GCC customizing approach is worthwhile only for large code bases (you'll need several days to master GCC internals).
On the other hand, many well designed applications may have packaged their operating system functions inside a well defined interface and library. You might consider porting your application to Qt or some other cross-platform library.
We need more details about your project to help you more.

C++ File adding

Can anyone please tell me how to add a C++ file in to an android project? Is there any method to import classes other than java classes?
The answer is that, you can't really add a C++ file directly to a project, but you can compile it and load it into the code that runs in your process and interface to it using the JNI. This is a way to interface native code to Java. However, be aware that you can't really do that much with the JNI. Getting access to standard Android things like UI, Intents, service connections, etc.., these are all somewhat more difficult to use in native code. And you certainly can't take a UNIX app "off the shelf" and stick it on Android by using the JNI. this is a fairly good looking tutorial on the JNI with Android. However, like I said, using the JNI is not an excuse for learning java and the Android SDK. The main reasons people use native code are for utility code (like crypto stuff) and performance (for example, quite a few Android games use the NDK)..
You have to use android NDK. Just download it and refer from android official site.

Categories

Resources