Here is my first public and very simple Android application!
https://market.android.com/details?id=bonifazi.nine
Nine 1.1
Marco Bonifazi
Saturday, April 16, 2011
Thursday, October 21, 2010
Android/Java: Download a file from an URL to sdcard updating a ProgressBar/ProgressDialog
Here is a simple class for download management with ProgressDialog realtime updating
String path_to_local_file = "/mnt/sdcard/marco_bonifazi.txt";
String url = "http://www.bonifazi.eu/marco_bonifazi.txt";
DownloadManager downloader = new DownloadManager(this, url, path_to_local_file, dialog);
boolean res = downloader.checkInternetConnection();
if (!res ) {
downloader.alertMissingConnection("No internet connections available");
} else {
res = downloader.download();
if (!res ) {
downloader.alertMissingConnection("URL not available");
}
}
class DownloadManager extends AsyncTask{
private URL url;
private String filename;
private ProgressDialog dialog;
private URLConnection ucon;
private Context context;
public DownloadManager(Context context, String file_url, String filename, ProgressDialog dialog) {
this.context = context;
this.dialog = dialog;
try {
this.url = new URL(file_url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
this.filename = filename;
}
public boolean checkInternetConnection() {
ConnectivityManager cm = (ConnectivityManager) this.context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm.getActiveNetworkInfo() == null){
return false;
} else {
return true;
}
}
public void alertMissingConnection(String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(this.context);
builder.setMessage(message)
.setCancelable(false)
.setPositiveButton("Ok", null);
AlertDialog alert = builder.create();
alert.show();
return;
}
boolean download() {
try {
this.ucon = url.openConnection();
this.ucon.connect();
this.execute("");
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
@Override
protected void onPreExecute() {
this.dialog.setTitle("Downloading...");
this.dialog.setIndeterminate(false);
this.dialog.show();
}
@Override
protected void onPostExecute(Integer result) {
this.dialog.hide();
this.alertMissingConnection("Download finished");
}
@Override
public void onProgressUpdate(Integer... args) {
this.dialog.setProgress(args[0].intValue());
}
@Override
protected Integer doInBackground(String... params) {
try {
int n_bytes_packet = 0;
int current_bytes_read = 0;
int percent = 0;
int n_total_bytes = this.ucon.getContentLength();
BufferedInputStream ios = new BufferedInputStream(this.url.openStream());
FileOutputStream fos = new FileOutputStream(new File(this.filename));
byte data[] = new byte[256];
while ((n_bytes_packet = ios.read(data)) != -1) {
current_bytes_read += n_bytes_packet;
percent = current_bytes_read * 100 / n_total_bytes;
fos.write(data, 0, n_bytes_packet);
publishProgress(percent);
}
fos.flush();
fos.close();
ios.close();
Log.d("fileManager", "Ok");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
Friday, October 8, 2010
OpenCV in Android, setup an image from a Bitmap pixel buffer, converting
JNIEXPORT void JNICALL Java_com_convert(JNIEnv *env,
jobject thiz,
jobject bitmap) {
const int width = 2592; // You can also get them through JNI
const int height = 1952;
IplImage* image = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 2);
cvInitImageHeader(image,
cvSize(width, height),
IPL_DEPTH_8U, 2, IPL_ORIGIN_BL, 4);
int *cv_data;
AndroidBitmap_lockPixels(env, bitmap, (void**)&cv_data);
cvSetData(image, cv_data, width * 2);
IplImage* grey_img = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1);
cvCvtColor(image, grey_img, CV_BGR5652GRAY);
cvSetData(image, NULL, 0);
image = grey_img;
DO_WHAT_YOU_WANT_WITH_YOUR_IMAGE(&image);
AndroidBitmap_unlockPixels(env, bitmap);
}
Monday, October 4, 2010
Releasing Android applications without going through the Market
It is assumed that the development framework and platform used is Eclipse, with the official Android platform plugins. It is also assumed that your are using Linux, because it's what I'm doing.
Two ways to do that:
You can do that using two different methods:
- Update the software version in your logging mechanism;
- update the software version in the AndroidManifest.xml file, it has to be coherent with the version of the previous step;
- give a Project->Clean just for the sake of being sure that all is fine;
- remember to update the “linking” of C/C++ libraries in the Java/Android platform (give a “refresh” with “F5” in the jni/lib directory, copy and paste jni/lib also into the root path of the project);
- export the package using Eclipse (with the official Android plugin) using this package name convention: YOUR_PROJECT_NAME_unaligned.apk.
- The Eclipse exporter will require for a key to be created, with security passwords: it's an operation related to certificates which is very important if the software is to be released to the official Android market;
- align the software:
- zipalign -v 4 YOUR_PROJECT_NAME_unaligned.apk YOUR_PROJECT_NAME.apk
It's an operation required by Android applications just before you can use them into the Android mobile phones.
Installation procedures
Installing the application into the Android mobile phones WITHOUT passing through the Market is an easy task.Cleanup previous releases of the software
For the sake of perfection it's better that you cleanup every kind of previous data/files you could have left related to your application: previous releases, directories created, personal configurations, etc.Two ways to do that:
- using a file manager/explorer which you will find easily on the Android Market and delete the files directly from the mobile phone;
- mounting the file system of your mobile phone onto the Linux development machine and give a
- rm -r DIRECTORY_TO_BE_REMOVED
Upload the application package
You have just to upload YOUR_PROJECT_NAME.apk file into a directory in the mobile phone.You can do that using two different methods:
- using the DDMS perspective in the Eclipse framework (“Push a file onto the device”);
- mounting the file system of your mobile phone as usb key and copy the project file using your Linux shell.
Install your application
Once you've got you file into the file system of the mobile phone, you can use whatever Application Installer or File Explorer you will find in the Android Market, locate your file and installing it.Saturday, September 11, 2010
Compile OpenCV for Android on Linux or Mac, high level hints
Android's market share is being the most growing among the Operating Systems for smartphones.
OpenCV is (from Wikipedia) a computer vision library originally developed by Intel. It is free for use under the open source BSD license. The library is cross-platform. It focuses mainly on real-time image processing.
http://sourceforge.net/projects/opencvlibrary/
The initial revision of OpenCV was in C, with the latest revision C++ has been introduced in many sections of the code.
Android NDK is the developer toolkit to plug C and C++ code into an Android application, whose default implementation language is Java (which works upon the Dalvik virtual machine, created for Android).
It must be used together with the Android SDK, which is the development framework for you Android/Java applications.
Your C++ code should be used as a library which communicates (through JNI, Java Native Interface) to the Java layer.
The main problem in order to get OpenCV compiling on Android relies on the fact that the Android NDK doesn't provide a full standard C++ ("Minimal set of headers for C++ support"), for example exceptions can't be used in the Android C++. It's probably a way to discourage people from using C++ as a native language for the Android applications. Or maybe it's because Google doesn't like C++ exceptions, especially in an embedded system context.
Luckily, there are some guys who got full C++ working for Android, you just have to download it and compile.
To use that, you have to download a tweaked C++ Android NDK, from Crystax site:
What you finally need is also OpenCV, ported to Android
Now you have to install the "Crystax" Android NDK as it was the official one, following the related guide:
You have to compile OpenCV with the Android NDK, considering it as a NDK application (check the samples in the ndk directory). The OpenCV for Android zip already contains the files/scripts to be compiled in that way.
If you want to embed your custom C++ code in it, you can easily do that, modifying the Android.mk file and restructuring the hierarchy of the directories in your project.
In my case, I simply added a subdirectory to the OpenCV one, moved there all my source code, and compiled it together with OpenCV.
Final note: remember
APP_ABI := armeabi armeabi-v7a
in the file Applications.mk if your mobile cpu supports it; your application will run much faster!
Friday, September 3, 2010
Sapthesis, a LaTeX class for your theses
sapthesis.cls.
A class for the typesetting of the Ph.D., master and bachelor theses of the "Sapienza - University of Rome".
http://biccari.altervista.org/c/informatica/latex/sapthesis.php
A class for the typesetting of the Ph.D., master and bachelor theses of the "Sapienza - University of Rome".
http://biccari.altervista.org/c/informatica/latex/sapthesis.php
Friday, August 27, 2010
Replace a string in a list of files
#!/usr/bin/python
import fileinput, glob, string, sys, os
from os.path import join
# replace a string in multiple files
#filesearch.py
if len(sys.argv) < 2:
print "usage: %s search_text replace_text directory" % os.path.basename(sys.argv[0])
sys.exit(0)
stext = sys.argv[1]
rtext = sys.argv[2]
if len(sys.argv) ==4:
path = join(sys.argv[3],"*")
else:
path = "*"
print "finding: " + stext + " replacing with: " + rtext + " in: " + path
files = [filename for filename in os.listdir(".") if not os.path.isdir(filename) and (filename[-4:] == ".cpp" or filename[-2:] == ".h") ]#glob.glob(path)
print files
for line in fileinput.input(files,inplace=1):
lineno = 0
lineno = string.find(line, stext)
if lineno >0:
line =line.replace(stext, rtext)
Create a series of md5 digests from a file
You can create md5 digests from a file, each line in the file is a different digest.
www.bonifazi.eu/appunti/createmd5.py
www.bonifazi.eu/appunti/createmd5.py
#!/usr/bin/python
import hashlib
import sys
f = open("my_lines.txt", "r")
for line in f.readlines():
line = line.strip()
m = hashlib.md5()
m.update(line)
print(line + ":::" + m.hexdigest())
f.close()
Monday, August 23, 2010
Discover the largest product of five consecutive digits in the 1000-digit number.
The code is a bit awful and not optimized... Python is really easier to work with in order to solve these kinds of algos!
product.cpp
string.txt
g++ -o product product.cpp
product.cpp
string.txt
g++ -o product product.cpp
Prime numbers generator
It's just a first C++ implementation for the Euler Project
primes.cpp
primes.h
In order to compile:
g++ -o prime primes.cpp
primes.cpp
primes.h
In order to compile:
g++ -o prime primes.cpp
Saturday, August 21, 2010
Python, PyGtk, PyOpenGL, PyWin32 all in one installer
After Gtk+ Installer, I realized also a PyGtk all in one installer, for Python 2.6 http://www.python.org/download/windows/
You can download it from here:
pygtk_windows_installer.exe
It is simply an assembling of all the different installers I previously downloaded (which are executed step by step), and you can choose.
I realized this installer using EclipseNSIS and compiling the script generated by NSIS.
Then, the script I created and that you can compile and modify using NSIS is the following:
pygtk_windows_installer.nsi
Gtk all in one installer
pygobject-2.20.0.win32-py2.6.exe
pycairo-1.8.6.win32-py2.6.exe
pygtk-2.16.0+glade.win32-py2.6.exe
PyOpenGL-3.0.1.zip
pywin32-214.win32-py2.6.exe
I'll try to keep update this installer.
You can download it from here:
pygtk_windows_installer.exe
It is simply an assembling of all the different installers I previously downloaded (which are executed step by step), and you can choose.
I realized this installer using EclipseNSIS and compiling the script generated by NSIS.
Then, the script I created and that you can compile and modify using NSIS is the following:
pygtk_windows_installer.nsi
Gtk all in one installer
pygobject-2.20.0.win32-py2.6.exe
pycairo-1.8.6.win32-py2.6.exe
pygtk-2.16.0+glade.win32-py2.6.exe
PyOpenGL-3.0.1.zip
pywin32-214.win32-py2.6.exe
I'll try to keep update this installer.
Gtk+ 2.20, Glade 3.7.1, GtkGlExt 1.2.0 all in one Windows installer
I created an all in one Windows installer,
for using Gtk+ libraries,
also with Glade 3.7.1 (GUI visual creation) and
GtkGlExt (OpenGl embedded in a GtkDrawingArea).
You can download the Gtk+, Glade, GtkGlExt global installer from this links:
Gtk+, Glade, GtkGlext all in one Windows installer
The installer simply copies the files into a Gtk directory, updating PATH environment variable.
Optionally, you can also get the different parts of the dlls which form the previous installer from the following links:
Gtk+ for Windows
Glade 3.7.1 zip for Windows
GtkGlExt installer for Windows
All the libraries are updated to the last version of today, and they seem to be perfectly useful (my project works very well).
I'll try to mantain here the most updated files.
You can view the licenses from those links.
The installer was created using
NSIS (Nullsoft Scriptable Install System
with its Eclipse plugin
The script file generated and compiled to create the installer is the following
setup_gtk.nsi
which uses this one as well
EnvVarUpdate.7z
(have a look at this page for path settings: /Environmental_Variables
Updating this installer is easy!
It is simple to maintain by everybody.
Updated to the date of this post with the latest libraries:
Glade 3.7.1!
Libglade 2.6.4
GLib 2.24.0
GTK+ 2.20.0
Pango 1.28.0
ATK 1.30.0
Cairo 1.8.10
for using Gtk+ libraries,
also with Glade 3.7.1 (GUI visual creation) and
GtkGlExt (OpenGl embedded in a GtkDrawingArea).
You can download the Gtk+, Glade, GtkGlExt global installer from this links:
Gtk+, Glade, GtkGlext all in one Windows installer
The installer simply copies the files into a Gtk directory, updating PATH environment variable.
Optionally, you can also get the different parts of the dlls which form the previous installer from the following links:
Gtk+ for Windows
Glade 3.7.1 zip for Windows
GtkGlExt installer for Windows
All the libraries are updated to the last version of today, and they seem to be perfectly useful (my project works very well).
I'll try to mantain here the most updated files.
You can view the licenses from those links.
The installer was created using
NSIS (Nullsoft Scriptable Install System
with its Eclipse plugin
The script file generated and compiled to create the installer is the following
setup_gtk.nsi
which uses this one as well
EnvVarUpdate.7z
(have a look at this page for path settings: /Environmental_Variables
Updating this installer is easy!
It is simple to maintain by everybody.
- You must only install NSIS,
- take the Windows binaries of the different tools from the official links,
- put them in c:\program files\GTK directory,
- compile setup_gtk.nsi file here aboveI updated.
Updated to the date of this post with the latest libraries:
Glade 3.7.1!
Libglade 2.6.4
GLib 2.24.0
GTK+ 2.20.0
Pango 1.28.0
ATK 1.30.0
Cairo 1.8.10
Thursday, July 15, 2010
OpenCL links
- http://en.wikipedia.org/wiki/OpenCL
- Ibm: http://www.alphaworks.ibm.com/tech/opencl/download
- Amd: http://developer.amd.com/zones/OpenCLZone/Pages/default.aspx
- NVidia: http://www.nvidia.com/object/cuda_opencl_new.html
- Khronos Group: http://www.khronos.org/opencl/
Tuesday, June 15, 2010
OProfile + Gprof2Dot commands in order to get wonderful profiling graphs
opcontrol --reset
opcontrol --start
[run your application]
[run your application]
opcontrol --shutdown
opreport -cg ./my_test /usr/lib/my_libraries | python gprof2dot.py -f oprofile | dot -Tpng -o output.png
Check the memory heap usage and its maximum (peak)
It's very useful to know the memory heap usage of an application.
The Massif tool of Valgrind framework gives you back some screenshots (you can tweak their frequency quite easily) related to the amount of the memory heap used at that moment.
It is very useful when you have limited memory per application/process.
Here are the commands:
valgrind --tool=massif --detailed-freq=1 ./test_application
ms_print massif.out.xxx > log.txt
Sunday, December 27, 2009
2007 - Orthogonal graph drawings
Application for creating segments, arcs, knots, grids and minimize orthogonal graph drawings, area compactation
Mago Google Code
Mago Google Code
2006 - Minimal application for degrees conversion
PyGtk, Glade, Python used together for converting temperature degrees representations: Celsius, Fahreneit, Reaumur, Kelvin
Subscribe to:
Posts (Atom)