Fixed configuration

pull/17/head
Florent CHAMPIGNY 8 years ago
parent bf492ff687
commit 981c5647cd
  1. 2
      app/src/main/java/com/github/florent37/camerafragment/sample/MainActivity.java
  2. 180
      app/src/main/java/com/github/florent37/camerafragment/sample/MainActivityCustoms.java
  3. 2
      camerafragment/src/main/java/com/github/florent37/camerafragment/CameraFragmentApi.java
  4. 8
      camerafragment/src/main/java/com/github/florent37/camerafragment/configuration/Configuration.java
  5. 3
      camerafragment/src/main/java/com/github/florent37/camerafragment/configuration/ConfigurationProvider.java
  6. 19
      camerafragment/src/main/java/com/github/florent37/camerafragment/configuration/ConfigurationProviderImpl.java
  7. 10
      camerafragment/src/main/java/com/github/florent37/camerafragment/internal/controller/impl/Camera1Controller.java
  8. 26
      camerafragment/src/main/java/com/github/florent37/camerafragment/internal/manager/impl/Camera1Manager.java
  9. 137
      camerafragment/src/main/java/com/github/florent37/camerafragment/internal/ui/BaseAnncaFragment.java
  10. BIN
      media/sample_small.png

@ -72,7 +72,7 @@ public class MainActivity extends AppCompatActivity {
public void onSwitchCameraClicked(){
final CameraFragmentApi cameraFragment = getCameraFragment();
if (cameraFragment != null) {
cameraFragment.switchCameraType();
cameraFragment.switchCameraTypeFrontBack();
}
}

@ -12,21 +12,13 @@ import android.support.v4.view.ViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.github.florent37.camerafragment.CameraFragment;
import com.github.florent37.camerafragment.CameraFragmentApi;
import com.github.florent37.camerafragment.PreviewActivity;
import com.github.florent37.camerafragment.configuration.Configuration;
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;
import com.github.florent37.camerafragment.widgets.CameraSettingsView;
import com.github.florent37.camerafragment.widgets.CameraSwitchView;
import com.github.florent37.camerafragment.widgets.FlashSwitchView;
import com.github.florent37.camerafragment.widgets.MediaActionSwitchView;
import com.github.florent37.camerafragment.widgets.RecordButton;
import java.io.File;
import java.util.ArrayList;
@ -59,93 +51,18 @@ public class MainActivityCustoms extends AppCompatActivity {
ButterKnife.bind(this);
}
@OnClick(R.id.flash_switch_view)
public void onFlashSwitcClicked(){
final CameraFragmentApi cameraFragment = getCameraFragment();
if (cameraFragment != null) {
cameraFragment.toggleFlashMode();
}
}
@OnClick(R.id.front_back_camera_switcher)
public void onSwitchCameraClicked(){
final CameraFragmentApi cameraFragment = getCameraFragment();
if (cameraFragment != null) {
cameraFragment.switchCameraType();
}
}
@OnClick(R.id.record_button)
public void onRecordButtonClicked(){
final CameraFragmentApi cameraFragment = getCameraFragment();
if (cameraFragment != null) {
cameraFragment.takePhotoOrCaptureVideo(new CameraFragmentResultListener() {
@Override
public void onVideoRecorded(String filePath) {
}
@Override
public void onPhotoTaken(byte[] bytes, String filePath) {
}
});
}
}
@OnClick(R.id.settings_view)
public void onSettingsClicked(){
final CameraFragmentApi cameraFragment = getCameraFragment();
if (cameraFragment != null) {
cameraFragment.openSettingDialog();
}
}
@OnClick(R.id.photo_video_camera_switcher)
public void onMediaActionSwitchClicked(){
final CameraFragmentApi cameraFragment = getCameraFragment();
if (cameraFragment != null) {
cameraFragment.switchActionPhotoVideo();
}
}
@OnClick(R.id.addCameraButton)
public void onAddCameraClicked(){
if (Build.VERSION.SDK_INT > 15) {
final String[] permissions = {
Manifest.permission.CAMERA,
Manifest.permission.RECORD_AUDIO,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE};
final List<String> permissionsToRequest = new ArrayList<>();
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
permissionsToRequest.add(permission);
}
}
if (!permissionsToRequest.isEmpty()) {
ActivityCompat.requestPermissions(this, permissionsToRequest.toArray(new String[permissionsToRequest.size()]), REQUEST_CAMERA_PERMISSIONS);
} else addCamera();
} else {
addCamera();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults.length != 0) {
addCamera();
}
}
@RequiresPermission(Manifest.permission.CAMERA)
public void addCamera() {
addCameraButton.setVisibility(View.GONE);
cameraLayout.setVisibility(View.VISIBLE);
final CameraFragment cameraFragment = CameraFragment.newInstance(new Configuration.Builder().build());
final Configuration.Builder builder = new Configuration.Builder();
builder
.setCamera(Configuration.CAMERA_FACE_FRONT)
.setFlashMode(Configuration.FLASH_MODE_ON)
.setMediaAction(Configuration.MEDIA_ACTION_VIDEO);
final CameraFragment cameraFragment = CameraFragment.newInstance(builder.build());
getSupportFragmentManager().beginTransaction()
.replace(R.id.content, cameraFragment, FRAGMENT_TAG)
.commit();
@ -195,12 +112,14 @@ public class MainActivityCustoms extends AppCompatActivity {
@Override
public void onCameraSetupForPhoto() {
mediaActionSwitchView.setText("photo");
recordButton.setText("take photo");
flashSwitchView.setVisibility(View.VISIBLE);
}
@Override
public void onCameraSetupForVideo() {
mediaActionSwitchView.setText("video");
recordButton.setText("capture video");
flashSwitchView.setVisibility(View.GONE);
}
@ -239,6 +158,87 @@ public class MainActivityCustoms extends AppCompatActivity {
}
}
@OnClick(R.id.flash_switch_view)
public void onFlashSwitcClicked(){
final CameraFragmentApi cameraFragment = getCameraFragment();
if (cameraFragment != null) {
cameraFragment.toggleFlashMode();
}
}
@OnClick(R.id.front_back_camera_switcher)
public void onSwitchCameraClicked(){
final CameraFragmentApi cameraFragment = getCameraFragment();
if (cameraFragment != null) {
cameraFragment.switchCameraTypeFrontBack();
}
}
@OnClick(R.id.record_button)
public void onRecordButtonClicked(){
final CameraFragmentApi cameraFragment = getCameraFragment();
if (cameraFragment != null) {
cameraFragment.takePhotoOrCaptureVideo(new CameraFragmentResultListener() {
@Override
public void onVideoRecorded(String filePath) {
}
@Override
public void onPhotoTaken(byte[] bytes, String filePath) {
}
});
}
}
@OnClick(R.id.settings_view)
public void onSettingsClicked(){
final CameraFragmentApi cameraFragment = getCameraFragment();
if (cameraFragment != null) {
cameraFragment.openSettingDialog();
}
}
@OnClick(R.id.photo_video_camera_switcher)
public void onMediaActionSwitchClicked(){
final CameraFragmentApi cameraFragment = getCameraFragment();
if (cameraFragment != null) {
cameraFragment.switchActionPhotoVideo();
}
}
@OnClick(R.id.addCameraButton)
public void onAddCameraClicked(){
if (Build.VERSION.SDK_INT > 15) {
final String[] permissions = {
Manifest.permission.CAMERA,
Manifest.permission.RECORD_AUDIO,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE};
final List<String> permissionsToRequest = new ArrayList<>();
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
permissionsToRequest.add(permission);
}
}
if (!permissionsToRequest.isEmpty()) {
ActivityCompat.requestPermissions(this, permissionsToRequest.toArray(new String[permissionsToRequest.size()]), REQUEST_CAMERA_PERMISSIONS);
} else addCamera();
} else {
addCamera();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults.length != 0) {
addCamera();
}
}
private CameraFragmentApi getCameraFragment() {
return (CameraFragmentApi) getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG);
}

