Thursday, May 17, 2012

Face Detection in Android


Here we go with the example of Face Detaction in android.
It is pretty much simpler then I expected.
1) Create a project… name it FaceDetection
2) Create A class FaceDetectionActivity.java
In your onCreate method write setContentView(new MyView(this)); after call to super.
@Override
public void onCreate(Bundle savedInstanceState)
{
     super.onCreate(savedInstanceState);
     //setContentView(R.layout.main);
    setContentView(new MyView(this));
}
So our next step is to create MyView Class.

public MyView(Context context)
{
     super(context);
     BitmapFactory.Options bitmapFatoryOptions=new BitmapFactory.Options();
     bitmapFatoryOptions.inPreferredConfig=Bitmap.Config.RGB_565;
     myBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.faceswapping,bitmapFatoryOptions);
     width=myBitmap.getWidth();
     height=myBitmap.getHeight();
     detectedFaces=new FaceDetector.Face[NUMBER_OF_FACES];
     faceDetector=new FaceDetector(width,height,NUMBER_OF_FACES);
     NUMBER_OF_FACE_DETECTED=faceDetector.findFaces(myBitmap, detectedFaces);
}
Let me explain the constructor…
·      For FaceDetection we need to convert in bitmap format that too in RGB_565.
·      Now get the image from the drawable folder. Get the width and height of image.
·      Now the reason I feel this API the simplest is coming now.
·      You need to pass the number of faces you want to detect.It will return the array of Face type.Last three lines is having logic for that.So you must declare an array with the size of number of faces you want to detect. 

Now when the face gets detected we will draw a red rectangle on it.For that we need to write few lines in our onDraw method.

@Override
protected void onDraw(Canvas canvas)
{
     canvas.drawBitmap(myBitmap, 0,0, null);
     Paint myPaint = new Paint();
     myPaint.setColor(Color.GREEN);
     myPaint.setStyle(Paint.Style.STROKE);
     myPaint.setStrokeWidth(3);

     for(int count=0;count<NUMBER_OF_FACE_DETECTED;count++)
     {
            Face face=detectedFaces[count];
            PointF midPoint=new PointF();
            face.getMidPoint(midPoint);

           eyeDistance=face.eyesDistance();
           canvas.drawRect(midPoint.x-eyeDistance, midPoint.y-eyeDistance, midPoint.x+eyeDistance, midPoint.y+eyeDistance, myPaint);
      }
}

* drawRect is taking five parameter left x,y and top x,y coordinate.From that given pint it will start drawing rectangle.We need to pass paint object also.
Find working example in and attachment.

How to FTP file Upload and Download in android?



Requirement :
·      FTP hostname
·      Username and Password of FTP server
Features :
·      Android (local) and remote (FTP) file browsers.
·      Upload and download with resume support.
·      Folders selection support for upload, download and delete.
Code for Upload file to FTP Server :
FTPClient ftp = new FTPClient();
                System.out.println(ftp.connect(host)[0]);
        ftp.login("username of FTP", "password of FTP");
        Log.v("log_tag", "path : " + pat.getText().toString());
        ftp.upload(new java.io.File(pat.getText().toString()));
Code for Download from FTP Server :
               FTPClient ftp = new FTPClient();
        System.out.println(ftp.connect(host)[0]);
        ftp.login("username of FTP", "password of FTP");
        File fileDownload = new File("/sdcard/testnew.xml");
        fileDownload.createNewFile();
        // File.createTempFile("testnew", ".xml");
        ftp.changeDirectory("dhaval");
        // ftp.download("testnew.xml", fileDownload);
        ftp.download("testnew.xml", fileDownload,
                        new FTPDataTransferListener() {

                            public void transferred(int arg0) {
                                Log.v("log_tag", "This is for tranfer");

                            }

                            public void started() {
                                // TODO Auto-generated method stub
                                Log.v("log_tag", "This is for started");
                            }

                            public void failed() {
                                Log.v("log_tag", "This is for failed");
                            }

                            public void completed() {

                                Log.v("log_tag", "This is for completed");

                            }

                            public void aborted() {
                                Log.v("log_tag", "This is for aborted");

                            }
                        });
Reference link
FTP jar : http://sourceforge.net/projects/ftp4j/

SerarchView For Android 3.1


SearchView


A widget that provides a user interface for the user to enter a search query and submit a request to a search provider. Shows a list of query suggestions or results, if available, and allows the user to pick a suggestion or result to launch into.

Example

Step - 1:

- Create /xml directory in /res.
- Create searchview.xml in /res/xml.
- Add <searchable /> tag in searchview.xml.
     <searchable
         xmlns:android="http://schemas.android.com/apk/res/android"
         android:label="SearchView">
     </searchable>
- Register SearchableActivity in AndroidManifest.xml file under the <application /> tag.
    <activity android:name=".SearchableActivity">
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>

        <meta-data android:name="android.app.searchable"
            android:resource="@xml/searchview" />
        </activity>
Step - 2:
- Add SearchView in /res/layout/main.xml
    <SearchView
        android:id="@+id/searchView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    </SearchView>
Step - 3:
- Implements the listener on SearchViewDemoActivity activity.
     public class SearchViewDemoActivity extends Activity implements SearchView.OnQueryTextListener, SearchView.OnCloseListener {
- Add the below code to search applications.
    /*** GET THE SERVICE FOR SEARCHING THE APPLICATIONS ***/                                                  
    final SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);             
    if(searchManager != null) {                                                                               

        /*** GET THE APPLICATIONS WITH GLOBAL SEARCH ***/                                                     
        List<SearchableInfo> searchableInfos = searchManager.getSearchablesInGlobalSearch();                  

        SearchableInfo info = searchManager.getSearchableInfo(getComponentName());                            
        for(SearchableInfo info2 : searchableInfos) {                                                         
            if(info2.getSuggestAuthority() != null && info2.getSuggestAuthority().startsWith("applications")) {
                info = info2;                                                                                 
            }                                                                                                 
        }                                                                                                      
        searchView.setSearchableInfo(info);                                                                   
    }                                                                                                         
Step - 4:
- Run application.
Find the attached zip file for complete source code.
I would be glad to receive your suggestions regarding it.
Thanks.