Added CameraApi

pull/17/head
Florent CHAMPIGNY 8 years ago
parent c3b0214cd0
commit 1c219d40f0
  1. 4
      README.md
  2. 78
      app/src/main/java/com/github/florent37/camerafragment/sample/MainActivity.java
  3. 8
      app/src/main/res/layout/activity_fragment.xml
  4. 32
      camerafragment/src/main/java/com/github/florent37/camerafragment/CameraFragmentApi.java
  5. 5
      camerafragment/src/main/java/com/github/florent37/camerafragment/internal/controller/CameraController.java
  6. 20
      camerafragment/src/main/java/com/github/florent37/camerafragment/internal/controller/impl/Camera1Controller.java
  7. 20
      camerafragment/src/main/java/com/github/florent37/camerafragment/internal/controller/impl/Camera2Controller.java
  8. 6
      camerafragment/src/main/java/com/github/florent37/camerafragment/internal/controller/view/CameraView.java
  9. 5
      camerafragment/src/main/java/com/github/florent37/camerafragment/internal/manager/CameraManager.java
  10. 114
      camerafragment/src/main/java/com/github/florent37/camerafragment/internal/manager/impl/Camera1Manager.java
  11. 89
      camerafragment/src/main/java/com/github/florent37/camerafragment/internal/manager/impl/Camera2Manager.java
  12. 4
      camerafragment/src/main/java/com/github/florent37/camerafragment/internal/manager/listener/CameraPhotoListener.java
  13. 3
      camerafragment/src/main/java/com/github/florent37/camerafragment/internal/manager/listener/CameraVideoListener.java
  14. 124
      camerafragment/src/main/java/com/github/florent37/camerafragment/internal/ui/BaseAnncaFragment.java
  15. 19
      camerafragment/src/main/java/com/github/florent37/camerafragment/widgets/CameraSwitchView.java
  16. 19
      camerafragment/src/main/java/com/github/florent37/camerafragment/widgets/FlashSwitchView.java

