How to Open the "Front Camera" on the Android Platform

Open the Front camera in a surface view

I changed the approach; previously I was implementing SurfaceHolder.Callback but now I created its object.

I moved code from SurfaceCreated to onResume()

public void onResume() {
super.onResume();
for(int i=0;i<Camera.getNumberOfCameras();i++){
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(i,info);
if(info.facing== Camera.CameraInfo.CAMERA_FACING_FRONT){
cameraId = i;
break;
}
}
camera=Camera.open(cameraId);
camera.startPreview();
}

And the object of Callback is like:

SurfaceHolder.Callback surfaceCallback=new SurfaceHolder.Callback() {
public void surfaceCreated(SurfaceHolder holder) {
// no-op -- wait until surfaceChanged()

}
public void surfaceChanged(SurfaceHolder holder,
int format, int width,
int height) {
Camera.Parameters parameters=camera.getParameters();
Camera.Size size=getBestPreviewSize(width, height,
parameters);
Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();

if (size!=null) {
parameters.setPreviewSize(size.width, size.height);
camera.setParameters(parameters);
camera.setDisplayOrientation(90);
cameraConfigured=true;
}
camera.startPreview();
}
public void surfaceDestroyed(SurfaceHolder holder) {
// no-op
}
};

This solved my problem.

Android front camera

For APIs >=9, you can use the Camera class: http://developer.android.com/reference/android/hardware/Camera.html to see if it has more than one camera, and query the CameraInfo

  • getNumberOfCameras

  • getCameraInfo:

http://developer.android.com/reference/android/hardware/Camera.CameraInfo.html

Constants

int CAMERA_FACING_BACK The facing of the camera is opposite to that of the screen.

int CAMERA_FACING_FRONT The facing of the camera is the same as that of the screen.


For APIs >=5, an option is to read public List<Camera.Size> getSupportedPictureSizes (). Front facing cameras will usually have much lower max resolution than back cameras.

http://developer.android.com/reference/android/hardware/Camera.Parameters.html

Launch front camera with intent

I am trying to open the front camera directly with Intent

There is nothing for that in the Android SDK.

Below is my code

There are hundreds, perhaps thousands, of different camera apps available for Android. Some are pre-installed; some are user-installed. None have to honor those undocumented Intent extras.

This code is not working on S6(7.0). However it is working on S5,S4,Nexus 6P(8.0).

There are ~20,000+ Android device models. Only some might have a pre-installed camera app that honors those undocumented Intent extras.

So is this some kind of bug of Samsung or there is something wrong in my code.

Samsung's camera app for that particular device simply does not honor those undocumented extras.



Related Topics



Leave a reply



Submit