3

I'm developing a face recognition app.I need to display a small dotted circle on my camera interface to place the face of the person so that user can train the app well than taking pictures with different scales. I followed "This link" but I don't know how to do it on camera interface. so can anyone please upload a sample project or give me something to move on with my project? thanks in advance..

Edit-2013-02-14
ok that code is working. but i got another 2 problems.

  1. i can draw the circle. but it's always not in the same position. when the screen size changes, it's position is always changing. I tried with getWidht() and getHeight() methods but i couldn't draw the circle in the center of the screen.do you have any idea about that?

  2. according to your answer i have to create all the views programatically. so now i need to add a capture button to my interface. can you please explain it with your answer?

Community
  • 1
  • 1
Ramesh Jaya
  • 671
  • 1
  • 10
  • 16

1 Answers1

6

EDIT :

Code edited to draw circle at center and add capture image button on surface view.

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Point;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;

public class TestActivity extends Activity { 
    /** Called when the activity is first created. */ 

    Camera mCamera = null; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        requestWindowFeature(Window.FEATURE_NO_TITLE); 
        setContentView(R.layout.camera_layout); 
        mCamera = getCameraInstance();
        Preview mPreview = new Preview(this, mCamera); 
        FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
        preview.addView(mPreview);
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int screenCenterX = (size.x /2);
        int screenCenterY = (size.y/2) ;
        DrawOnTop mDraw = new DrawOnTop(this,screenCenterX,screenCenterY); 
        addContentView(mDraw, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 

        //Adding listener
        Button captureButton = (Button) findViewById(R.id.button_capture);
        captureButton.setOnClickListener(
                new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mCamera.takePicture(null, null, mPicture);

                }
            });
    } 
    /**
     * Helper method to access the camera returns null if
     * it cannot get the camera or does not exist
     * @return
     */
    private Camera getCameraInstance() {
        Camera camera = null;

        try {
            camera = Camera.open();
        } catch (Exception e) {
            // cannot get camera or does not exist
        }
        return camera;
    }
    PictureCallback mPicture = new PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
             File pictureFile = getOutputMediaFile();
                if (pictureFile == null){
                    return;
                }
                try {
                    FileOutputStream fos = new FileOutputStream(pictureFile);
                    fos.write(data);
                    fos.close();
                    Toast.makeText(TestActivity.this, "Photo saved to folder \"Pictures\\MyCameraApp\"", Toast.LENGTH_SHORT).show();
                } catch (FileNotFoundException e) {

                } catch (IOException e) {

                }
        }
    };

    private static File getOutputMediaFile(){
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                  Environment.DIRECTORY_PICTURES), "MyCameraApp");
        if (! mediaStorageDir.exists()){
            if (! mediaStorageDir.mkdirs()){
                Log.d("MyCameraApp", "failed to create directory");
                return null;
            }
        }
        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaFile;
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
            "IMG_"+ timeStamp + ".jpg");

        return mediaFile;
    }
} 

class DrawOnTop extends View { 
    int screenCenterX = 0;
    int screenCenterY = 0;
    final int radius = 50;
        public DrawOnTop(Context context, int screenCenterX, int screenCenterY) { 
                super(context); 
               this.screenCenterX = screenCenterX;
               this.screenCenterY = screenCenterY;
         } 

        @Override 
        protected void onDraw(Canvas canvas) { 
                // TODO Auto-generated method stub 
             Paint p = new Paint();
             p.setColor(Color.RED);
             DashPathEffect dashPath = new DashPathEffect(new float[]{5,5}, (float)1.0);
             p.setPathEffect(dashPath);
             p.setStyle(Style.STROKE);
             canvas.drawCircle(screenCenterX, screenCenterY, radius, p);
             invalidate();
             super.onDraw(canvas); 
        } 
    } 

//---------------------------------------------------------------------- 

class Preview extends SurfaceView implements SurfaceHolder.Callback { 
    SurfaceHolder mHolder; 
    Camera mCamera; 
    Preview(Context context, Camera camera) { 
        super(context); 
        // Install a SurfaceHolder.Callback so we get notified when 
        this.mCamera = camera;
        // underlying surface is created and destroyed. 
        mHolder = getHolder(); 
        mHolder.addCallback(this); 
      //this is a deprecated method, is not required after 3.0
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
    } 

    public void surfaceCreated(SurfaceHolder holder) { 
        // The Surface has been created, acquire the camera and tell 
        // to draw. 
        try {
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    } 

    public void surfaceDestroyed(SurfaceHolder holder) { 
        // Surface will be destroyed when we return, so stop the 
        // Because the CameraDevice object is not a shared resource, 
        // important to release it when the activity is paused. 
        mCamera.stopPreview(); 
        mCamera.release();
        mCamera = null; 
    } 

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { 
        // Now that the size is known, set up the camera parameters 
        // the preview. 
        Camera.Parameters parameters = mCamera.getParameters();
        List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();
        // You need to choose the most appropriate previewSize for your app
        Camera.Size previewSize = previewSizes.get(0);
        parameters.setPreviewSize(previewSize.width, previewSize.height);
        mCamera.setParameters(parameters);
        mCamera.startPreview();
    }


}

camera_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
  <FrameLayout
    android:id="@+id/camera_preview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    />

  <Button
    android:id="@+id/button_capture"
    android:text="Capture"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    />
</LinearLayout>

Don't forget to add permission:

<uses-permission android:name="android.permission.CAMERA" />
 <uses-feature android:name="android.hardware.camera" />

New permission added:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Sourab Sharma
  • 2,940
  • 1
  • 25
  • 38
  • hi friend thank you so much for the quick reply. I'll try this code and give you feedback.. – Ramesh Jaya Feb 13 '13 at 09:22
  • Its upto you explain problem here or post a new question. – Sourab Sharma Feb 14 '13 at 06:35
  • ok that code is working. but i got another 2 problems. 1. i can draw the circle. but it's always not in the same position. when the screen size changes, it's position is always changing. I tried with getWidht() and getHeight() methods but i couldn't draw the circle in the center of the screen.do you have any idea about that? 2. according to your answer i have to create all the views programatically. so now i need to add a capture button to my interface. can you please explain it with your answer? – Ramesh Jaya Feb 14 '13 at 06:45
  • 1
    try this and when everything works perfect edit your question text so that next person land on this page should understand the edited part of the requirement too. Will help someone else. – Sourab Sharma Feb 14 '13 at 08:07
  • yeah that's true friend..and it's done..thanks for everything !! – Ramesh Jaya Feb 14 '13 at 08:19