@ -15,7 +15,7 @@ public interface CameraFragmentApi {
void openSettingDialog();
void switchCameraType();
void switchCameraTypeFrontBack();
void switchActionPhotoVideo();

@ -149,6 +149,11 @@ public final class Configuration implements Serializable {
return this;
}
public Builder setCamera(@CameraFace int camera){
configuration.cameraFace = camera;
return this;
}
public Configuration build() throws IllegalArgumentException {
if (configuration.mediaQuality == MEDIA_QUALITY_AUTO && configuration.minimumVideoDuration < 0) {
throw new IllegalStateException("Please provide minimum video duration in milliseconds to use auto quality.");
@ -159,6 +164,7 @@ public final class Configuration implements Serializable {
}
@MediaAction
public int getMediaAction() {
return mediaAction;
}
@ -167,6 +173,7 @@ public final class Configuration implements Serializable {
return mediaQuality;
}
@CameraFace
public int getCameraFace() {
return cameraFace;
}
@ -183,6 +190,7 @@ public final class Configuration implements Serializable {
return minimumVideoDuration;
}
@FlashMode
public int getFlashMode() {
return flashMode;
}

@ -25,6 +25,9 @@ public interface ConfigurationProvider {
@Configuration.FlashMode
int getFlashMode();
@Configuration.CameraFace
int getCameraFace();
void setMediaQuality(int mediaQuality);
void setPassedMediaQuality(int mediaQuality);

@ -22,6 +22,9 @@ public class ConfigurationProviderImpl implements ConfigurationProvider {
@Configuration.FlashMode
protected int flashMode = Configuration.FLASH_MODE_AUTO;
@Configuration.CameraFace
protected int cameraFace = Configuration.CAMERA_FACE_REAR;
@Configuration.SensorPosition
protected int sensorPosition = Configuration.SENSOR_POSITION_UNSPECIFIED;
@ -100,7 +103,7 @@ public class ConfigurationProviderImpl implements ConfigurationProvider {
this.flashMode = flashMode;
}
public void setMediaAction(int mediaAction) {
public void setMediaAction(@Configuration.MediaAction int mediaAction) {
this.mediaAction = mediaAction;
}
@ -108,6 +111,15 @@ public class ConfigurationProviderImpl implements ConfigurationProvider {
return passedMediaQuality;
}
@Configuration.CameraFace
public int getCameraFace() {
return cameraFace;
}
public void setCameraFace(@Configuration.CameraFace int cameraFace) {
this.cameraFace = cameraFace;
}
@Override
public void setupWithAnnaConfiguration(Configuration configuration) {
if (configuration != null) {
@ -160,6 +172,11 @@ public class ConfigurationProviderImpl implements ConfigurationProvider {
setVideoDuration(videoDuration);
}
final int cameraFace = configuration.getCameraFace();
if (cameraFace != -1) {
setCameraFace(cameraFace);
}
final long videoFileSize = configuration.getVideoFileSize();
if (videoFileSize != -1) {
setVideoFileSize(videoFileSize);

@ -99,8 +99,14 @@ public class Camera1Controller implements CameraController<Integer>,
@Override
public void switchCamera(@Configuration.CameraFace final int cameraFace) {
setCurrentCameraId(cameraManager.getCurrentCameraId().equals(cameraManager.getFaceFrontCameraId()) ?
cameraManager.getFaceBackCameraId() : cameraManager.getFaceFrontCameraId());
final Integer backCameraId = cameraManager.getFaceBackCameraId();
final Integer frontCameraId = cameraManager.getFaceFrontCameraId();
if(cameraManager.getCurrentCameraId().equals(frontCameraId)){
setCurrentCameraId(backCameraId);
} else {
setCurrentCameraId(frontCameraId);
}
cameraManager.closeCamera(this);
}

@ -13,13 +13,6 @@ import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.WindowManager;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.github.florent37.camerafragment.configuration.Configuration;
import com.github.florent37.camerafragment.configuration.ConfigurationProvider;
import com.github.florent37.camerafragment.internal.manager.listener.CameraCloseListener;
@ -32,6 +25,13 @@ import com.github.florent37.camerafragment.internal.utils.CameraHelper;
import com.github.florent37.camerafragment.internal.utils.Size;
import com.github.florent37.camerafragment.listeners.CameraFragmentResultListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/*
* Created by memfis on 8/14/16.
*/
@ -50,6 +50,8 @@ public class Camera1Manager extends BaseCameraManager<Integer, SurfaceHolder.Cal
private CameraVideoListener videoListener;
private CameraPhotoListener photoListener;
private Integer futurFlashMode;
@Override
public void openCamera(final Integer cameraId,
final CameraOpenListener<Integer, SurfaceHolder.Callback> cameraOpenListener) {
@ -60,6 +62,10 @@ public class Camera1Manager extends BaseCameraManager<Integer, SurfaceHolder.Cal
try {
camera = Camera.open(cameraId);
prepareCameraOutputs();
if(futurFlashMode != null) {
setFlashMode(futurFlashMode);
futurFlashMode = null;
}
if (cameraOpenListener != null) {
uiHandler.post(new Runnable() {
@Override
@ -142,8 +148,12 @@ public class Camera1Manager extends BaseCameraManager<Integer, SurfaceHolder.Cal
}
@Override
public void setFlashMode(@Configuration.FlashMode int flashMode) {
public void setFlashMode(@Configuration.FlashMode final int flashMode) {
if (camera != null) {
setFlashMode(camera, camera.getParameters(), flashMode);
} else {
futurFlashMode = flashMode;
}
}
@Override

@ -74,16 +74,28 @@ public abstract class BaseAnncaFragment<CameraId> extends Fragment implements Ca
private AlertDialog settingsDialog;
private CameraFragmentControlsListener cameraFragmentControlsListener;
private CameraFragmentVideoRecordTextListener cameraFragmentVideoRecordTextListener;
final TimerTaskBase.Callback timerCallBack = new TimerTaskBase.Callback() {
@Override
public void setText(String text) {
if (cameraFragmentVideoRecordTextListener != null) {
cameraFragmentVideoRecordTextListener.setRecordDurationText(text);
}
}
@Override
public void setTextVisible(boolean visible) {
if (cameraFragmentVideoRecordTextListener != null) {
cameraFragmentVideoRecordTextListener.setRecordDurationTextVisible(visible);
}
}
};
private CameraFragmentStateListener cameraFragmentStateListener;
@Flash.FlashMode
private int currentFlashMode = Flash.FLASH_AUTO;
@Camera.CameraType
private int currentCameraType = Camera.CAMERA_TYPE_REAR;
@MediaAction.MediaActionState
private int currentMediaActionState = MediaAction.ACTION_PHOTO;
@Record.RecordState
private int currentRecordState = Record.TAKE_PHOTO_STATE;
private String mediaFilePath;
@ -157,15 +169,7 @@ public abstract class BaseAnncaFragment<CameraId> extends Fragment implements Ca
this.configurationProvider = new ConfigurationProviderImpl();
this.configurationProvider.setupWithAnnaConfiguration(configuration);
//onProcessBundle
currentMediaActionState = configurationProvider.getMediaAction() == Configuration.MEDIA_ACTION_VIDEO ?
MediaAction.ACTION_VIDEO : MediaAction.ACTION_PHOTO;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
previewContainer = (AspectFrameLayout) view.findViewById(R.id.previewContainer);
this.sensorManager = (SensorManager) getContext().getSystemService(Activity.SENSOR_SERVICE);
final CameraView cameraView = new CameraView() {
@ -210,7 +214,7 @@ public abstract class BaseAnncaFragment<CameraId> extends Fragment implements Ca
@Override
public void onVideoRecordStop(@Nullable CameraFragmentResultListener callback) {
BaseAnncaFragment.this.onStopVideoRecord(callback);
//BaseAnncaFragment.this.onStopVideoRecord(callback);
}
@Override
@ -220,13 +224,22 @@ public abstract class BaseAnncaFragment<CameraId> extends Fragment implements Ca
};
if (CameraHelper.hasCamera2(getContext())) {
cameraController = new Camera2Controller(getActivity(), cameraView, configurationProvider);
cameraController = new Camera2Controller(getContext(), cameraView, configurationProvider);
} else {
cameraController = new Camera1Controller(getActivity(), cameraView, configurationProvider);
cameraController = new Camera1Controller(getContext(), cameraView, configurationProvider);
}
cameraController.onCreate(savedInstanceState);
sensorManager = (SensorManager) getContext().getSystemService(Activity.SENSOR_SERVICE);
//onProcessBundle
currentMediaActionState = configurationProvider.getMediaAction() == Configuration.MEDIA_ACTION_VIDEO ?
MediaAction.ACTION_VIDEO : MediaAction.ACTION_PHOTO;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
previewContainer = (AspectFrameLayout) view.findViewById(R.id.previewContainer);
final int defaultOrientation = Utils.getDeviceDefaultOrientation(getContext());
switch (defaultOrientation) {
case android.content.res.Configuration.ORIENTATION_LANDSCAPE:
@ -254,6 +267,16 @@ public abstract class BaseAnncaFragment<CameraId> extends Fragment implements Ca
setMaxVideoFileSize(configurationProvider.getVideoFileSize());
}
setCameraTypeFrontBack(configurationProvider.getCameraFace());
notifyListeners();
}
public void notifyListeners() {
onFlashModeChanged();
onActionPhotoVideoChanged();
onCameraTypeFrontBackChanged();
}
@Override
@ -313,26 +336,10 @@ public abstract class BaseAnncaFragment<CameraId> extends Fragment implements Ca
}
protected void setMaxVideoDuration(int maxVideoDurationInMillis) {
final TimerTaskBase.Callback callback = new TimerTaskBase.Callback() {
@Override
public void setText(String text) {
if (cameraFragmentVideoRecordTextListener != null) {
cameraFragmentVideoRecordTextListener.setRecordDurationText(text);
}
}
@Override
public void setTextVisible(boolean visible) {
if (cameraFragmentVideoRecordTextListener != null) {
cameraFragmentVideoRecordTextListener.setRecordDurationTextVisible(visible);
}
}
};
if (maxVideoDurationInMillis > 0) {
this.countDownTimer = new CountdownTask(callback, maxVideoDurationInMillis);
this.countDownTimer = new CountdownTask(timerCallBack, maxVideoDurationInMillis);
} else {
this.countDownTimer = new TimerTask(callback);
this.countDownTimer = new TimerTask(timerCallBack);
}
}
@ -392,7 +399,7 @@ public abstract class BaseAnncaFragment<CameraId> extends Fragment implements Ca
}
@Override
public void switchCameraType() {
public void switchCameraTypeFrontBack() {
if (cameraFragmentControlsListener != null) {
cameraFragmentControlsListener.lockControls();
cameraFragmentControlsListener.allowRecord(false);
@ -403,20 +410,44 @@ public abstract class BaseAnncaFragment<CameraId> extends Fragment implements Ca
case Camera.CAMERA_TYPE_FRONT:
currentCameraType = Camera.CAMERA_TYPE_REAR;
cameraFace = Configuration.CAMERA_FACE_REAR;
if (cameraFragmentStateListener != null) {
cameraFragmentStateListener.onCurrentCameraBack();
}
break;
case Camera.CAMERA_TYPE_REAR:
currentCameraType = Camera.CAMERA_TYPE_FRONT;
cameraFace = Configuration.CAMERA_FACE_FRONT;
if (cameraFragmentStateListener != null) {
cameraFragmentStateListener.onCurrentCameraFront();
}
break;
}
onCameraTypeFrontBackChanged();
this.cameraController.switchCamera(cameraFace);
}
protected void setCameraTypeFrontBack(@Configuration.CameraFace int cameraFace){
switch (cameraFace) {
case Configuration.CAMERA_FACE_FRONT:
currentCameraType = Camera.CAMERA_TYPE_FRONT;
cameraFace = Configuration.CAMERA_FACE_FRONT;
break;
case Configuration.CAMERA_FACE_REAR:
currentCameraType = Camera.CAMERA_TYPE_REAR;
cameraFace = Configuration.CAMERA_FACE_REAR;
break;
}
onCameraTypeFrontBackChanged();
this.cameraController.switchCamera(cameraFace);
}
protected void onCameraTypeFrontBackChanged() {
if (cameraFragmentStateListener != null) {
switch (currentCameraType) {
case Camera.CAMERA_TYPE_REAR:
cameraFragmentStateListener.onCurrentCameraBack();
break;
case Camera.CAMERA_TYPE_FRONT:
cameraFragmentStateListener.onCurrentCameraFront();
break;
}
}
}
@Override
@ -424,18 +455,26 @@ public abstract class BaseAnncaFragment<CameraId> extends Fragment implements Ca
switch (currentMediaActionState) {
case MediaAction.ACTION_PHOTO:
currentMediaActionState = MediaAction.ACTION_VIDEO;
if (cameraFragmentStateListener != null) {
cameraFragmentStateListener.onCameraSetupForVideo();
}
break;
case MediaAction.ACTION_VIDEO:
currentMediaActionState = MediaAction.ACTION_PHOTO;
break;
}
onActionPhotoVideoChanged();
}
protected void onActionPhotoVideoChanged() {
if (cameraFragmentStateListener != null) {
switch (currentMediaActionState) {
case MediaAction.ACTION_VIDEO:
cameraFragmentStateListener.onCameraSetupForVideo();
break;
case MediaAction.ACTION_PHOTO:
cameraFragmentStateListener.onCameraSetupForPhoto();
}
break;
}
}
}
@Override
public void toggleFlashMode() {
@ -484,6 +523,7 @@ public abstract class BaseAnncaFragment<CameraId> extends Fragment implements Ca
this.currentRecordState = recordState;
}
//@Override
//public void onActivityResult(int requestCode, int resultCode, Intent data) {
// if (resultCode == Activity.RESULT_OK) {
@ -642,6 +682,10 @@ public abstract class BaseAnncaFragment<CameraId> extends Fragment implements Ca
Log.e("FileObserver", "setMediaFilePath: ", e);
}
}
if (countDownTimer == null) {
this.countDownTimer = new TimerTask(timerCallBack);
}
countDownTimer.start();
if (cameraFragmentStateListener != null) {
@ -660,7 +704,10 @@ public abstract class BaseAnncaFragment<CameraId> extends Fragment implements Ca
if (fileObserver != null)
fileObserver.stopWatching();
if(countDownTimer != null) {
countDownTimer.stop();
}
final int mediaAction = configurationProvider.getMediaAction();
if (cameraFragmentControlsListener != null) {

Binary file not shown.

After

Width:  |  Height:  |  Size: 216 KiB

Loading…
Cancel
Save