I understood what you actually want. I will write you the complete code and just go along.
In this code i am taking an imageview in layout, and two classes , one activity class and other is imageview class.
I will create two buttons, where one button is used to select image from gallery and display it ( for face detection) and second button for detecting faces on the selected image.
firstly mainlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.simpleapps.facedetection.MyView
android:id="@+id/faceview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="top">
<ImageView
android:id="@+id/gallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:background="@drawable/gallery" />
<ImageView
android:id="@+id/detectf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:background="@drawable/detect" />
</LinearLayout>
</FrameLayout>
now the activity class
MainActivity.java
public class MainActivity extends Activity {
public MyView faceview;
public static Bitmap defaultBitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_main);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
screenHeight = displaymetrics.heightPixels;
screenWidth = displaymetrics.widthPixels;
faceview = (MyView)findViewById(R.id.faceview);
myGallery = (LinearLayout)findViewById(R.id.mygallery);
gallery=(ImageView)findViewById(R.id.gallery);
detectf=(ImageView)findViewById(R.id.detectf);
BitmapFactory.Options bitmapFatoryOptions=new BitmapFactory.Options();
bitmapFatoryOptions.inPreferredConfig=Bitmap.Config.RGB_565;
defaultBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.face,bitmapFatoryOptions);
faceview.setImage(defaultBitmap);
gallery.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, 0 );
}
});
detectf.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
faceview.facedetect();
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if(requestCode==0){
imageURI = data.getData();
try {
BitmapFactory.Options bitmapFatoryOptions=new BitmapFactory.Options();
bitmapFatoryOptions.inPreferredConfig=Bitmap.Config.RGB_565;
Bitmap b =
BitmapFactory.decodeStream(getContentResolver().openInputStream(imageURI), null,
bitmapFatoryOptions);
faceview.myBitmap=b;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
faceview.invalidate();
}
faceview.invalidate();
} else {
System.exit(0);
Log.e("result", "BAD");
}
}
}
now the view class.
MyView.java
public class MyView extends View {
private FaceDetector.Face[] detectedFaces;
private int NUMBER_OF_FACES=10;
private FaceDetector faceDetector;
private int NUMBER_OF_FACE_DETECTED;
private float eyeDistance;
public Paint myPaint;
public Bitmap resultBmp;
public Bitmap myBitmap,HairBitmap;
public PointF midPoint1;
public MyView(Context context, AttributeSet attrs) {
super(context,attrs);
// TODO Auto-generated constructor stub
BitmapFactory.Options bitmapFatoryOptions=new BitmapFactory.Options();
bitmapFatoryOptions.inPreferredConfig=Bitmap.Config.RGB_565;
}
public void setImage(Bitmap bitmap) {
myBitmap = bitmap;
invalidate();
}
public void facedetect(){
myPaint = new Paint();
myPaint.setColor(Color.GREEN);
myPaint.setStyle(Paint.Style.STROKE);
myPaint.setStrokeWidth(3);
detectedFaces=new FaceDetector.Face[NUMBER_OF_FACES];
faceDetector=new FaceDetector(resultBmp.getWidth(),resultBmp.getHeight(),NUMBER_OF_FACES);
NUMBER_OF_FACE_DETECTED=faceDetector.findFaces(resultBmp, detectedFaces);
System.out.println("faces detected are"+NUMBER_OF_FACE_DETECTED);
Canvas facec=new Canvas();
for(int count=0;count<NUMBER_OF_FACE_DETECTED;count++)
{
if(count==0){
face1=detectedFaces[count];
midPoint1=new PointF();
face1.getMidPoint(midPoint1);
eyeDistance=face1.eyesDistance();
}
}
invalidate();
if(NUMBER_OF_FACE_DETECTED==0){
Toast.makeText(getContext(), "no faces detected", Toast.LENGTH_LONG).show();
}else if(NUMBER_OF_FACE_DETECTED!=0){
Toast.makeText(getContext(), "faces detected "+NUMBER_OF_FACE_DETECTED, Toast.LENGTH_LONG).show();
}
}
protected void onDraw(Canvas canvas)
{
if(myBitmap!=null)
{
w = myBitmap.getWidth();
h = myBitmap.getHeight();
resultBmp = null;
int widthofBitMap = MainActivity.screenWidth ;
int heightofBitMap = widthofBitMap*h/w;
resultBmp = Bitmap.createScaledBitmap(myBitmap, widthofBitMap, heightofBitMap, true);
canvas.drawBitmap(resultBmp, (MainActivity.screenWidth-widthofBitMap)/2,(MainActivity.screenHeight-heightofBitMap)/2, null);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
int action = event.getAction();
switch(action){
case MotionEvent.ACTION_MOVE:
x = event.getX();
y = event.getY();
break;
case MotionEvent.ACTION_DOWN:
x = event.getX();
y = event.getY();
break;
case MotionEvent.ACTION_UP:
default:
}
invalidate();
return true;
}
}
I took some time to write this code. I hope it helps. If you get some error just ask.