CameraFragment
CameraFragment copied to clipboard
Flash doesn't work after first time
Steps to reproduce it:
- Toggle flash till is in ON state.
- Take a picture (flash is fine), go to the preview
- Come back to the camera screen
- Take another picture (flash won't work).
Basically, it looks like the flash state is not preserved. Toggling programmatically using 3 times "toggleFlashMode()" fixes it (really hacky but it's the only public method of the api that interacts with the flash).
You have to modify Camera1Manager.java
- startPreview() ... setFlashMode(camera, parameters, configurationProvider.getFlashMode()); ...
private void startPreview(SurfaceHolder surfaceHolder) {
try {
final Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
Camera.getCameraInfo(currentCameraId, cameraInfo);
int cameraRotationOffset = cameraInfo.orientation;
final Camera.Parameters parameters = camera.getParameters();
setAutoFocus(camera, parameters);
setFlashMode(camera, parameters, configurationProvider.getFlashMode());
if (configurationProvider.getMediaAction() == Configuration.MEDIA_ACTION_PHOTO
|| configurationProvider.getMediaAction() == Configuration.MEDIA_ACTION_UNSPECIFIED)
turnPhotoCameraFeaturesOn(camera, parameters);
else if (configurationProvider.getMediaAction() == Configuration.MEDIA_ACTION_VIDEO)
turnVideoCameraFeaturesOn(camera, parameters);
final int rotation = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0:
degrees = 0;
break; // Natural orientation
case Surface.ROTATION_90:
degrees = 90;
break; // Landscape left
case Surface.ROTATION_180:
degrees = 180;
break;// Upside down
case Surface.ROTATION_270:
degrees = 270;
break;// Landscape right
}
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
displayRotation = (cameraRotationOffset + degrees) % 360;
displayRotation = (360 - displayRotation) % 360; // compensate
} else {
displayRotation = (cameraRotationOffset - degrees + 360) % 360;
}
this.camera.setDisplayOrientation(displayRotation);
if (Build.VERSION.SDK_INT > 13
&& (configurationProvider.getMediaAction() == Configuration.MEDIA_ACTION_VIDEO
|| configurationProvider.getMediaAction() == Configuration.MEDIA_ACTION_UNSPECIFIED)) {
// parameters.setRecordingHint(true);
}
if (Build.VERSION.SDK_INT > 14
&& parameters.isVideoStabilizationSupported()
&& (configurationProvider.getMediaAction() == Configuration.MEDIA_ACTION_VIDEO
|| configurationProvider.getMediaAction() == Configuration.MEDIA_ACTION_UNSPECIFIED)) {
parameters.setVideoStabilization(true);
}
parameters.setPreviewSize(previewSize.getWidth(), previewSize.getHeight());
parameters.setPictureSize(photoSize.getWidth(), photoSize.getHeight());
camera.setParameters(parameters);
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
} catch (IOException error) {
Log.d(TAG, "Error setting camera preview: " + error.getMessage());
} catch (Exception ignore) {
Log.d(TAG, "Error starting camera preview: " + ignore.getMessage());
}
}