Skip to content

Commit

Permalink
Support recording from front camera
Browse files Browse the repository at this point in the history
  • Loading branch information
chartsai committed Mar 27, 2017
1 parent a3b972f commit f624734
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 10 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

The recorded video will store in /sdcard/Pictures/MyCameraApp/*.mp4

Now support Start/Stop button.
- Support Start/Stop button.
- Support choosing front or back camera for recording.

1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package="example.chatea.servicecamera" >

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.front" android:required="false" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
Expand Down
17 changes: 14 additions & 3 deletions app/src/main/java/example/chatea/servicecamera/CameraService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.media.CameraProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.IBinder;
Expand Down Expand Up @@ -39,6 +40,8 @@ public class CameraService extends Service {
private static final int COMMAND_START_RECORDING = 0;
private static final int COMMAND_STOP_RECORDING = 1;

private static final String SELECTED_CAMERA_FOR_RECORDING = "cameraForRecording";

private Camera mCamera;
private MediaRecorder mMediaRecorder;

Expand All @@ -48,9 +51,11 @@ public class CameraService extends Service {
public CameraService() {
}

public static void startToStartRecording(Context context, ResultReceiver resultReceiver) {
public static void startToStartRecording(Context context, int cameraId,
ResultReceiver resultReceiver) {
Intent intent = new Intent(context, CameraService.class);
intent.putExtra(START_SERVICE_COMMAND, COMMAND_START_RECORDING);
intent.putExtra(SELECTED_CAMERA_FOR_RECORDING, cameraId);
intent.putExtra(RESULT_RECEIVER, resultReceiver);
context.startService(intent);
}
Expand Down Expand Up @@ -111,7 +116,9 @@ private void handleStartRecordingCommand(Intent intent) {
mRecording = true;

if (Util.checkCameraHardware(this)) {
mCamera = Util.getCameraInstance();
final int cameraId = intent.getIntExtra(SELECTED_CAMERA_FOR_RECORDING,
Camera.CameraInfo.CAMERA_FACING_BACK);
mCamera = Util.getCameraInstance(cameraId);
if (mCamera != null) {
SurfaceView sv = new SurfaceView(this);

Expand Down Expand Up @@ -163,7 +170,11 @@ public void surfaceCreated(SurfaceHolder holder) {
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
if (cameraId == Camera.CameraInfo.CAMERA_FACING_BACK) {
mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
} else {
mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_480P));
}

mRecordingPath = Util.getOutputMediaFile(Util.MEDIA_TYPE_VIDEO).getPath();
mMediaRecorder.setOutputFile(mRecordingPath);
Expand Down
25 changes: 24 additions & 1 deletion app/src/main/java/example/chatea/servicecamera/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package example.chatea.servicecamera;

import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Handler;
import android.os.ResultReceiver;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;

/**
Expand All @@ -16,13 +18,26 @@ public class MainActivity extends Activity {
private boolean mRecording;
private boolean mHandlingEvent;

private RadioButton mFrontRadioButton;
private RadioButton mBackRadioButton;
private Button bt_recordingButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mFrontRadioButton = (RadioButton) findViewById(R.id.front_camera_radio_button);
if (!Util.isCameraExist(Camera.CameraInfo.CAMERA_FACING_FRONT)) {
mFrontRadioButton.setVisibility(View.GONE);
mFrontRadioButton.setChecked(false);
}
mBackRadioButton = (RadioButton) findViewById(R.id.back_camera_radio_button);
if (!Util.isCameraExist(Camera.CameraInfo.CAMERA_FACING_BACK)) {
mFrontRadioButton.setVisibility(View.GONE);
mFrontRadioButton.setChecked(false);
}

bt_recordingButton = (Button) findViewById(R.id.recording_button);
bt_recordingButton.setOnClickListener(new View.OnClickListener() {
@Override
Expand All @@ -47,7 +62,15 @@ protected void onReceiveResult(int resultCode, Bundle resultData) {
mHandlingEvent = false;
}
};
CameraService.startToStartRecording(this, receiver);
if (mFrontRadioButton.isChecked()) {
CameraService.startToStartRecording(this,
Camera.CameraInfo.CAMERA_FACING_FRONT, receiver);
} else if (mBackRadioButton.isChecked()) {
CameraService.startToStartRecording(this,
Camera.CameraInfo.CAMERA_FACING_FRONT, receiver);
} else {
throw new IllegalStateException("Must choose a camera for recording");
}
}
}

Expand Down
16 changes: 14 additions & 2 deletions app/src/main/java/example/chatea/servicecamera/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,22 @@ public static boolean checkCameraHardware(Context context) {
}
}

public static Camera getCameraInstance() {
public static boolean isCameraExist(int cameraId) {
int numberOfCameras = Camera.getNumberOfCameras();
for (int i = 0; i < numberOfCameras; i++) {
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(i, info);
if (info.facing == cameraId) {
return true;
}
}
return false;
}

public static Camera getCameraInstance(int cameraId) {
Camera c = null;
try {
c = Camera.open();
c = Camera.open(cameraId);
} catch (Exception e) {
Log.d("TAG", "Open camera failed: " + e);
}
Expand Down
21 changes: 21 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,27 @@
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:checkedButton="@+id/back_camera_radio_button">

<RadioButton
android:id="@+id/front_camera_radio_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/front_camera"
android:layout_weight="1" />

<RadioButton
android:id="@+id/back_camera_radio_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/back_camera"
android:layout_weight="1" />
</RadioGroup>

<Button
android:id="@+id/recording_button"
android:layout_width="wrap_content"
Expand Down
5 changes: 2 additions & 3 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<resources>
<string name="app_name">ServiceCamera</string>

<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>

<string name="start_recording">Start Recording</string>
<string name="stop_recording">Stop Recording</string>
<string name="front_camera">Front Camera</string>
<string name="back_camera">Back Camera</string>
</resources>

0 comments on commit f624734

Please sign in to comment.