Home » Android » android – How to use Front Facing Camera on Samsung Galaxy S

android – How to use Front Facing Camera on Samsung Galaxy S

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’ve tried several answer I’ve found across the web, such as:

Camera.Parameters parameters = mCamera.getParameters();
parameters.set("camera-id", 2);
mCamera.setParameters(parameters);

or

mMediaRecorder.setVideoSource(2);

But it doesn’t work. I’ve also set permissions on the manifest file:

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

Am i missing out on something? I’ve searched StackOverflow and I know this has been asked before but there seem to be no confirmed solution on this, any kind of help would be appreciated.

Note: I’m using Galaxy S on the 2.1 platform

How to&Answers:

Anyway after a few trials and error, I figured it out how to do it:

Camera.Parameters parameters = mCamera.getParameters();
parameters.set("camera-id", 2);
parameters.setPreviewSize(640, 480); // or (800,480) this is supported front camera preview size @ Samsung Galaxy S
mCamera.setParameters(parameters);

Or, if you need to use it with MediaRecorder:

MediaRecorder mMediaRecorder = new MediaRecorder();
Camera mCamera = Camere.open();
Camera.Parameters parameters = mCamera.getParameters();
parameters.set("camera-id", 2);
parameters.setPreviewSize(640, 480); // or (800,480)
mCamera.setParameters(parameters);
mCamera.unlock(); // unlock, to give other process to access it otherwise it can't be used later
mMediaRecorder.setCamera(mCamera);
// continue with mMediaRecorder standard routines here

If you need to have a smaller preview size, you could set/scale down your SurfaceView size instead.

Answer:

There currently isn’t a standard API for front-facing cameras; you will need to rely on whatever (if any) documentation the hardware manufacturer has for using their extensions to access the front-facing camera. This will of course only work on those specific devices.

Note writing raw strings like “camera-id” is often a good sign you have wandered into the woods outside of the official SDK. 🙂

Answer:

I think this is as good a place as any to add some details I’ve worked out.

In case you’re using the front facing camera in portrait mode and the resulting file comes out cut up into squares with green blocks thrown in try inverting the width and height (both preview and recorder) and setting the encoder to H263…

This solved the problem on my Samsung Galaxy S on 2.3.3+…