@ -15,7 +15,7 @@ getSupportFragmentManager().beginTransaction()
You can directly take a photo / video with You can directly take a photo / video with
```java ```java
cameraFragment.takePhotoOrCaptureVideo(); cameraFragment.takePhotoOrCaptureVideo(callback);
``` ```
[![gif](https://raw.githubusercontent.com/florent37/CameraFragment/master/media/take_photo.gif)](https://github.com/florent37/CameraFragment) [![gif](https://raw.githubusercontent.com/florent37/CameraFragment/master/media/take_photo.gif)](https://github.com/florent37/CameraFragment)
@ -39,7 +39,7 @@ cameraFragment.switchCameraType();
Camera action ( `PHOTO` / `VIDEO` ) can be modified with Camera action ( `PHOTO` / `VIDEO` ) can be modified with
```java ```java
cameraFragment.switchAction(); cameraFragment.switchActionPhotoVideo();
``` ```
[![gif](https://raw.githubusercontent.com/florent37/CameraFragment/master/media/switch_action.gif)](https://github.com/florent37/CameraFragment) [![gif](https://raw.githubusercontent.com/florent37/CameraFragment/master/media/switch_action.gif)](https://github.com/florent37/CameraFragment)

@ -21,6 +21,7 @@ import butterknife.Bind;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import butterknife.OnClick; import butterknife.OnClick;
import com.github.florent37.camerafragment.CameraFragment; import com.github.florent37.camerafragment.CameraFragment;
import com.github.florent37.camerafragment.CameraFragmentApi;
import com.github.florent37.camerafragment.configuration.Configuration; import com.github.florent37.camerafragment.configuration.Configuration;
import com.github.florent37.camerafragment.PreviewActivity; import com.github.florent37.camerafragment.PreviewActivity;
import com.github.florent37.camerafragment.listeners.CameraFragmentStateListener; import com.github.florent37.camerafragment.listeners.CameraFragmentStateListener;
@ -57,74 +58,56 @@ public class MainActivity extends AppCompatActivity {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment); setContentView(R.layout.activity_fragment);
ButterKnife.bind(this); ButterKnife.bind(this);
recordButton.setVisibility(View.GONE);
settingsView.setVisibility(View.GONE);
flashSwitchView.setVisibility(View.GONE);
cameraSwitchView.setVisibility(View.GONE);
mediaActionSwitchView.setVisibility(View.GONE);
setupViewListeners();
}
@OnClick(R.id.take_photo)
public void takePhotoClicked(){
final CameraFragment cameraFragment = getCameraFragment();
if (cameraFragment != null) {
cameraFragment.takePhotoOrCaptureVideo();
}
} }
private void setupViewListeners() { @OnClick(R.id.flash_switch_view)
flashSwitchView.setFlashSwitchListener(new FlashSwitchView.FlashModeSwitchListener() { public void onFlashSwitcClicked(){
@Override final CameraFragmentApi cameraFragment = getCameraFragment();
public void toggleFlashMode() {
final CameraFragment cameraFragment = getCameraFragment();
if (cameraFragment != null) { if (cameraFragment != null) {
cameraFragment.toggleFlashMode(); cameraFragment.toggleFlashMode();
} }
} }
});
cameraSwitchView.setOnCameraTypeChangeListener(new CameraSwitchView.OnCameraTypeChangeListener() { @OnClick(R.id.front_back_camera_switcher)
@Override public void onSwitchCameraClicked(){
public void switchCameraType() { final CameraFragmentApi cameraFragment = getCameraFragment();
final CameraFragment cameraFragment = getCameraFragment();
if (cameraFragment != null) { if (cameraFragment != null) {
cameraFragment.switchCameraType(); cameraFragment.switchCameraType();
} }
} }
});
recordButton.setRecordButtonListener(new RecordButton.RecordButtonListener() { @OnClick(R.id.record_button)
@Override public void onRecordButtonClicked(){
public void onRecordButtonClicked() { final CameraFragmentApi cameraFragment = getCameraFragment();
final CameraFragment cameraFragment = getCameraFragment();
if (cameraFragment != null) { if (cameraFragment != null) {
cameraFragment.takePhotoOrCaptureVideo(); cameraFragment.takePhotoOrCaptureVideo(new CameraFragmentResultListener() {
} @Override
public void onVideoRecorded(String filePath) {
} }
});
mediaActionSwitchView.setOnMediaActionStateChangeListener(new MediaActionSwitchView.OnMediaActionStateChangeListener() {
@Override @Override
public void switchAction() { public void onPhotoTaken(byte[] bytes, String filePath) {
final CameraFragment cameraFragment = getCameraFragment();
if (cameraFragment != null) {
cameraFragment.switchAction();
}
} }
}); });
}
}
settingsView.setOnClickListener(new View.OnClickListener() { @OnClick(R.id.settings_view)
@Override public void onSettingsClicked(){
public void onClick(View view) { final CameraFragmentApi cameraFragment = getCameraFragment();
final CameraFragment cameraFragment = getCameraFragment();
if (cameraFragment != null) { if (cameraFragment != null) {
cameraFragment.openSettingDialog(); cameraFragment.openSettingDialog();
} }
} }
});
@OnClick(R.id.settings_view)
public void onMediaActionSwitchClicked(){
final CameraFragmentApi cameraFragment = getCameraFragment();
if (cameraFragment != null) {
cameraFragment.switchActionPhotoVideo();
}
} }
@OnClick(R.id.addCameraButton) @OnClick(R.id.addCameraButton)
@ -169,7 +152,6 @@ public class MainActivity extends AppCompatActivity {
.commit(); .commit();
if (cameraFragment != null) { if (cameraFragment != null) {
cameraFragment.setResultListener(new CameraFragmentResultListener() { cameraFragment.setResultListener(new CameraFragmentResultListener() {
@Override @Override
public void onVideoRecorded(String filePath) { public void onVideoRecorded(String filePath) {
@ -327,7 +309,7 @@ public class MainActivity extends AppCompatActivity {
mediaActionSwitchView.displayActionWillSwitchVideo(); mediaActionSwitchView.displayActionWillSwitchVideo();
} }
private CameraFragment getCameraFragment() { private CameraFragmentApi getCameraFragment() {
return (CameraFragment) getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG); return (CameraFragmentApi) getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG);
} }
} }

@ -73,18 +73,10 @@
android:id="@+id/record_button" android:id="@+id/record_button"
android:layout_width="75dp" android:layout_width="75dp"
android:layout_height="75dp" android:layout_height="75dp"
android:visibility="gone"
android:layout_centerInParent="true" android:layout_centerInParent="true"
android:layout_marginLeft="50dp" android:layout_marginLeft="50dp"
android:layout_marginRight="50dp" /> android:layout_marginRight="50dp" />
<Button
android:id="@+id/take_photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="take photo"/>
<com.github.florent37.camerafragment.widgets.MediaActionSwitchView <com.github.florent37.camerafragment.widgets.MediaActionSwitchView
android:id="@+id/photo_video_camera_switcher" android:id="@+id/photo_video_camera_switcher"
android:layout_width="40dp" android:layout_width="40dp"

@ -0,0 +1,32 @@
package com.github.florent37.camerafragment;
import com.github.florent37.camerafragment.listeners.CameraFragmentControlsListener;
import com.github.florent37.camerafragment.listeners.CameraFragmentResultListener;
import com.github.florent37.camerafragment.listeners.CameraFragmentStateListener;
import com.github.florent37.camerafragment.listeners.CameraFragmentVideoRecordTextListener;
/**
* Created by florentchampigny on 16/01/2017.
*/
public interface CameraFragmentApi {
void takePhotoOrCaptureVideo(CameraFragmentResultListener resultListener);
void openSettingDialog();
void switchCameraType();
void switchActionPhotoVideo();
void toggleFlashMode();
void setStateListener(CameraFragmentStateListener cameraFragmentStateListener);
void setTextListener(CameraFragmentVideoRecordTextListener cameraFragmentVideoRecordTextListener);
void setControlsListener(CameraFragmentControlsListener cameraFragmentControlsListener);
void setResultListener(CameraFragmentResultListener cameraFragmentResultListener);
}

@ -6,6 +6,7 @@ import java.io.File;
import com.github.florent37.camerafragment.configuration.Configuration; import com.github.florent37.camerafragment.configuration.Configuration;
import com.github.florent37.camerafragment.internal.manager.CameraManager; import com.github.florent37.camerafragment.internal.manager.CameraManager;
import com.github.florent37.camerafragment.listeners.CameraFragmentResultListener;
/** /**
* Created by memfis on 7/6/16. * Created by memfis on 7/6/16.
@ -20,11 +21,11 @@ public interface CameraController<CameraId> {
void onDestroy(); void onDestroy();
void takePhoto(); void takePhoto(CameraFragmentResultListener resultListener);
void startVideoRecord(); void startVideoRecord();
void stopVideoRecord(); void stopVideoRecord(CameraFragmentResultListener callback);
boolean isVideoRecording(); boolean isVideoRecording();

@ -2,6 +2,7 @@ package com.github.florent37.camerafragment.internal.controller.impl;
import android.content.Context; import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log; import android.util.Log;
import android.view.SurfaceHolder; import android.view.SurfaceHolder;
@ -20,6 +21,7 @@ import com.github.florent37.camerafragment.internal.manager.listener.CameraVideo
import com.github.florent37.camerafragment.internal.ui.view.AutoFitSurfaceView; import com.github.florent37.camerafragment.internal.ui.view.AutoFitSurfaceView;
import com.github.florent37.camerafragment.internal.utils.CameraHelper; import com.github.florent37.camerafragment.internal.utils.CameraHelper;
import com.github.florent37.camerafragment.internal.utils.Size; import com.github.florent37.camerafragment.internal.utils.Size;
import com.github.florent37.camerafragment.listeners.CameraFragmentResultListener;
/** /**
* Created by memfis on 7/7/16. * Created by memfis on 7/7/16.
@ -48,7 +50,7 @@ public class Camera1Controller implements CameraController<Integer>,
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
cameraManager = Camera1Manager.getInstance(); cameraManager = new Camera1Manager();
cameraManager.initializeCameraManager(configurationProvider, context); cameraManager.initializeCameraManager(configurationProvider, context);
setCurrentCameraId(cameraManager.getFaceBackCameraId()); setCurrentCameraId(cameraManager.getFaceBackCameraId());
} }
@ -74,9 +76,9 @@ public class Camera1Controller implements CameraController<Integer>,
} }
@Override @Override
public void takePhoto() { public void takePhoto(CameraFragmentResultListener callback) {
outputFile = CameraHelper.getOutputMediaFile(context, Configuration.MEDIA_ACTION_PHOTO); outputFile = CameraHelper.getOutputMediaFile(context, Configuration.MEDIA_ACTION_PHOTO);
cameraManager.takePhoto(outputFile, this); cameraManager.takePhoto(outputFile, this, callback);
} }
@Override @Override
@ -86,8 +88,8 @@ public class Camera1Controller implements CameraController<Integer>,
} }
@Override @Override
public void stopVideoRecord() { public void stopVideoRecord(CameraFragmentResultListener callback) {
cameraManager.stopVideoRecord(); cameraManager.stopVideoRecord(callback);
} }
@Override @Override
@ -154,8 +156,8 @@ public class Camera1Controller implements CameraController<Integer>,
} }
@Override @Override
public void onPhotoTaken(byte[] bytes, File photoFile) { public void onPhotoTaken(byte[] bytes, File photoFile, CameraFragmentResultListener callback) {
cameraView.onPhotoTaken(bytes); cameraView.onPhotoTaken(bytes, callback);
} }
@Override @Override
@ -168,8 +170,8 @@ public class Camera1Controller implements CameraController<Integer>,
} }
@Override @Override
public void onVideoRecordStopped(File videoFile) { public void onVideoRecordStopped(File videoFile, @Nullable CameraFragmentResultListener callback) {
cameraView.onVideoRecordStop(); cameraView.onVideoRecordStop(callback);
} }
@Override @Override

@ -4,6 +4,7 @@ import android.annotation.TargetApi;
import android.content.Context; import android.content.Context;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log; import android.util.Log;
import android.view.TextureView; import android.view.TextureView;
@ -22,6 +23,7 @@ import com.github.florent37.camerafragment.internal.manager.listener.CameraVideo
import com.github.florent37.camerafragment.internal.ui.view.AutoFitTextureView; import com.github.florent37.camerafragment.internal.ui.view.AutoFitTextureView;
import com.github.florent37.camerafragment.internal.utils.CameraHelper; import com.github.florent37.camerafragment.internal.utils.CameraHelper;
import com.github.florent37.camerafragment.internal.utils.Size; import com.github.florent37.camerafragment.internal.utils.Size;
import com.github.florent37.camerafragment.listeners.CameraFragmentResultListener;
/** /**
* Created by memfis on 7/6/16. * Created by memfis on 7/6/16.
@ -49,7 +51,7 @@ public class Camera2Controller implements CameraController<String>,
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
camera2Manager = Camera2Manager.getInstance(); camera2Manager = new Camera2Manager();
camera2Manager.initializeCameraManager(configurationProvider, context); camera2Manager.initializeCameraManager(configurationProvider, context);
setCurrentCameraId(camera2Manager.getFaceBackCameraId()); setCurrentCameraId(camera2Manager.getFaceBackCameraId());
} }
@ -71,9 +73,9 @@ public class Camera2Controller implements CameraController<String>,
} }
@Override @Override
public void takePhoto() { public void takePhoto(CameraFragmentResultListener callback) {
outputFile = CameraHelper.getOutputMediaFile(context, Configuration.MEDIA_ACTION_PHOTO); outputFile = CameraHelper.getOutputMediaFile(context, Configuration.MEDIA_ACTION_PHOTO);
camera2Manager.takePhoto(outputFile, this); camera2Manager.takePhoto(outputFile, this, callback);
} }
@Override @Override
@ -83,8 +85,8 @@ public class Camera2Controller implements CameraController<String>,
} }
@Override @Override
public void stopVideoRecord() { public void stopVideoRecord(CameraFragmentResultListener callback) {
camera2Manager.stopVideoRecord(); camera2Manager.stopVideoRecord(callback);
} }
@Override @Override
@ -155,8 +157,8 @@ public class Camera2Controller implements CameraController<String>,
} }
@Override @Override
public void onPhotoTaken(byte[] bytes, File photoFile) { public void onPhotoTaken(byte[] bytes, File photoFile, CameraFragmentResultListener callback) {
cameraView.onPhotoTaken(bytes); cameraView.onPhotoTaken(bytes, callback);
} }
@Override @Override
@ -169,8 +171,8 @@ public class Camera2Controller implements CameraController<String>,
} }
@Override @Override
public void onVideoRecordStopped(File videoFile) { public void onVideoRecordStopped(File videoFile, @Nullable CameraFragmentResultListener callback) {
cameraView.onVideoRecordStop(); cameraView.onVideoRecordStop(callback);
} }
@Override @Override

@ -1,9 +1,11 @@
package com.github.florent37.camerafragment.internal.controller.view; package com.github.florent37.camerafragment.internal.controller.view;
import android.support.annotation.Nullable;
import android.view.View; import android.view.View;
import com.github.florent37.camerafragment.configuration.Configuration; import com.github.florent37.camerafragment.configuration.Configuration;
import com.github.florent37.camerafragment.internal.utils.Size; import com.github.florent37.camerafragment.internal.utils.Size;
import com.github.florent37.camerafragment.listeners.CameraFragmentResultListener;
/** /**
* Created by memfis on 7/6/16. * Created by memfis on 7/6/16.
@ -16,11 +18,11 @@ public interface CameraView {
void updateCameraSwitcher(int numberOfCameras); void updateCameraSwitcher(int numberOfCameras);
void onPhotoTaken(byte[] bytes); void onPhotoTaken(byte[] bytes, @Nullable CameraFragmentResultListener callback);
void onVideoRecordStart(int width, int height); void onVideoRecordStart(int width, int height);
void onVideoRecordStop(); void onVideoRecordStop(@Nullable CameraFragmentResultListener callback);
void releaseCameraPreview(); void releaseCameraPreview();

@ -11,6 +11,7 @@ import com.github.florent37.camerafragment.internal.manager.listener.CameraOpenL
import com.github.florent37.camerafragment.internal.manager.listener.CameraPhotoListener; import com.github.florent37.camerafragment.internal.manager.listener.CameraPhotoListener;
import com.github.florent37.camerafragment.internal.manager.listener.CameraVideoListener; import com.github.florent37.camerafragment.internal.manager.listener.CameraVideoListener;
import com.github.florent37.camerafragment.internal.utils.Size; import com.github.florent37.camerafragment.internal.utils.Size;
import com.github.florent37.camerafragment.listeners.CameraFragmentResultListener;
/** /**
* Created by memfis on 8/14/16. * Created by memfis on 8/14/16.
@ -25,13 +26,13 @@ public interface CameraManager<CameraId, SurfaceListener> {
void setFlashMode(@Configuration.FlashMode int flashMode); void setFlashMode(@Configuration.FlashMode int flashMode);
void takePhoto(File photoFile, CameraPhotoListener cameraPhotoListener); void takePhoto(File photoFile, CameraPhotoListener cameraPhotoListener, CameraFragmentResultListener callback);
void startVideoRecord(File videoFile, CameraVideoListener cameraVideoListener); void startVideoRecord(File videoFile, CameraVideoListener cameraVideoListener);
Size getPhotoSizeForQuality(@Configuration.MediaQuality int mediaQuality); Size getPhotoSizeForQuality(@Configuration.MediaQuality int mediaQuality);
void stopVideoRecord(); void stopVideoRecord(CameraFragmentResultListener callback);
void releaseCameraManager(); void releaseCameraManager();

@ -7,6 +7,7 @@ import android.media.CamcorderProfile;
import android.media.ExifInterface; import android.media.ExifInterface;
import android.media.MediaRecorder; import android.media.MediaRecorder;
import android.os.Build; import android.os.Build;
import android.support.annotation.Nullable;
import android.util.Log; import android.util.Log;
import android.view.Surface; import android.view.Surface;
import android.view.SurfaceHolder; import android.view.SurfaceHolder;
@ -29,21 +30,19 @@ import com.github.florent37.camerafragment.internal.ui.model.PhotoQualityOption;
import com.github.florent37.camerafragment.internal.ui.model.VideoQualityOption; import com.github.florent37.camerafragment.internal.ui.model.VideoQualityOption;
import com.github.florent37.camerafragment.internal.utils.CameraHelper; import com.github.florent37.camerafragment.internal.utils.CameraHelper;
import com.github.florent37.camerafragment.internal.utils.Size; import com.github.florent37.camerafragment.internal.utils.Size;
import com.github.florent37.camerafragment.listeners.CameraFragmentResultListener;
/** /**
* Created by memfis on 8/14/16. * Created by memfis on 8/14/16.
*/ */
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public class Camera1Manager extends BaseCameraManager<Integer, SurfaceHolder.Callback> public class Camera1Manager extends BaseCameraManager<Integer, SurfaceHolder.Callback> {
implements SurfaceHolder.Callback, Camera.PictureCallback {
private static final String TAG = "Camera1Manager"; private static final String TAG = "Camera1Manager";
private Camera camera; private Camera camera;
private Surface surface; private Surface surface;
private static Camera1Manager currentInstance;
private int orientation; private int orientation;
private int displayRotation = 0; private int displayRotation = 0;
@ -51,15 +50,6 @@ public class Camera1Manager extends BaseCameraManager<Integer, SurfaceHolder.Cal
private CameraVideoListener videoListener; private CameraVideoListener videoListener;
private CameraPhotoListener photoListener; private CameraPhotoListener photoListener;
private Camera1Manager() {
}
public static Camera1Manager getInstance() {
if (currentInstance == null) currentInstance = new Camera1Manager();
return currentInstance;
}
@Override @Override
public void openCamera(final Integer cameraId, public void openCamera(final Integer cameraId,
final CameraOpenListener<Integer, SurfaceHolder.Callback> cameraOpenListener) { final CameraOpenListener<Integer, SurfaceHolder.Callback> cameraOpenListener) {
@ -74,7 +64,44 @@ public class Camera1Manager extends BaseCameraManager<Integer, SurfaceHolder.Cal
uiHandler.post(new Runnable() { uiHandler.post(new Runnable() {
@Override @Override
public void run() { public void run() {
cameraOpenListener.onCameraOpened(cameraId, previewSize, currentInstance); cameraOpenListener.onCameraOpened(cameraId, previewSize, new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
if (surfaceHolder.getSurface() == null) {
return;
}
surface = surfaceHolder.getSurface();
try {
camera.stopPreview();
} catch (Exception ignore) {
}
startPreview(surfaceHolder);
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int width, int height) {
if (surfaceHolder.getSurface() == null) {
return;
}
surface = surfaceHolder.getSurface();
try {
camera.stopPreview();
} catch (Exception ignore) {
}
startPreview(surfaceHolder);
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
}
});
} }
}); });
} }
@ -120,14 +147,19 @@ public class Camera1Manager extends BaseCameraManager<Integer, SurfaceHolder.Cal
} }
@Override @Override
public void takePhoto(File photoFile, CameraPhotoListener cameraPhotoListener) { public void takePhoto(File photoFile, CameraPhotoListener cameraPhotoListener, final CameraFragmentResultListener callback) {
this.outputPath = photoFile; this.outputPath = photoFile;
this.photoListener = cameraPhotoListener; this.photoListener = cameraPhotoListener;
backgroundHandler.post(new Runnable() { backgroundHandler.post(new Runnable() {
@Override @Override
public void run() { public void run() {
setCameraPhotoQuality(camera); setCameraPhotoQuality(camera);
camera.takePicture(null, null, currentInstance); camera.takePicture(null, null, new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] bytes, Camera camera) {
Camera1Manager.this.onPictureTaken(bytes, camera, callback);
}
});
} }
}); });
} }
@ -158,7 +190,7 @@ public class Camera1Manager extends BaseCameraManager<Integer, SurfaceHolder.Cal
} }
@Override @Override
public void stopVideoRecord() { public void stopVideoRecord(@Nullable final CameraFragmentResultListener callback) {
if (isVideoRecording) if (isVideoRecording)
backgroundHandler.post(new Runnable() { backgroundHandler.post(new Runnable() {
@Override @Override
@ -178,7 +210,7 @@ public class Camera1Manager extends BaseCameraManager<Integer, SurfaceHolder.Cal
uiHandler.post(new Runnable() { uiHandler.post(new Runnable() {
@Override @Override
public void run() { public void run() {
videoListener.onVideoRecordStopped(outputPath); videoListener.onVideoRecordStopped(outputPath, callback);
} }
}); });
} }
@ -306,12 +338,12 @@ public class Camera1Manager extends BaseCameraManager<Integer, SurfaceHolder.Cal
@Override @Override
protected void onMaxDurationReached() { protected void onMaxDurationReached() {
stopVideoRecord(); stopVideoRecord(null);
} }
@Override @Override
protected void onMaxFileSizeReached() { protected void onMaxFileSizeReached() {
stopVideoRecord(); stopVideoRecord(null);
} }
@Override @Override
@ -511,45 +543,7 @@ public class Camera1Manager extends BaseCameraManager<Integer, SurfaceHolder.Cal
return rotate; return rotate;
} }
@Override protected void onPictureTaken(final byte[] bytes, Camera camera, final CameraFragmentResultListener callback) {
public void surfaceCreated(SurfaceHolder surfaceHolder) {
if (surfaceHolder.getSurface() == null) {
return;
}
surface = surfaceHolder.getSurface();
try {
camera.stopPreview();
} catch (Exception ignore) {
}
startPreview(surfaceHolder);
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int width, int height) {
if (surfaceHolder.getSurface() == null) {
return;
}
surface = surfaceHolder.getSurface();
try {
camera.stopPreview();
} catch (Exception ignore) {
}
startPreview(surfaceHolder);
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
}
@Override
public void onPictureTaken(final byte[] bytes, Camera camera) {
File pictureFile = outputPath; File pictureFile = outputPath;
if (pictureFile == null) { if (pictureFile == null) {
Log.d(TAG, "Error creating media file, check storage permissions."); Log.d(TAG, "Error creating media file, check storage permissions.");
@ -577,7 +571,7 @@ public class Camera1Manager extends BaseCameraManager<Integer, SurfaceHolder.Cal
uiHandler.post(new Runnable() { uiHandler.post(new Runnable() {
@Override @Override
public void run() { public void run() {
photoListener.onPhotoTaken(bytes, outputPath); photoListener.onPhotoTaken(bytes, outputPath, callback);
} }
}); });
} }

@ -28,15 +28,6 @@ import android.view.Surface;
import android.view.TextureView; import android.view.TextureView;
import android.view.WindowManager; import android.view.WindowManager;
import java.io.File;
import java.io.IOException;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import com.github.florent37.camerafragment.configuration.Configuration; import com.github.florent37.camerafragment.configuration.Configuration;
import com.github.florent37.camerafragment.configuration.ConfigurationProvider; import com.github.florent37.camerafragment.configuration.ConfigurationProvider;
import com.github.florent37.camerafragment.internal.manager.listener.CameraCloseListener; import com.github.florent37.camerafragment.internal.manager.listener.CameraCloseListener;
@ -48,6 +39,16 @@ import com.github.florent37.camerafragment.internal.ui.model.VideoQualityOption;
import com.github.florent37.camerafragment.internal.utils.CameraHelper; import com.github.florent37.camerafragment.internal.utils.CameraHelper;
import com.github.florent37.camerafragment.internal.utils.ImageSaver; import com.github.florent37.camerafragment.internal.utils.ImageSaver;
import com.github.florent37.camerafragment.internal.utils.Size; import com.github.florent37.camerafragment.internal.utils.Size;
import com.github.florent37.camerafragment.listeners.CameraFragmentResultListener;
import java.io.File;
import java.io.IOException;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
/** /**
* Created by memfis on 8/9/16. * Created by memfis on 8/9/16.
@ -57,54 +58,39 @@ public final class Camera2Manager extends BaseCameraManager<String, TextureView.
implements ImageReader.OnImageAvailableListener, TextureView.SurfaceTextureListener { implements ImageReader.OnImageAvailableListener, TextureView.SurfaceTextureListener {
private final static String TAG = "Camera2Manager"; private final static String TAG = "Camera2Manager";
private static final int STATE_PREVIEW = 0;
private static Camera2Manager currentInstance; private static final int STATE_WAITING_LOCK = 1;
private static final int STATE_WAITING_PRE_CAPTURE = 2;
private static final int STATE_WAITING_NON_PRE_CAPTURE = 3;
private static final int STATE_PICTURE_TAKEN = 4;
private CameraOpenListener<String, TextureView.SurfaceTextureListener> cameraOpenListener; private CameraOpenListener<String, TextureView.SurfaceTextureListener> cameraOpenListener;
private CameraPhotoListener cameraPhotoListener; private CameraPhotoListener cameraPhotoListener;
private CameraVideoListener cameraVideoListener; private CameraVideoListener cameraVideoListener;
private File outputPath; private File outputPath;
@CameraPreviewState @CameraPreviewState
private int previewState = STATE_PREVIEW; private int previewState = STATE_PREVIEW;
private static final int STATE_PREVIEW = 0;
private static final int STATE_WAITING_LOCK = 1;
private static final int STATE_WAITING_PRE_CAPTURE = 2;
private static final int STATE_WAITING_NON_PRE_CAPTURE = 3;
private static final int STATE_PICTURE_TAKEN = 4;
@IntDef({STATE_PREVIEW, STATE_WAITING_LOCK, STATE_WAITING_PRE_CAPTURE, STATE_WAITING_NON_PRE_CAPTURE, STATE_PICTURE_TAKEN})
@Retention(RetentionPolicy.SOURCE)
@interface CameraPreviewState {
}
private CameraManager manager; private CameraManager manager;
private CameraDevice cameraDevice; private CameraDevice cameraDevice;
private CaptureRequest previewRequest; private CaptureRequest previewRequest;
private CaptureRequest.Builder previewRequestBuilder; private CaptureRequest.Builder previewRequestBuilder;
private CameraCaptureSession captureSession; private CameraCaptureSession captureSession;
private CameraCharacteristics frontCameraCharacteristics; private CameraCharacteristics frontCameraCharacteristics;
private CameraCharacteristics backCameraCharacteristics; private CameraCharacteristics backCameraCharacteristics;
private StreamConfigurationMap frontCameraStreamConfigurationMap; private StreamConfigurationMap frontCameraStreamConfigurationMap;
private StreamConfigurationMap backCameraStreamConfigurationMap; private StreamConfigurationMap backCameraStreamConfigurationMap;
private SurfaceTexture texture; private SurfaceTexture texture;
private Surface workingSurface; private Surface workingSurface;
private ImageReader imageReader; private ImageReader imageReader;
private CameraDevice.StateCallback stateCallback = new CameraDevice.StateCallback() { private CameraDevice.StateCallback stateCallback = new CameraDevice.StateCallback() {
@Override @Override
public void onOpened(CameraDevice cameraDevice) { public void onOpened(CameraDevice cameraDevice) {
currentInstance.cameraDevice = cameraDevice; Camera2Manager.this.cameraDevice = cameraDevice;
if (cameraOpenListener != null) { if (cameraOpenListener != null) {
uiHandler.post(new Runnable() { uiHandler.post(new Runnable() {
@Override @Override
public void run() { public void run() {
if (!TextUtils.isEmpty(currentCameraId) && previewSize != null && currentInstance != null) if (!TextUtils.isEmpty(currentCameraId) && previewSize != null)
cameraOpenListener.onCameraOpened(currentCameraId, previewSize, currentInstance); cameraOpenListener.onCameraOpened(currentCameraId, previewSize, Camera2Manager.this);
} }
}); });
} }
@ -113,7 +99,7 @@ public final class Camera2Manager extends BaseCameraManager<String, TextureView.
@Override @Override
public void onDisconnected(CameraDevice cameraDevice) { public void onDisconnected(CameraDevice cameraDevice) {
cameraDevice.close(); cameraDevice.close();
currentInstance.cameraDevice = null; Camera2Manager.this.cameraDevice = null;
uiHandler.post(new Runnable() { uiHandler.post(new Runnable() {
@Override @Override
@ -126,7 +112,7 @@ public final class Camera2Manager extends BaseCameraManager<String, TextureView.
@Override @Override
public void onError(CameraDevice cameraDevice, int error) { public void onError(CameraDevice cameraDevice, int error) {
cameraDevice.close(); cameraDevice.close();
currentInstance.cameraDevice = null; Camera2Manager.this.cameraDevice = null;
uiHandler.post(new Runnable() { uiHandler.post(new Runnable() {
@Override @Override
@ -136,7 +122,6 @@ public final class Camera2Manager extends BaseCameraManager<String, TextureView.
}); });
} }
}; };
private CameraCaptureSession.CaptureCallback captureCallback private CameraCaptureSession.CaptureCallback captureCallback
= new CameraCaptureSession.CaptureCallback() { = new CameraCaptureSession.CaptureCallback() {
@ -155,14 +140,7 @@ public final class Camera2Manager extends BaseCameraManager<String, TextureView.
} }
}; };
private CameraFragmentResultListener callback;
private Camera2Manager() {
}
public static Camera2Manager getInstance() {
if (currentInstance == null) currentInstance = new Camera2Manager();
return currentInstance;
}
@Override @Override
public void initializeCameraManager(ConfigurationProvider configurationProvider, Context context) { public void initializeCameraManager(ConfigurationProvider configurationProvider, Context context) {
@ -259,9 +237,10 @@ public final class Camera2Manager extends BaseCameraManager<String, TextureView.
} }
@Override @Override
public void takePhoto(File photoFile, CameraPhotoListener cameraPhotoListener) { public void takePhoto(File photoFile, CameraPhotoListener cameraPhotoListener, CameraFragmentResultListener callback) {
this.outputPath = photoFile; this.outputPath = photoFile;
this.cameraPhotoListener = cameraPhotoListener; this.cameraPhotoListener = cameraPhotoListener;
this.callback = callback;
backgroundHandler.post(new Runnable() { backgroundHandler.post(new Runnable() {
@Override @Override
@ -292,7 +271,7 @@ public final class Camera2Manager extends BaseCameraManager<String, TextureView.
closePreviewSession(); closePreviewSession();
if (prepareVideoRecorder()) { if (prepareVideoRecorder()) {
SurfaceTexture texture = currentInstance.texture; SurfaceTexture texture = Camera2Manager.this.texture;
texture.setDefaultBufferSize(videoSize.getWidth(), videoSize.getHeight()); texture.setDefaultBufferSize(videoSize.getWidth(), videoSize.getHeight());
try { try {
@ -348,7 +327,7 @@ public final class Camera2Manager extends BaseCameraManager<String, TextureView.
} }
@Override @Override
public void stopVideoRecord() { public void stopVideoRecord(final CameraFragmentResultListener callback) {
if (isVideoRecording) if (isVideoRecording)
backgroundHandler.post(new Runnable() { backgroundHandler.post(new Runnable() {
@Override @Override
@ -368,7 +347,7 @@ public final class Camera2Manager extends BaseCameraManager<String, TextureView.
uiHandler.post(new Runnable() { uiHandler.post(new Runnable() {
@Override @Override
public void run() { public void run() {
cameraVideoListener.onVideoRecordStopped(outputPath); cameraVideoListener.onVideoRecordStopped(outputPath, callback);
} }
}); });
} }
@ -376,8 +355,6 @@ public final class Camera2Manager extends BaseCameraManager<String, TextureView.
}); });
} }
//--------------------Internal methods------------------
private void startPreview(SurfaceTexture texture) { private void startPreview(SurfaceTexture texture) {
try { try {
if (texture == null) return; if (texture == null) return;
@ -410,14 +387,16 @@ public final class Camera2Manager extends BaseCameraManager<String, TextureView.
} }
} }
//--------------------Internal methods------------------
@Override @Override
protected void onMaxDurationReached() { protected void onMaxDurationReached() {
stopVideoRecord(); stopVideoRecord(callback);
} }
@Override @Override
protected void onMaxFileSizeReached() { protected void onMaxFileSizeReached() {
stopVideoRecord(); stopVideoRecord(callback);
} }
@Override @Override
@ -762,7 +741,8 @@ public final class Camera2Manager extends BaseCameraManager<String, TextureView.
uiHandler.post(new Runnable() { uiHandler.post(new Runnable() {
@Override @Override
public void run() { public void run() {
cameraPhotoListener.onPhotoTaken(bytes, outputPath); cameraPhotoListener.onPhotoTaken(bytes, outputPath, callback);
callback = null;
} }
}); });
} }
@ -841,4 +821,9 @@ public final class Camera2Manager extends BaseCameraManager<String, TextureView.
return array; return array;
} }
@IntDef({STATE_PREVIEW, STATE_WAITING_LOCK, STATE_WAITING_PRE_CAPTURE, STATE_WAITING_NON_PRE_CAPTURE, STATE_PICTURE_TAKEN})
@Retention(RetentionPolicy.SOURCE)
@interface CameraPreviewState {
}
} }

@ -1,12 +1,14 @@
package com.github.florent37.camerafragment.internal.manager.listener; package com.github.florent37.camerafragment.internal.manager.listener;
import com.github.florent37.camerafragment.listeners.CameraFragmentResultListener;
import java.io.File; import java.io.File;
/** /**
* Created by memfis on 8/14/16. * Created by memfis on 8/14/16.
*/ */
public interface CameraPhotoListener { public interface CameraPhotoListener {
void onPhotoTaken(byte[] bytes, File photoFile); void onPhotoTaken(byte[] bytes, File photoFile, CameraFragmentResultListener callback);
void onPhotoTakeError(); void onPhotoTakeError();
} }

@ -3,6 +3,7 @@ package com.github.florent37.camerafragment.internal.manager.listener;
import java.io.File; import java.io.File;
import com.github.florent37.camerafragment.internal.utils.Size; import com.github.florent37.camerafragment.internal.utils.Size;
import com.github.florent37.camerafragment.listeners.CameraFragmentResultListener;
/** /**
* Created by memfis on 8/14/16. * Created by memfis on 8/14/16.
@ -10,7 +11,7 @@ import com.github.florent37.camerafragment.internal.utils.Size;
public interface CameraVideoListener { public interface CameraVideoListener {
void onVideoRecordStarted(Size videoSize); void onVideoRecordStarted(Size videoSize);
void onVideoRecordStopped(File videoFile); void onVideoRecordStopped(File videoFile, CameraFragmentResultListener callback);
void onVideoRecordError(); void onVideoRecordError();
} }

@ -22,6 +22,7 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.WindowManager; import android.view.WindowManager;
import com.github.florent37.camerafragment.CameraFragmentApi;
import com.github.florent37.camerafragment.R; import com.github.florent37.camerafragment.R;
import com.github.florent37.camerafragment.configuration.Configuration; import com.github.florent37.camerafragment.configuration.Configuration;
import com.github.florent37.camerafragment.configuration.ConfigurationProvider; import com.github.florent37.camerafragment.configuration.ConfigurationProvider;
@ -55,7 +56,7 @@ import java.io.File;
* Updated by Florent37 * Updated by Florent37
*/ */
public abstract class BaseAnncaFragment<CameraId> extends Fragment { public abstract class BaseAnncaFragment<CameraId> extends Fragment implements CameraFragmentApi {
public static final String ARG_CONFIGURATION = "configuration"; public static final String ARG_CONFIGURATION = "configuration";
public static final int MIN_VERSION_ICECREAM = Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1; public static final int MIN_VERSION_ICECREAM = Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1;
@ -191,22 +192,25 @@ public abstract class BaseAnncaFragment<CameraId> extends Fragment {
} }
@Override @Override
public void onPhotoTaken(byte[] bytes) { public void onPhotoTaken(byte[] bytes, CameraFragmentResultListener callback) {
if (cameraFragmentResultListener != null) { if (cameraFragmentResultListener != null) {
final String filePath = getCameraController().getOutputFile().toString(); final String filePath = cameraController.getOutputFile().toString();
cameraFragmentResultListener.onPhotoTaken(bytes, filePath); cameraFragmentResultListener.onPhotoTaken(bytes, filePath);
if (callback != null) {
callback.onPhotoTaken(bytes, filePath);
}
} }
} }
@Override @Override
public void onVideoRecordStart(int width, int height) { public void onVideoRecordStart(int width, int height) {
final File outputFile = getCameraController().getOutputFile(); final File outputFile = cameraController.getOutputFile();
onStartVideoRecord(outputFile); onStartVideoRecord(outputFile);
} }
@Override @Override
public void onVideoRecordStop() { public void onVideoRecordStop(@Nullable CameraFragmentResultListener callback) {
BaseAnncaFragment.this.onStopVideoRecord(); BaseAnncaFragment.this.onStopVideoRecord(callback);
} }
@Override @Override
@ -252,15 +256,16 @@ public abstract class BaseAnncaFragment<CameraId> extends Fragment {
} }
public void takePhotoOrCaptureVideo() { @Override
public void takePhotoOrCaptureVideo(final CameraFragmentResultListener resultListener) {
switch (currentMediaActionState) { switch (currentMediaActionState) {
case MediaAction.ACTION_PHOTO: case MediaAction.ACTION_PHOTO:
takePhoto(); takePhoto(resultListener);
break; break;
case MediaAction.ACTION_VIDEO: case MediaAction.ACTION_VIDEO:
switch (currentRecordState) { switch (currentRecordState) {
case Record.RECORD_IN_PROGRESS_STATE: case Record.RECORD_IN_PROGRESS_STATE:
stopRecording(); stopRecording(resultListener);
break; break;
default: default:
startRecording(); startRecording();
@ -303,11 +308,11 @@ public abstract class BaseAnncaFragment<CameraId> extends Fragment {
cameraController.onDestroy(); cameraController.onDestroy();
} }
public void setMaxVideoFileSize(long maxVideoFileSize) { protected void setMaxVideoFileSize(long maxVideoFileSize) {
this.maxVideoFileSize = maxVideoFileSize; this.maxVideoFileSize = maxVideoFileSize;
} }
public void setMaxVideoDuration(int maxVideoDurationInMillis) { protected void setMaxVideoDuration(int maxVideoDurationInMillis) {
final TimerTaskBase.Callback callback = new TimerTaskBase.Callback() { final TimerTaskBase.Callback callback = new TimerTaskBase.Callback() {
@Override @Override
public void setText(String text) { public void setText(String text) {
@ -331,19 +336,30 @@ public abstract class BaseAnncaFragment<CameraId> extends Fragment {
} }
} }
@Override
public void openSettingDialog() { public void openSettingDialog() {
final Context context = getContext(); final Context context = getContext();
AlertDialog.Builder builder = new AlertDialog.Builder(context); AlertDialog.Builder builder = new AlertDialog.Builder(context);
if (currentMediaActionState == MediaAction.ACTION_VIDEO) { if (currentMediaActionState == MediaAction.ACTION_VIDEO) {
builder.setSingleChoiceItems(videoQualities, getVideoOptionCheckedIndex(), getVideoOptionSelectedListener()); builder.setSingleChoiceItems(videoQualities, getVideoOptionCheckedIndex(), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int index) {
newQuality = ((VideoQualityOption) videoQualities[index]).getMediaQuality();
}
});
if (configurationProvider.getVideoFileSize() > 0) if (configurationProvider.getVideoFileSize() > 0)
builder.setTitle(String.format(getString(R.string.settings_video_quality_title), builder.setTitle(String.format(getString(R.string.settings_video_quality_title),
"(Max " + String.valueOf(configurationProvider.getVideoFileSize() / (1024 * 1024) + " MB)"))); "(Max " + String.valueOf(configurationProvider.getVideoFileSize() / (1024 * 1024) + " MB)")));
else else
builder.setTitle(String.format(getString(R.string.settings_video_quality_title), "")); builder.setTitle(String.format(getString(R.string.settings_video_quality_title), ""));
} else { } else {
builder.setSingleChoiceItems(photoQualities, getPhotoOptionCheckedIndex(), getPhotoOptionSelectedListener()); builder.setSingleChoiceItems(photoQualities, getPhotoOptionCheckedIndex(), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int index) {
newQuality = ((PhotoQualityOption) photoQualities[index]).getMediaQuality();
}
});
builder.setTitle(R.string.settings_photo_quality_title); builder.setTitle(R.string.settings_photo_quality_title);
} }
@ -356,7 +372,7 @@ public abstract class BaseAnncaFragment<CameraId> extends Fragment {
if (cameraFragmentControlsListener != null) { if (cameraFragmentControlsListener != null) {
cameraFragmentControlsListener.lockControls(); cameraFragmentControlsListener.lockControls();
} }
getCameraController().switchQuality(); cameraController.switchQuality();
} }
} }
}); });
@ -375,6 +391,7 @@ public abstract class BaseAnncaFragment<CameraId> extends Fragment {
settingsDialog.getWindow().setAttributes(layoutParams); settingsDialog.getWindow().setAttributes(layoutParams);
} }
@Override
public void switchCameraType() { public void switchCameraType() {
if (cameraFragmentControlsListener != null) { if (cameraFragmentControlsListener != null) {
cameraFragmentControlsListener.lockControls(); cameraFragmentControlsListener.lockControls();
@ -399,10 +416,11 @@ public abstract class BaseAnncaFragment<CameraId> extends Fragment {
break; break;
} }
getCameraController().switchCamera(cameraFace); this.cameraController.switchCamera(cameraFace);
} }
public void switchAction() { @Override
public void switchActionPhotoVideo() {
switch (currentMediaActionState) { switch (currentMediaActionState) {
case MediaAction.ACTION_PHOTO: case MediaAction.ACTION_PHOTO:
currentMediaActionState = MediaAction.ACTION_VIDEO; currentMediaActionState = MediaAction.ACTION_VIDEO;
@ -419,6 +437,7 @@ public abstract class BaseAnncaFragment<CameraId> extends Fragment {
} }
} }
@Override
public void toggleFlashMode() { public void toggleFlashMode() {
switch (currentFlashMode) { switch (currentFlashMode) {
case Flash.FLASH_AUTO: case Flash.FLASH_AUTO:
@ -439,17 +458,17 @@ public abstract class BaseAnncaFragment<CameraId> extends Fragment {
case Flash.FLASH_AUTO: case Flash.FLASH_AUTO:
if (cameraFragmentStateListener != null) cameraFragmentStateListener.onFlashAuto(); if (cameraFragmentStateListener != null) cameraFragmentStateListener.onFlashAuto();
configurationProvider.setFlashMode(Configuration.FLASH_MODE_AUTO); configurationProvider.setFlashMode(Configuration.FLASH_MODE_AUTO);
getCameraController().setFlashMode(Configuration.FLASH_MODE_AUTO); this.cameraController.setFlashMode(Configuration.FLASH_MODE_AUTO);
break; break;
case Flash.FLASH_ON: case Flash.FLASH_ON:
if (cameraFragmentStateListener != null) cameraFragmentStateListener.onFlashOn(); if (cameraFragmentStateListener != null) cameraFragmentStateListener.onFlashOn();
configurationProvider.setFlashMode(Configuration.FLASH_MODE_ON); configurationProvider.setFlashMode(Configuration.FLASH_MODE_ON);
getCameraController().setFlashMode(Configuration.FLASH_MODE_ON); this.cameraController.setFlashMode(Configuration.FLASH_MODE_ON);
break; break;
case Flash.FLASH_OFF: case Flash.FLASH_OFF:
if (cameraFragmentStateListener != null) cameraFragmentStateListener.onFlashOff(); if (cameraFragmentStateListener != null) cameraFragmentStateListener.onFlashOff();
configurationProvider.setFlashMode(Configuration.FLASH_MODE_OFF); configurationProvider.setFlashMode(Configuration.FLASH_MODE_OFF);
getCameraController().setFlashMode(Configuration.FLASH_MODE_OFF); this.cameraController.setFlashMode(Configuration.FLASH_MODE_OFF);
break; break;
} }
} }
@ -461,12 +480,7 @@ public abstract class BaseAnncaFragment<CameraId> extends Fragment {
rotateSettingsDialog(degrees); rotateSettingsDialog(degrees);
} }
@Record.RecordState protected void setRecordState(@Record.RecordState int recordState) {
public int getRecordState() {
return currentRecordState;
}
public void setRecordState(@Record.RecordState int recordState) {
this.currentRecordState = recordState; this.currentRecordState = recordState;
} }
@ -493,12 +507,12 @@ public abstract class BaseAnncaFragment<CameraId> extends Fragment {
// } // }
//} //}
public void setFlashMode(@Flash.FlashMode int mode) { protected void setFlashMode(@Flash.FlashMode int mode) {
this.currentFlashMode = mode; this.currentFlashMode = mode;
onFlashModeChanged(); onFlashModeChanged();
} }
private void rotateSettingsDialog(int degrees) { protected void rotateSettingsDialog(int degrees) {
if (settingsDialog != null && settingsDialog.isShowing() && Build.VERSION.SDK_INT > 10) { if (settingsDialog != null && settingsDialog.isShowing() && Build.VERSION.SDK_INT > 10) {
ViewGroup dialogView = (ViewGroup) settingsDialog.getWindow().getDecorView(); ViewGroup dialogView = (ViewGroup) settingsDialog.getWindow().getDecorView();
for (int i = 0; i < dialogView.getChildCount(); i++) { for (int i = 0; i < dialogView.getChildCount(); i++) {
@ -535,73 +549,51 @@ public abstract class BaseAnncaFragment<CameraId> extends Fragment {
return checkedIndex; return checkedIndex;
} }
protected DialogInterface.OnClickListener getVideoOptionSelectedListener() { protected void takePhoto(CameraFragmentResultListener callback) {
return new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int index) {
newQuality = ((VideoQualityOption) videoQualities[index]).getMediaQuality();
}
};
}
protected DialogInterface.OnClickListener getPhotoOptionSelectedListener() {
return new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int index) {
newQuality = ((PhotoQualityOption) photoQualities[index]).getMediaQuality();
}
};
}
public void takePhoto() {
if (Build.VERSION.SDK_INT > MIN_VERSION_ICECREAM) { if (Build.VERSION.SDK_INT > MIN_VERSION_ICECREAM) {
new MediaActionSound().play(MediaActionSound.SHUTTER_CLICK); new MediaActionSound().play(MediaActionSound.SHUTTER_CLICK);
} }
setRecordState(Record.TAKE_PHOTO_STATE); setRecordState(Record.TAKE_PHOTO_STATE);
getCameraController().takePhoto(); this.cameraController.takePhoto(callback);
if (cameraFragmentStateListener != null) { if (cameraFragmentStateListener != null) {
cameraFragmentStateListener.onRecordStatePhoto(); cameraFragmentStateListener.onRecordStatePhoto();
} }
} }
public void startRecording() { protected void startRecording() {
if (Build.VERSION.SDK_INT > MIN_VERSION_ICECREAM) { if (Build.VERSION.SDK_INT > MIN_VERSION_ICECREAM) {
new MediaActionSound().play(MediaActionSound.START_VIDEO_RECORDING); new MediaActionSound().play(MediaActionSound.START_VIDEO_RECORDING);
} }
setRecordState(Record.RECORD_IN_PROGRESS_STATE); setRecordState(Record.RECORD_IN_PROGRESS_STATE);
getCameraController().startVideoRecord(); this.cameraController.startVideoRecord();
if (cameraFragmentStateListener != null) { if (cameraFragmentStateListener != null) {
cameraFragmentStateListener.onRecordStateVideoInProgress(); cameraFragmentStateListener.onRecordStateVideoInProgress();
} }
} }
public void stopRecording() { protected void stopRecording(CameraFragmentResultListener callback) {
if (Build.VERSION.SDK_INT > MIN_VERSION_ICECREAM) { if (Build.VERSION.SDK_INT > MIN_VERSION_ICECREAM) {
new MediaActionSound().play(MediaActionSound.STOP_VIDEO_RECORDING); new MediaActionSound().play(MediaActionSound.STOP_VIDEO_RECORDING);
} }
setRecordState(Record.READY_FOR_RECORD_STATE); setRecordState(Record.READY_FOR_RECORD_STATE);
getCameraController().stopVideoRecord(); this.cameraController.stopVideoRecord(callback);
this.onStopVideoRecord(); this.onStopVideoRecord(callback);
if (cameraFragmentStateListener != null) { if (cameraFragmentStateListener != null) {
cameraFragmentStateListener.onRecordStateVideoReadyForRecord(); cameraFragmentStateListener.onRecordStateVideoReadyForRecord();
} }
} }
public CameraController<CameraId> getCameraController() { protected void clearCameraPreview() {
return cameraController;
}
public void clearCameraPreview() {
if (previewContainer != null) if (previewContainer != null)
previewContainer.removeAllViews(); previewContainer.removeAllViews();
} }
public void setCameraPreview(View preview, Size previewSize) { protected void setCameraPreview(View preview, Size previewSize) {
//onCameraControllerReady() //onCameraControllerReady()
videoQualities = cameraController.getVideoQualityOptions(); videoQualities = cameraController.getVideoQualityOptions();
photoQualities = cameraController.getPhotoQualityOptions(); photoQualities = cameraController.getPhotoQualityOptions();
@ -613,11 +605,11 @@ public abstract class BaseAnncaFragment<CameraId> extends Fragment {
previewContainer.setAspectRatio(previewSize.getHeight() / (double) previewSize.getWidth()); previewContainer.setAspectRatio(previewSize.getHeight() / (double) previewSize.getWidth());
} }
public void setMediaFilePath(final File mediaFile) { protected void setMediaFilePath(final File mediaFile) {
this.mediaFilePath = mediaFile.toString(); this.mediaFilePath = mediaFile.toString();
} }
public void onStartVideoRecord(final File mediaFile) { protected void onStartVideoRecord(final File mediaFile) {
setMediaFilePath(mediaFile); setMediaFilePath(mediaFile);
if (maxVideoFileSize > 0) { if (maxVideoFileSize > 0) {
@ -657,7 +649,7 @@ public abstract class BaseAnncaFragment<CameraId> extends Fragment {
} }
} }
public void onStopVideoRecord() { protected void onStopVideoRecord(@Nullable CameraFragmentResultListener callback) {
if (cameraFragmentControlsListener != null) { if (cameraFragmentControlsListener != null) {
cameraFragmentControlsListener.allowRecord(false); cameraFragmentControlsListener.allowRecord(false);
} }
@ -679,24 +671,32 @@ public abstract class BaseAnncaFragment<CameraId> extends Fragment {
} }
} }
final String filePath = this.cameraController.getOutputFile().toString();
if (cameraFragmentResultListener != null) { if (cameraFragmentResultListener != null) {
final String filePath = getCameraController().getOutputFile().toString();
cameraFragmentResultListener.onVideoRecorded(filePath); cameraFragmentResultListener.onVideoRecorded(filePath);
} }
if (callback != null) {
callback.onVideoRecorded(filePath);
}
} }
@Override
public void setStateListener(CameraFragmentStateListener cameraFragmentStateListener) { public void setStateListener(CameraFragmentStateListener cameraFragmentStateListener) {
this.cameraFragmentStateListener = cameraFragmentStateListener; this.cameraFragmentStateListener = cameraFragmentStateListener;
} }
@Override
public void setTextListener(CameraFragmentVideoRecordTextListener cameraFragmentVideoRecordTextListener) { public void setTextListener(CameraFragmentVideoRecordTextListener cameraFragmentVideoRecordTextListener) {
this.cameraFragmentVideoRecordTextListener = cameraFragmentVideoRecordTextListener; this.cameraFragmentVideoRecordTextListener = cameraFragmentVideoRecordTextListener;
} }
@Override
public void setControlsListener(CameraFragmentControlsListener cameraFragmentControlsListener) { public void setControlsListener(CameraFragmentControlsListener cameraFragmentControlsListener) {
this.cameraFragmentControlsListener = cameraFragmentControlsListener; this.cameraFragmentControlsListener = cameraFragmentControlsListener;
} }
@Override
public void setResultListener(CameraFragmentResultListener cameraFragmentResultListener) { public void setResultListener(CameraFragmentResultListener cameraFragmentResultListener) {
this.cameraFragmentResultListener = cameraFragmentResultListener; this.cameraFragmentResultListener = cameraFragmentResultListener;
} }

@ -18,13 +18,6 @@ import com.github.florent37.camerafragment.internal.utils.Utils;
*/ */
public class CameraSwitchView extends AppCompatImageButton { public class CameraSwitchView extends AppCompatImageButton {
@Nullable
private OnCameraTypeChangeListener onCameraTypeChangeListener;
public interface OnCameraTypeChangeListener {
void switchCameraType();
}
private Drawable frontCameraDrawable; private Drawable frontCameraDrawable;
private Drawable rearCameraDrawable; private Drawable rearCameraDrawable;
private int padding = 5; private int padding = 5;
@ -53,14 +46,6 @@ public class CameraSwitchView extends AppCompatImageButton {
DrawableCompat.setTintList(rearCameraDrawable.mutate(), ContextCompat.getColorStateList(context, R.drawable.switch_camera_mode_selector)); DrawableCompat.setTintList(rearCameraDrawable.mutate(), ContextCompat.getColorStateList(context, R.drawable.switch_camera_mode_selector));
setBackgroundResource(R.drawable.circle_frame_background_dark); setBackgroundResource(R.drawable.circle_frame_background_dark);
setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if (onCameraTypeChangeListener != null) {
onCameraTypeChangeListener.switchCameraType();
}
}
});
displayBackCamera(); displayBackCamera();
padding = Utils.convertDipToPixels(context, padding); padding = Utils.convertDipToPixels(context, padding);
@ -86,8 +71,4 @@ public class CameraSwitchView extends AppCompatImageButton {
} }
} }
} }
public void setOnCameraTypeChangeListener(OnCameraTypeChangeListener listener) {
this.onCameraTypeChangeListener = listener;
}
} }

