Interfacing c/c++ libraries via JNI, example: tesseract

April 26th, 2009 6 comments

(Android phone – how-to/example)

If you want to use c/c++ libraries with your Android Java code, you will need to learn a bit about Android 1.5NDK, Release 1.

First you need to have installed both the Android SDK and the NDK, for these you will need to follow the install steps described on the official android-developers website. Note that the NDK contains just some of the default C libraries:
- standard C library: stdlib, stdio etc
- math library
- c++ library: cstddef, new, utility, stl_pair.h
- log library
- zlib compression library
Thusporting “any” c/c++ code to Android may not be such an easy task.

After you install the NDK there are 2 demos in your install directory: hello-jni and two-libs. One should first understand these two basic-demos before moving forward. Note: the documentation is also a must read, it resides in /docs, especially ANDROID-MK.TXT, APPLICATION-MK.TXT, OVERVIEW.TXT

To create your own code, do the following:
1. Place your c/c++ sources under sources//…

2. Write sources//Android.mk to describe your sources to the NDK build system
The Android.mk is <>
So for developers familiar with Makefile format, this should be a piece of cake.
For all available flags consult docs/ANDROID-MK.TXT

3. Write apps//Application.mk to describe your application and the native sources it needs to the NDK build system. The purpose of Application.mk is to describe which native ‘modules’ (i.e. static/shared libraries) are needed by your application.
A trivial Application.mk file would be:
APP_MODULES := APP_PROJECT_PATH :=

First line instructs where to find the java source files, second line instructs what library (libraries) are needed for the application to run.
For all available flags consult docs/APPLICATION-MK.TXT

4. Build your native code by running “make APP=” in the top-level NDK directory. Where ‘make’ refers to GNU Make, and is the name of one of the subdirectories of ‘$NDK/apps/’

To get more info about the build process use “V=1″
To force rebuild all your sources use “-B”

Observation: a useful trick to know, if you do not want to keep separated your C files from your jave files, according to the NDK preferences, you can use the the “ln” command to make a symbolic link of your directory. Syntax: ln -s

And now for the example consider the Google’s text-recognition project Tesseract, you can download the sources from here.
Now you have to compile the code (as described in the steps above). The developers have already included an Android.mk, but you will need to rewrite it a bit, in order to compile with the NDK. The modified version of the Android.mk is here.

Also running the Tesseract on the Android’s processor may take some time, so it would be cool to split the OCR process in steps, and run just some of the steps if more speed is needed. Tesseract already provides the means to do this, unfortunately, the methods are present only in the “core” and not in the JNI interface, nor close to it. To be able to do the OCR step-by-step consider the jni.cpp updated file in our code repository. For jni.c to work, you will also need to update the baseapi.cpp file.

All said about the Tesseract modifications, you will just need to build your native code (see step 4). Than you need to integrate it with your Java code.

The tesseract java-wrapper is encapsulated in OCR.java. All native method declarations are at the end of the class:

// general initialization/cleanup/setup functions
public native void classInitNative(); // init the lib (1st to be called at startup)
public native void initializeNativeDataNative(); // init allocate lib buffers (2nd to be called)
public native boolean openNative(String sLanguage); // init the api with a language

public native void cleanupNativeDataNative(); // delete the lib buffers
public native void clearResultsNative(); // api clear
public native void closeNative(); // api.end, called by the destructor automatically

public native void setVariableNative( // set a lib variable
String var, String value);

// language functions
public static native String[] getLanguagesNative(); // get the language list
public native int getShardsNative( // get the shard of the language
String lang);
public native boolean isValidWord(String word); // is the word valid according to the installed language

// aux functions before OCR
public native void setImageNative( // copy the image to the internal api buffers
byte[] image, int width, int height, int bpp);
public native void setImageNative( // copy the image to the internal api buffers
int[] image, int width, int height,
boolean bBWFilter, boolean bHorizDisp);
public native void releaseImageNative(); // release the internal api buffers
public native void setRectangleNative( // set the rectangle where OCR will focus
int left, int top, int width, int height);
public native void setPageSegModeNative( // set the page segmentation mode
int mode);

