Showing posts with label Android example. Show all posts
Showing posts with label Android example. Show all posts

Tuesday, 25 October 2011

Android Face Detection Example

In Android Sdk provide rich class called android.media.FaceDetector to identify the faces of people in a Bitmap graphic object.
In this below example we can see using FaceDetector how i find any people faces in Bitmap image .

Here below class i am using FaceDetector i can apply one rectangle shape to face.

Here i am using one image(aishwarya.png) that can be converted into Bitmap image
and Apply FaceDetection view on Image.and set that image to Activity View.

Note: Dont forget to place (aishwarya.png) in drawable folder.you can use your own image insted of (aishwarya.png) no problem .but u should you should use (yourpngfilename.png) insted of (aishwarya.png) in the below code

Android Activity(FaceDetectorActivity.java)

package com.androidsurya.facemodule;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.media.FaceDetector;
import android.media.FaceDetector.Face;
import android.os.Bundle;
import android.view.View;

public class FaceDetectorActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new faceView(this));
}

private class faceView extends View {
// numberOfFace is for how many faces you want to find
private int numberOfFace = 1;
private FaceDetector faceDectect;
private FaceDetector.Face[] faceObj;
float eyesDistance;
int numberOfFacesDetected;
Bitmap bitmapImag;

public faceView(Context context) {
super(context);
// TODO Auto-generated constructor stub

BitmapFactory.Options BitmapFactoryOptionsbfo = new BitmapFactory.Options();
BitmapFactoryOptionsbfo.inPreferredConfig = Bitmap.Config.RGB_565;
bitmapImag = BitmapFactory.decodeResource(getResources(),
R.drawable.aishwarya, BitmapFactoryOptionsbfo);
faceObj = new FaceDetector.Face[numberOfFace];
faceDectect = new FaceDetector(bitmapImag.getWidth(),
bitmapImag.getHeight(), numberOfFace);
numberOfFacesDetected = faceDectect.findFaces(bitmapImag, faceObj);

}

@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub

canvas.drawBitmap(bitmapImag, 0, 0, null);

Paint myPaint = new Paint();
myPaint.setColor(Color.YELLOW);
myPaint.setStyle(Paint.Style.STROKE);
myPaint.setStrokeWidth(2);

for (int i = 0; i < numberOfFacesDetected; i++) {
Face face = faceObj[i];
PointF myMidPoint = new PointF();
face.getMidPoint(myMidPoint);
eyesDistance = face.eyesDistance();
canvas.drawRect((int) (myMidPoint.x - eyesDistance),
(int) (myMidPoint.y - eyesDistance),
(int) (myMidPoint.x + eyesDistance),
(int) (myMidPoint.y + eyesDistance), myPaint);
}
}
}
}

Register Android Activity in Android Manifest File

<activity
            android:name="com.androidsurya.facemodule.FaceDetectorActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

Output ScreenShot
















Friday, 12 August 2011

Load Image From URL Android Example

Here you can show image from URL (web)
.
Here we are going to see about how to load the image from web using URL.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidsurya.tutorial"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.androidsurya.tutorial.LoadImageFromUrl"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Write xml file(load_image_from_url.xml)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LoadImageFromUrl" >
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_launcher" />
</RelativeLayout>

Create Android Activity(LoadImageFromUrl.Class)

package com.androidsurya.tutorial;

import java.io.InputStream;
import java.net.URL;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.widget.ImageView;

public class LoadImageFromUrl extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.load_image_from_url);
        ImageView imgView = (ImageView) findViewById(R.id.imageView1);
        Drawable drawable = LoadImageFromURL("http://www.imagesup.net/dt-713727549903.png");
        imgView.setImageDrawable(drawable);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.load_image_from_url, menu);
        return true;
    }

    private Drawable LoadImageFromURL(String url) {
        try {
            InputStream is = (InputStream) new URL(url).getContent();
            Drawable d = Drawable.createFromStream(is, "src name");
            return d;
        } catch (Exception e) {
            System.out.println("Excecption is=" + e);
            return null;
        }
    }

}

See the final output like this:





Android SQLite Database Viewer or Debuging with Stetho

Every Android Developer uses SQLite Database to store data into the Android Application data. But to view the data in SQLite have a lot of...