@ -19,17 +19,10 @@ import com.github.florent37.camerafragment.R;
*/ */
public class FlashSwitchView extends ImageButton { public class FlashSwitchView extends ImageButton {
@Nullable
private FlashModeSwitchListener switchListener;
private Drawable flashOnDrawable; private Drawable flashOnDrawable;
private Drawable flashOffDrawable; private Drawable flashOffDrawable;
private Drawable flashAutoDrawable; private Drawable flashAutoDrawable;
public interface FlashModeSwitchListener {
void toggleFlashMode();
}
public FlashSwitchView(@NonNull Context context) { public FlashSwitchView(@NonNull Context context) {
this(context, null); this(context, null);
} }
@ -44,14 +37,6 @@ public class FlashSwitchView extends ImageButton {
private void init() { private void init() {
setBackgroundColor(Color.TRANSPARENT); setBackgroundColor(Color.TRANSPARENT);
setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if (switchListener != null) {
switchListener.toggleFlashMode();
}
}
});
} }
public void displayFlashOff() { public void displayFlashOff() {
@ -66,10 +51,6 @@ public class FlashSwitchView extends ImageButton {
setImageDrawable(flashAutoDrawable); setImageDrawable(flashAutoDrawable);
} }
public void setFlashSwitchListener(@NonNull FlashModeSwitchListener switchListener) {
this.switchListener = switchListener;
}
@Override @Override
public void setEnabled(boolean enabled) { public void setEnabled(boolean enabled) {
super.setEnabled(enabled); super.setEnabled(enabled);

Loading…
Cancel
Save