Line 1: Line 1:
==PAGE IN PROGRESS==
+
==Coding for Android on Windows, by [[User:Ctendyck|Christian Tendyck]]==
 +
This page shall help you to get started to write applications for an Android phone, using [http://www.eclipse.org/ Eclipse] with the [http://developer.android.com/sdk/index.html Android SDK] and [http://developer.android.com/sdk/ndk/index.html NDK] in general. The last two parts of this page are more related to my current work in the research lab and shows you how to do image processing on an Android phone.<br />
 +
I have devided this page in several subpages. So it is easier to write comments to special topics and this page doesn't get to messy. The topics are:
 +
# [[Coding_for_Android_on_Windows_-_Set_up_your_machine|Set up your machine and create a virtual device]]
 +
# [[Coding_for_Android_on_Windows_-_First_projects|First project and the NDK]]
 +
# [[Coding_for_Android_on_Windows_-_Image_processing_-_Java_Code|Image processing on an Android phone - Java Code]]
 +
# [[Coding_for_Android_on_Windows_-_Image_processing_-_C_Code|Image processing on an Android phone - C++ Code]]
  
=Coding for Android on Windows, by [[User:Ctendyck|Christian Tendyck]]=
+
Feel free to write comments and edit my work or add content.
This page shall help you to get started to write applications for an Android phone, using [http://www.eclipse.org/ Eclipse] with the [http://developer.android.com/sdk/index.html Android SDK] and [http://developer.android.com/sdk/ndk/index.html NDK] in general. The last part of this page is more related to my current work in the research lab and shows you how to do image processing on an Android phone
+
 
+
 
+
==Set up your machine==
+
Before you can start coding, you have to download and install a few programs. First of all there is [http://www.eclipse.org/ Eclipse]. If you are used to Java, you will probably know how to use Eclipse. Furthermore you need to download the [http://developer.android.com/sdk/index.html Android SDK (Software Development Kit) and ADT (Android Development Tools)]. The [http://developer.android.com/sdk/ndk/index.html NDK] is just needed if you are planing to use native code. That means to write a part of your program in C or C++ or to use C-libraries. Then you also have to install the C/C++ Development tool for eclipse. You can do this via "help/install new software" in eclise. Follow the instructions on [http://eclipse.org/cdt/downloads.php this site]. To compile C code in eclipse, you need another tool called [http://cygwin.com/ Cygwin]. With Cygwin you can emulate some kind of a Linux console. This is the only way I know to compile C-Code in Eclipse on a Windows machine. Some really common standard C++ libraries cannot be used when compiling with the NDK. But there is a modified version of it called [http://www.crystax.net/de/android/ndk Crystax] that allows you to use these libraries.
+
 
+
 
+
==Create a virtual device==
+
If you do not have an Android phone on your own or if you do not want to upload your apps on your phone everytime to check if your code is working, you can create a virtual device. A virtual device is an emulator of an Android phone. It is very easy to create it. I assume you have already installed the Android SDK. Start Eclipse and click on "Window/Android SDK and AVD Manager". Install the packages you need. That means, download the package with the correct plattform version you want to code for. Then, click "virtual device/new". Choose a name and a target and if you want to, you can also create a virtual SD Card. This is very helpful if you are planing to use the SD Card on your real phone as well. Eclipse will create a file "sdcard.img" that is as big as you have chosen it to be. To mount it as a virtual hard disk, you need a program like [http://www.ltr-data.se/opencode.html/#ImDisk ImDisk Virtual Disk Driver]. You can create folders on your virtual SD card and use it as any other SD Card.
+
 
+
 
+
==Your first projects==
+
The [http://developer.android.com/resources/browser.html?tag=tutorial Android Developers page] offers some tutorials that helps you to get started. As usual, start with a [http://developer.android.com/resources/tutorials/hello-world.html Hello World]. It is not really complicated. The page shows you from scratch how to create a new Android project. This page also shows you again how to create a virtual device. Remember for your following projects, that it is always better to use .xml files for entering text that shall be displayed instead of actually writing it directly into your code. That helps to change the text afterwards and to convert your application to other languages. How to use .xml files is explained in the second part of the article mentioned above.<br />
+
Another tutorial that helped me a lot is the [http://developer.android.com/resources/tutorials/notepad/index.html Notepad Tutorial]. To be honest, this tutorial is much more complicated than the other one and I haven't really understood everything when I read it, but it definitely helps you to understand, how an Android project works and how you can connect different parts of your program with each other or even with other applications.
+
 
+
 
+
==Using the NDK==
+
Now you know the basics of coding for an Android phone by just using Java. Integrating native code written in C or C++ is more complicated, but once you have understood how it works, you will get used to it very quick. I assume you have already installed the NDK. If not, follow the steps described [http://developer.android.com/sdk/ndk/index.html here]. The NDK uses the JNI (Java Native Interface). JNI cannot just be used for Android programming, but also for simple Java coding. I think the [http://en.wikipedia.org/wiki/Java_Native_Interface Wikipedia article] is a good way to start understanding the JNI. Afterwards, read carefully through the NDK documentation and understand what the intent of the NDK is. Also read [http://developer.android.com/sdk/ndk/overview.html#samples this] page. On this page you also find all the tutorials you have just downloaded together with the NDK. Read the chapter ''Exploring the hello-jni Sample''. I do not really like the instructions you find on this page and as I was struggling with them, I try to give you another explanation how to get this application running.<br />
+
Create a new Android project just like explained on the mentioned page above and open the project ''hello-jni'' from the sample folder. I guess after reading the NDK documentation you are more or less familiar with the structure of this program. An Android app does not have a Main function, but an ''onCreate'' method. This method is called when the application is started. So we have to say inside this method how the app shall look like in the beginning. As we just want to display text, we have have to create a ''TextView'':
+
<pre>TextView  tv = new TextView(this);</pre>
+
We want to receive the content of this TextView from a JNI method:
+
<pre>tv.setText( stringFromJNI() );</pre>
+
The method ''stringFromJNI()'' must be declared inside the Java part of the program. It is very important to use the key word ''native''. Otherwise you would become an error. So the declaration looks like this:
+
<pre>public native String  stringFromJNI();</pre>
+
To tell your application where to look for the native methods, you have to include the library you will create later from your C code:
+
<pre>static {
+
    System.loadLibrary("hello-jni");
+
}</pre>
+
The rest has to be done in C code. Open the file ''Android.mk'' and ''hello-jni.cc'' in the folder ''jni''. Take a look on the Android.mk. This is not really the code you want to ride but more like an interface between the C and the Java code. You always have to have such a file that looks similar to this one when you are using the NDK. It is always called the same.<br />
+
Your actual code has to be in the C file. Take a look on this file. The methods are always named in a special way: You take the package name, followed by the class name and the actual method name. These parts of the name are connected with underscores and also all the dots (for example in the package name) become underscores. So it's better/necessary to avoid underscores within your class or method names. Otherwise you will probably become an error. Even if you do not need any attributes, you always have two parameters belonging to your method. But you do not have to really care about them. As long as you are creating objects within your C code and work on these objects, you can do it like you are used to. But as you see in the code, you have to work with pointers when you want to work with values/objects received via the JNI or if you want to return results back. In our case, we are just creating a ''string s'' and return it to the Java part of the program.<br />
+
Now we are finished with the code and can compile it. I do not really understand what they are doing in the instructions on the page I have just mentioned above. So here is the way I compile the C/C++ Code and create the library:<br />
+
* Start Cygwin to use the Linux-like console.
+
* Navigate to the JNI-folder within your project. For example "cd /cygdrive/c/java_workspace/projectname/jni". ''Cygdrive'' is the command needed by cygwin to navigate to the correct folder and the ''c'' indicates that your project is on the C drive of your computer.
+
* Now you have to use the Android NDK to build the library. As explained above, I am using the modified version ''Crystax'' of the NDK.
+
* Type "/cygdrive/c/Android/Crystax/ndk-build" or something like that into cygwin, depending on where you have stored the Android NDK. Do NOT write "cd" in front of your command.
+
* If you do not get an error, your C-file has been compiled. Refresh your project in Eclipse and you will see a new folder ''obj'' that contains your library.
+
Now everything has been done and we can start the application. You can either use your Android phone or the virtual device you have created as explained above to test the app. Usually it is much faster to upload the app to your phone than using the virtual device.
+
 
+
 
+
==Image processing on an Android phone - Lowpass filter an Image==
+
My first own project was to lowpass filter an image on an Android phone using a Gaussian filter. Therefore it should be possible to either take a picture with the integrated camera or to open an an already existing image from the gallery on the phone. The filtering itself should be done in native C++ code. So all that has to be done in Java is to prepare the image to get filtered by the native code.
+
 
+
[[Image:AndroidLPupload.zip|Here]] is the source code for this project.
+
 
+
<table> <tr>
+
<td width="600px">
+
Take a look at the layouts on the right first. You can find them in the folder ''res/layout'' inside the project. The first one is the layout that is displayed when the application is started. The only thing you can do is to choose between taking a new picture and loading a picture from the phone.<br />
+
The second layout appears when a picture was taken/chosen. Here you can deside between choosing a new picture or lowpass filter the displayed image.<br />
+
The layouts are typically written as a .xml file. You can switch from the graphical layout to the .xml code in Eclipse. The first line of this code is always the same. In the second and the following lines we are creating a vertical layout. That means that the following elements are placed one below the other. ''Fill_parent'' means that your content is stretched to the entire width of the screen, while ''wrap_content'' means that your content is displayed as small as possible.<br />
+
The First element of the starting layout is a text. So we are using a ''TextView'' to display it. The text that shall be displayed can be found ''strings.xml''-file. That makes it easy to convert the app to another language, as the only thing that has to be changed is this file. The second ''TextView'' is similar.<br />
+
The buttons shall be arranged side by side. So we are creating a horizonal layout as the third element of the vertical layout and creating the buttons inside this new layout. The text of the buttons can also be found in the ''strings.xml''-file. Both buttons get it's own id. So it is easy to use them in the actual code. You will see this later.<br />
+
The ''design.xml''-file is similar. You are just using an ''ImageView'' instead of a ''TextView''.
+
</td>
+
<td>[[Image:Layout1.png|right|200px]]</td>
+
<td></td>
+
<td>[[Image:Layout2.png|right|200px]]</td>
+
</tr></table>
+
 
+
 
+
The ''strings.xml''-file is quite easy to understand. You just need to specify a name for your String, so it can be used somewhere else in your code, and the actual text you want to get displayed.
+
 
+
Now let's take a look on the Java code. I assume that you already know something about coding for Java and so I do not want to explain every single line of the code and want to concentrate more on the Android specific code instead.
+
 
+
 
+
==PAGE IN PROGRESS==
+

Revision as of 14:22, 7 January 2012

Coding for Android on Windows, by Christian Tendyck

This page shall help you to get started to write applications for an Android phone, using Eclipse with the Android SDK and NDK in general. The last two parts of this page are more related to my current work in the research lab and shows you how to do image processing on an Android phone.
I have devided this page in several subpages. So it is easier to write comments to special topics and this page doesn't get to messy. The topics are:

  1. Set up your machine and create a virtual device
  2. First project and the NDK
  3. Image processing on an Android phone - Java Code
  4. Image processing on an Android phone - C++ Code

Feel free to write comments and edit my work or add content.

Alumni Liaison

ECE462 Survivor

Seraj Dosenbach