// OCR
public native String recognizeNative( // do OCR over the parameter image
byte[] image, int width, int height, int bpp);
public native String recognizeNative(); // do OCR over the image in the api buffers (all ocr)
public native String recognizeNative(int nopass); // do OCR over the image in the api buffers
// (options: 0=all, 1 or 2 passes)

// aux functions, to be run after OCR
public native int meanConfidenceNative(); // mean confidence (last OCR)
public native int[] wordConfidencesNative(); // confidence for each word (last OCR)
public native String getBoxText(); // get the box for each letter

// debug functions
public native void closeDebug(); // clean close the debug (if any)
public native String libVer(); // get the lib version

public int mNativeData; // storage space for the library’s internal buffers

/* this is used to load the ‘ocr’ library on application
* startup. The library has already been unpacked into
* /data/data/com…/lib/libocr.so at installation time by the package manager.
*/
static
{
System.loadLibrary(”ocr”);
}

Code example: you may find useful to see the whole implementation of this in the Mezzofanti application – google code. The code is released under Apache License, ver 2.0, so you can freely use it free of charge in your own code.

Categories: android c/c++ Tags: ,

Using the Camera

April 15th, 2009 No comments

(Android phone – how-to/example)

In order to use the Android Camera you need your activity to implement android.view.SurfaceHolder.Callback, example:

