Skip to content

Commit

Permalink
fixed FFC rotation issue
Browse files Browse the repository at this point in the history
  • Loading branch information
shliama committed Oct 7, 2014
1 parent d160d60 commit fa70432
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,35 @@
import android.view.OrientationEventListener;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yalantis.cameramodule.CameraConst;
import com.yalantis.cameramodule.R;
import com.yalantis.cameramodule.control.CameraPreview;
import com.yalantis.cameramodule.interfaces.*;
import com.yalantis.cameramodule.model.*;
import timber.log.Timber;
import com.yalantis.cameramodule.interfaces.CameraParamsChangedListener;
import com.yalantis.cameramodule.interfaces.FocusCallback;
import com.yalantis.cameramodule.interfaces.KeyEventsListener;
import com.yalantis.cameramodule.interfaces.PhotoSavedListener;
import com.yalantis.cameramodule.interfaces.PhotoTakenCallback;
import com.yalantis.cameramodule.interfaces.RawPhotoTakenCallback;
import com.yalantis.cameramodule.model.FlashMode;
import com.yalantis.cameramodule.model.FocusMode;
import com.yalantis.cameramodule.model.HDRMode;
import com.yalantis.cameramodule.model.Quality;
import com.yalantis.cameramodule.model.Ratio;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import timber.log.Timber;

public class CameraFragment extends BaseFragment implements PhotoSavedListener, KeyEventsListener, CameraParamsChangedListener, FocusCallback {

public static final String QUALITY = "quality";
Expand All @@ -66,7 +81,6 @@ public class CameraFragment extends BaseFragment implements PhotoSavedListener,
private int mScreenHeight;
private int mNavigationBarHeight;
private int mStatusBarHeight;
private int currOrientation;
private List<Integer> zoomRatios;
private int zoomIndex;
private int minZoomIndex;
Expand All @@ -87,6 +101,9 @@ public class CameraFragment extends BaseFragment implements PhotoSavedListener,
private HDRMode hdrMode;
private boolean supportedHDR = false;

private int cameraId;
private int outputOrientation;

public static CameraFragment newInstance(int layoutId, PhotoTakenCallback callback, Bundle params) {
CameraFragment fragment = new CameraFragment();
fragment.layoutId = layoutId;
Expand Down Expand Up @@ -255,9 +272,8 @@ private int getCameraId(boolean useFrontCamera) {
break;
}
}
// facingCamera = info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT;
}
// cameraId = result;
cameraId = result;
return result;
}

Expand Down Expand Up @@ -320,7 +336,7 @@ public void setParamsChangedListener(CameraParamsChangedListener paramsChangedLi
@Override
public void onPictureTaken(byte[] data, Camera camera) {
if (callback != null) {
callback.photoTaken(data.clone(), currOrientation);
callback.photoTaken(data.clone(), outputOrientation);
}
camera.startPreview();
cameraPreview.onPictureTaken();
Expand Down Expand Up @@ -572,12 +588,9 @@ private void setPreviewSize(Camera.Parameters parameters, Ratio ratio) {
}

/**
* @param width
* Screen width
* @param height
* Screen height
* @param ratio
* Required ratio
* @param width Screen width
* @param height Screen height
* @param ratio Required ratio
*/
private void setPreviewContainerSize(int width, int height, Ratio ratio) {
height = (width / ratio.h) * ratio.w;
Expand Down Expand Up @@ -666,20 +679,38 @@ private void initOrientationListener() {

@Override
public void onOrientationChanged(int orientation) {
if (orientation == ORIENTATION_UNKNOWN) {
return;
}
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(0, info);
orientation = (orientation + 45) / 90 * 90;
int rotation = (info.orientation + orientation) % 360;
synchronized (parameters) {
parameters.setRotation(rotation);
camera.setParameters(parameters);
currOrientation = rotation;
if (camera != null && orientation != ORIENTATION_UNKNOWN) {
int newOutputOrientation = getCameraPictureRotation(orientation);

if (newOutputOrientation != outputOrientation) {
outputOrientation = newOutputOrientation;

Camera.Parameters params = camera.getParameters();
params.setRotation(outputOrientation);
try {
camera.setParameters(params);
} catch (Exception e) {
Timber.e(e, "Exception updating camera parameters in orientation change");
}
}
}
}
};
}

private int getCameraPictureRotation(int orientation) {
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(cameraId, info);
int rotation;

orientation = (orientation + 45) / 90 * 90;

if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
rotation = (info.orientation - orientation + 360) % 360;
} else { // back-facing camera
rotation = (info.orientation + orientation) % 360;
}

return (rotation);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void openCamera(View view) {
intent.putExtra(CameraActivity.PATH, Const.FOLDERS.PATH);
intent.putExtra(CameraActivity.OPEN_PHOTO_PREVIEW, true);
intent.putExtra(CameraActivity.LAYOUT_ID, R.layout.fragment_camera_custom);
intent.putExtra(CameraActivity.USE_FRONT_CAMERA, false);
intent.putExtra(CameraActivity.USE_FRONT_CAMERA, true);
startActivity(intent);
}

Expand Down

0 comments on commit fa70432

Please sign in to comment.