Skip to content

Commit

Permalink
Simplify the API for start/stop camera preview
Browse files Browse the repository at this point in the history
Change-Id: I898a855d2d891111fdf0558d6eb94fb4f9e6e65b
  • Loading branch information
yaraki committed May 11, 2016
1 parent 6c04858 commit 3e23db8
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 41 deletions.
2 changes: 2 additions & 0 deletions demo/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
xmlns:android="http://schemas.android.com/apk/res/android">

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

<application
android:allowBackup="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,12 @@ protected void onCreate(Bundle savedInstanceState) {
@Override
protected void onResume() {
super.onResume();
mCameraView.onResume();
mCameraView.startPreview();
mCameraView.start();
}

@Override
protected void onPause() {
mCameraView.stopPreview();
mCameraView.onPause();
mCameraView.stop();
super.onPause();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,9 @@ public CameraViewImpl(Callback callback) {

abstract TextureView.SurfaceTextureListener getSurfaceTextureListener();

abstract void onResume();
abstract void start();

abstract void onPause();

abstract void startPreview();

abstract void stopPreview();
abstract void stop();

abstract SizeMap getSupportedPreviewSizes();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,24 +107,21 @@ TextureView.SurfaceTextureListener getSurfaceTextureListener() {
}

@Override
void onResume() {
void start() {
chooseCamera();
openCamera();
}

@Override
void onPause() {
releaseCamera();
}

@Override
void startPreview() {
if (mPreviewInfo.surface != null) {
setUpPreview();
}
mCamera.startPreview();
}

@Override
void stop() {
mCamera.stopPreview();
releaseCamera();
}

private void setUpPreview() {
try {
mCamera.setPreviewTexture(mPreviewInfo.surface);
Expand All @@ -133,11 +130,6 @@ private void setUpPreview() {
}
}

@Override
void stopPreview() {
mCamera.stopPreview();
}

@Override
SizeMap getSupportedPreviewSizes() {
return mPreviewSizes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@

public class CameraView extends FrameLayout {

private static final String TAG = "CameraView";

private final CameraViewImpl mImpl;

private final CallbackBridge mCallbacks;
Expand Down Expand Up @@ -126,25 +124,19 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
}

/**
* This needs to be called from {@link Activity#onResume()}.
* Open a camera device and start showing camera preview. This is typically called from
* {@link Activity#onResume()}.
*/
public void onResume() {
mImpl.onResume();
public void start() {
mImpl.start();
}

/**
* This needs to be called from {@link Activity#onPause()}.
* Stop camera preview and close the device. This is typically called from
* {@link Activity#onPause()}.
*/
public void onPause() {
mImpl.onPause();
}

public void startPreview() {
mImpl.startPreview();
}

public void stopPreview() {
mImpl.stopPreview();
public void stop() {
mImpl.stop();
}

public SizeMap getSupportedPreviewSizes() {
Expand Down

0 comments on commit 3e23db8

Please sign in to comment.