0

I want to take a photo from my camera, however I don't want to start a new intent to capture a photo but I want the camera to open inside my layout and then click a button to take the photo, like in the bellow picture.

enter image description here

So is it possible to do it because i didn't find any ressources about this, also I prefer that the token photo to be cropped?

Abdennacer Lachiheb
  • 4,388
  • 7
  • 30
  • 61
  • make imageView become clickable will solve your problem ? – John Joe Nov 03 '16 at 10:17
  • For custom camera view: http://stackoverflow.com/questions/8543244/custom-camera-android. For circle surfaceView: http://stackoverflow.com/questions/31443911/creating-circle-in-surfaceview-on-button-click-android Almost everything is already on StackOverflow. Just search for it. – R. Zagórski Nov 03 '16 at 10:20
  • You have to make costume camera So that you can design your Surface view in your desire manner – Nivedh Nov 03 '16 at 10:20
  • @JohnJoe I'm sorry but I don't understand how making the imageView clickable will prevent opening the camera intent ? – Abdennacer Lachiheb Nov 03 '16 at 10:21

2 Answers2

1

Google demo of Camera2 Api. It will help you. Link : https://github.com/googlesamples/android-Camera2Basic

Mavya Soni
  • 932
  • 7
  • 15
0

You need to get the camera runtime permission. You can then access the camera API directly.

This SurfaceView for example will show the preview image in your layout structure (from the official documentation):

/** A basic Camera preview class */
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder mHolder;
    private Camera mCamera;

    public CameraPreview(Context context, Camera camera) {
        super(context);
        mCamera = camera;

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);
        // deprecated setting, but required on Android versions prior to 3.0
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, now tell the camera where to draw the preview.
        try {
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
        } catch (IOException e) {
            Log.d(TAG, "Error setting camera preview: " + e.getMessage());
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // empty. Take care of releasing the Camera preview in your activity.
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // If your preview can change or rotate, take care of those events here.
        // Make sure to stop the preview before resizing or reformatting it.

        if (mHolder.getSurface() == null){
          // preview surface does not exist
          return;
        }

        // stop preview before making changes
        try {
            mCamera.stopPreview();
        } catch (Exception e){
          // ignore: tried to stop a non-existent preview
        }

        // set preview size and make any resize, rotate or
        // reformatting changes here

        // start preview with new settings
        try {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();

        } catch (Exception e){
            Log.d(TAG, "Error starting camera preview: " + e.getMessage());
        }
    }
}

You get the Camera instance with this code:

/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(){
    Camera c = null;
    try {
        c = Camera.open(); // attempt to get a Camera instance
    }
    catch (Exception e){
        // Camera is not available (in use or does not exist)
    }
    return c; // returns null if camera is unavailable
}

And you display it in your Activity like this:

public class CameraActivity extends Activity {

    private Camera mCamera;
    private CameraPreview mPreview;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Create an instance of Camera
        mCamera = getCameraInstance();

        // Create our Preview view and set it as the content of our activity.
        mPreview = new CameraPreview(this, mCamera);
        FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
        preview.addView(mPreview);
    }
}

See the official documentation.

Knossos
  • 15,802
  • 10
  • 54
  • 91