public class CameraTest extends Activity implements SurfaceHolder.Callback
{
// Camera object
private Camera m_oCamera;

In your Activity, you’ll also need:
- an android.view.SurfaceView.SurfaceView to display the picture preview and
- an android.view.SurfaceHolder that is an <<abstract interface to someone holding a display surface. Allows you to control the surface size and format, edit the pixels in the surface, and monitor changes to the surface.>>

private SurfaceView m_oSurfaceView;
private SurfaceHolder m_oSurfaceHolder;

Than you initialize them:
m_oSurfaceView = new SurfaceView(this);
setContentView(m_oSurfaceView);

Connect SurfaceHolder to SurfaceView
mSurfaceHolder = mSurfaceView.getHolder();

After that, you will need to add a callback to the surface holder. The callback will inform you when the surface was created/changed/destroyed:
mSurfaceHolder.addCallback(this);

Set surface not to have its own buffers in order to immediately display the image
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

Also you may want to implement surfaceCreated(SurfaceHolder holder). This method is called when surface is created, for example to initialize the camera object:

public void surfaceCreated(SurfaceHolder holder)
{
m_oCamera = Camera.open();
}

The method surfaceChanged(SurfaceHolder holder, int format, int width, int height) is called after surface is created. Here you can start to preview and set camera/preview parameters.

public void surfaceChanged(SurfaceHolder holder,
int format,
int width,
int heigth)
{
/* if the preview is already running (for example the application was paused) */
if (m_bPreviewRunning)
m_oCamera.stopPreview();

/* set preview parameters */
Camera.Parameters params = m_oCamera.getParameters();
params.setPreviewSize(width, heigth);
m_oCamera.setParameters(params);

try
{
/* Sets the SurfaceHolder to be used for a picture preview. */
m_oCamera.setPreviewDisplay(holder);
}
catch (IOException e) {}

/* start camera preview */
m_oCamera.startPreview();
m_bPreviewRunning = true;
}

The surfaceDestroyed(SurfaceHolder holder) method is called immediately before a surface is being destroyed. You must stop camera preview because the surface will no longer exist.

public void surfaceDestroyed(SurfaceHolder holder)
{
m_oCamera.stopPreview();
m_bPreviewRunning = false;
m_oCamera.release();
}

When you want to take a picture you will call:

m_oCamera.takePicture(null, null, m_cbPicture);

This will take a picture and after the picture is jpeg compressed the callback m_cbPicture will be called.
NOTE: In Android SDK 1.5 the raw picture callback has a bug and image buffer is empty.

This is how your jpeg callback should look like.
Camera.PictureCallback m_cbPicture = new Camera.PictureCallback()
{
public void onPictureTaken(byte[] data, Camera c)
{
/* do something with picture from data array
do something with picture from data array
Hint: use the Bitmap class, but do not do much processing in the callback, send yourself a message with the message handler, and do the processing at some other point (maybe in a different thread if you will implement lots of processing)
*/

/* restart camera preview */
m_oCamera.startPreview();
}
};

Code example: you may find useful to see an implementation of this in the Mezzofanti application – google code. The code is released under Apache License, ver 2.0, so you can freely use it free of charge in your own code. Consult the class CameraManager.java.

Categories: android java Tags:

Obtain root access on your Android HTC Dream phone

March 25th, 2009 No comments

(Android phone – how-to)

Step 1 – Preparing to Get Root on your Phone
Getting root on your phone involves exploiting a security hole that existed in an early version of Android. The versions with the security holes are known as RC19, RC28, and RC29 (or RC7 or lower for UK phones). As of RC30 (RC8 on UK), Google patched the security hole so that your phone can not be rooted (don’t worry, we can still root it!).
To check your version, go to the Home Screen, click your menu button, choose “Settings”, and then click “About phone”.
If you have US-RC30/UK-RC8 or higher, you will first need to downgrade your phone to a previous version. (Skip these steps otherwise)

1. Format your phone’s SD card to FAT32 mode:
Hook your phone up to your computer using a USB cable and then wait for the notification to show up in your title bar of your phone.
Click the notification, and then click “Mount”.
A new removable disk should show up on your computer. Right click it and select Format, and select FAT32 as the file system type.
Note: Do not format the sdcard directly inserting it in your computer. You may find that formatting FAT32 with your computer not to work. This may sound silly, but there may be small differences (that matter) between the FAT32 formats. In this case, do format the sdcard with another phone.

2. Download and unzip the RC29 or RC7 image file. Copy the DREAMIMG.nbh file to the SD card. (RC29 for US, RC7 is for UK)
3. Turn the device power off.
4. Hold Camera button, and press Power button to entry bootloader mode. You should see a gray/white screen with instructions to flash your phone with the update on your SD card. If you don’t see that, make sure you followed the instructions properly.
5. As per the on-screen instructions, press the Power button to start upgrade procedure. DO NOT DO ANYTHING TO INTERRUPT THIS PROCESS.
6. After it is finished, perform the restart your phone.

Step 2 – Rooting your RC29 or lower phone:
On RC29 phones and lower, anything you type into your keyboard is also being run in a hidden console with root permissions. More information regarding that at the bottom of this post. But, to get root access, do the following:

Instructions:
1. Download recovery.img and copy it to your SD card (see the previous instructions on how to copy from your computer to your Phone’s SD card).
2. Download the Hard SPL and copy the zip file to the SD card.
3. All files must be on the root of your SD card.
4. Restart your phone. Wait for your phone to start up fully and show the home screen.
5. After your phone starts up, hit the enter key twice, type “telnetd” and press enter. (Yes, it will start up a contact search, don’t worry. Just type it.)
6. Download an Android “Telnet” application from the Market and connect to localhost.
7. If you connect successfully, you will have a root prompt “#”.
8. Type the following into Telnet (these commands will give you root access easier in the future):
mount -o rw,remount -t yaffs2 /dev/block/mtdblock3 /system
cd sdcard
flash_image recovery recovery.img
cat recovery.img > /system/recovery.img
Now you have root!

Step 3 – What To Do Now That You Have Root
Now that you have root, you will want to apply “Hard SPL” to your phone. HardSPL is what will allow you to apply flash images from other regions (like UK on US phones, and vice versa), create full backups of your phone, install the latest build from the Android source, and usually resurrect your phone if it is “bricked”. You have already downloaded the file to your SD card, so now you can apply it.
1. Power off your phone.
2. Start up in recovery mode by holding home and pressing power.
3. You will now enter recovery mode. You should see an exclamation.
4. If you do not see a menu on screen, press Alt-L to show the menu.
5. Press Alt-S to apply the update from the SD card.
6. After the update is complete, hold Home and press Back to restart.

And now, the last step! You are still running an old version of Android, but you want to upgrade to the latest and greatest update! You can do this, and not lose root by downloading modified versions of the updates.

First, choose a modified image from the list below:

ADP1.1: (md5: bacc58302e0b239d66c7bcc8db6c434b)
http://jf.odiness.com/v1.41/JFv1.41_…ronment.tar.gz
http://android-dls.com/forum/index.p…rb_v=viewtopic
http://andblogs.net/2009/01/jesusfre…mages-are-out/

RC33: (md5: f24b6c237775147cb4bc42efc2393973)
http://jf.odiness.com/v1.41/JFv1.41_…ronment.tar.gz
http://android-dls.com/forum/index.p…rb_v=viewtopic
http://andblogs.net/2009/02/new-rc33…om-jesusfreke/

RC8: (md5: de2d0d34adbb4015ee3aa5e4e7ca3c07)
http://jf.odiness.com/v1.41/JFv1.41_…ronment.tar.gz
http://android-dls.com/forum/index.p…rb_v=viewtopic
http://andblogs.net/2009/01/jesusfre…mages-are-out/

1. Download the image.
2. Rename it to update.zip.
3. Copy it to your phone’s SD card.
4. Turn your phone off.
5. Start up in recovery mode by holding Home and pressing Power.
6. Press alt-W to wipe your device. (You need to do this, or the device may hang at the flashing Android screen)
7. Press alt-S to apply the update.

Note: the original message was posted here.

Some other interesting links you may consider:
1. You can install a full Linux distribution, like Debian.
2. You can install the latest Android build directly from the Android source tree.
3. Customize your boot image.
4. Create custom ROMs.
5. How to go back to official cupcake 1.5 dev.
6. Video of the steps above

Oracle & OS Outsourced Maintenance

January 16th, 2006 No comments

Certified in:
• Linux Administration OCA
• RAC Administration OCP
• Oracle DBA 8i-11g OCP
• Oracle Development OCP
• Oracle Performance Tuning OCP

Oracle 8i-11g DBA

Database Administration
With different backgrounds (administration, design and development, support), our team of expert Oracle Certified Professionals can cover various areas of the Oracle Database Administration and often go even beyond the Oracle environment, being able to easily perform any of the following:
• Installations Upgrades and Migrations
• Instance Management
• Database Healthchecks
• Instance Performance Tuning
• Database Security
• Backup and Recovery
• Disaster Recovery
• ETL

Performance Tuning
Reactively and proactively investigation of any performance problem, with top down or bottom up investigation techniques and hence being able to easily address any of the following:
• Instance Tuning
• Memory Tuning
• Contention Tuning
• Query Tuning
• Database Design
• Application Tuning
• IO Tuning
• OS tuning

High Availability
Based on the business requirements and SLA, we offer consultancy regarding the best possible solution for your business needs, along with the installation and configuration of the required tools:
• Clustering (RAC, CRS, ASM)
• DataGuard
• Replication
• Grid Control

Backup and Recovery
• Backup and Recovery strategy and implementation
• Recovery Manager (RMAN)
• DataGuard

Security
• Basic Database Security (authentication, authorization, access control, auditing)
• Access Control (VPD, FGAC, OLS)
• Network Services Security
• Advanced Network Security
• Encryption (TDE)
• Database Vault
• Audit Vault
• Strong Authentication (OID, OAS)

Categories: services Tags: ,

Hardware Outsourcing Services

January 16th, 2006 No comments

1. Electronics Design and Architecture
•    Custom solutions with microcontrollers, DSPs, FPGAs and various processors.
•    PCB design and manufacturing
•    Robotics
•    Process control (including custom solutions)

2.    Firmware for Microcontrollers / DSPs / FPGAs  / Processors
•    Freescale DSP56F800/E family
•    Frescale Starcore SC140
•    TI MSP430 family
•    ARM
•    Atmel AVR
•    X86 family
•    Microchip PIC family
•    MSC51 family

3.    Custom Designed Hardware Interfacing
•    PCI / ISA / PC104 / IDE / USB
•    External Memory: CompactFlash / SecureDigital / MMC
•    Video: LCD, touch screen, Video-IO, NTSC, PAL-TV, Digital Video Recorder, Digital Camera
•    Identification: Biometrics, RFID
•    Communications:
ISM, WiFi, Bluetooth, ZigBee / Ethernet / IrDA
Modems (interfacing and design): analog and digital radio, GSM, iDEN, CDMA, GPRS, Tetra, SMS / Serial communications (RS232, RS485, RS422, CAN)

4.    Data acquisition and processing
•    Analog and Digital Input/Output cards
•    Virtual instrumentation
•    Motion control
•    Industrial automation

5.    Software defined radio
•    CDMA signal processing
•    Software Viterbi decoder
•    Downconverters
•    Audio codecs (G711, G722AB)
•    Software modems

Categories: services Tags: ,