9
0
mirror of https://gitee.com/shuto/customCamera.git synced 2026-05-02 00:07:24 +08:00

Correctifs de codes (commentaires, deplacement). Deplacement de setDisplayOrientation dans ManagerCamera et appel dans initCameraResource. Modification du hook pour la langue par defaut.

This commit is contained in:
Thomas BOY
2015-02-09 11:16:58 +01:00
parent 91349dc4ed
commit e4fd766f20
4 changed files with 69 additions and 83 deletions
@@ -75,16 +75,20 @@ public class CameraActivity extends Activity {
/**
* To get camera resource or stop this activity.
*
* @param position The position of the camera.
*
* @return boolean
*/
protected boolean initCameraResource() {
int defaultCamera;
if (this.getIntent().getIntExtra("defaultCamera", CameraActivity.CAMERA_BACK) == CameraActivity.CAMERA_FRONT) {
defaultCamera = ManagerCamera.determinePositionFrontCamera();
} else {
defaultCamera = ManagerCamera.determinePositionBackCamera();
protected boolean initCameraResource(Integer position) {
if (position == null) {
if (this.getIntent().getIntExtra("defaultCamera", CameraActivity.CAMERA_BACK) == CameraActivity.CAMERA_FRONT) {
position = ManagerCamera.determinePositionFrontCamera();
} else {
position = ManagerCamera.determinePositionBackCamera();
}
}
customCamera = ManagerCamera.getCameraInstance(defaultCamera);
customCamera = ManagerCamera.getCameraInstance(position);
ManagerCamera.setCameraDisplayOrientation(this);
if (customCamera == null) {
this.setResult(2,
@@ -208,33 +212,12 @@ public class CameraActivity extends Activity {
super.onStart();
// Init camera resource.
if (!initCameraResource()) {
if (!initCameraResource(null)) {
return;
}
stateFlash = this.getIntent().getIntExtra("defaultFlash", CameraActivity.FLASH_DISABLE);
updateStateFlash(stateFlash);
int orientation = 0;
switch (getCustomRotation()) {
case 0:
orientation = CameraActivity.DEGREE_90;
break;
case 1:
orientation = CameraActivity.DEGREE_0;
break;
case 2:
orientation = CameraActivity.DEGREE_270;
break;
case 3:
orientation = CameraActivity.DEGREE_180;
break;
default:
break;
}
customCamera.setDisplayOrientation(orientation);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
@@ -1028,8 +1011,7 @@ public class CameraActivity extends Activity {
*/
public void switchCamera(View view) {
int oppositeCamera = ManagerCamera.determineOppositeCamera();
customCamera = ManagerCamera.getCameraInstance(oppositeCamera);
setCameraDisplayOrientation(CameraActivity.this, oppositeCamera, customCamera);
initCameraResource(oppositeCamera);
FrameLayout cameraPreview = (FrameLayout) findViewById(R.id.camera_preview);
cameraPreview.removeAllViews();
CameraPreview myPreview = new CameraPreview(this, customCamera);
@@ -1037,34 +1019,4 @@ public class CameraActivity extends Activity {
// To re-display the flash.
updateStateFlash(stateFlash);
}
/**
* To stabilize the orientation of the camera preview.
* @param activity
* @param cameraId
* @param camera
*/
public static void setCameraDisplayOrientation(Activity activity,
int cameraId, android.hardware.Camera camera) {
Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
Camera.getCameraInfo(cameraId, info);
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0: degrees = 0; break;
case Surface.ROTATION_90: degrees = 90; break;
case Surface.ROTATION_180: degrees = 180; break;
case Surface.ROTATION_270: degrees = 270; break;
default : break;
}
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
}
camera.setDisplayOrientation(result);
}
}
@@ -1,8 +1,10 @@
package org.geneanet.customcamera;
import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.util.Log;
import android.view.Surface;
/**
* Manage camera resource.
@@ -55,23 +57,36 @@ public class ManagerCamera {
}
/**
* Return a function to determine the front camera in use.
* Return the value of the position of the front camera.
*
* @return function
* @return int
*/
public static int determinePositionFrontCamera() {
return determineCamera(Camera.CameraInfo.CAMERA_FACING_FRONT);
}
/**
* Return a function to determine the back camera in use.
* Return the value of the position of the back camera.
*
* @return function
* @return int
*/
public static int determinePositionBackCamera() {
return determineCamera(Camera.CameraInfo.CAMERA_FACING_BACK);
}
/**
* Determine the opposite camera of which currently in use.
*
* @return int.
*/
public static int determineOppositeCamera() {
if (getCurrentFacingCamera() == Camera.CameraInfo.CAMERA_FACING_BACK) {
return determinePositionFrontCamera();
} else {
return determinePositionBackCamera();
}
}
/**
* Determine the cameraId of the camera currently in use.
*
@@ -98,25 +113,15 @@ public class ManagerCamera {
}
/**
* Get the currentCamera.
* Get the current camera.
*
* @return the value of the variable.
* @return the value of currentCameraPosition.
*/
private static int getCurrentFacingCamera() {
return currentCameraPosition;
}
/**
* Determine the opposite camera of which currently in use.
*
* @return function.
*/
public static int determineOppositeCamera() {
if (getCurrentFacingCamera() == Camera.CameraInfo.CAMERA_FACING_BACK) {
return determinePositionFrontCamera();
} else {
return determinePositionBackCamera();
}
CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(currentCameraPosition, info);
return info.facing;
}
/**
@@ -142,4 +147,33 @@ public class ManagerCamera {
}
return false;
}
/**
* To stabilize the orientation of the camera preview.
* @param activity
* @param cameraId
* @param camera
*/
public static void setCameraDisplayOrientation(Activity activity) {
CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(ManagerCamera.currentCameraPosition, info);
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0: degrees = 0; break;
case Surface.ROTATION_90: degrees = 90; break;
case Surface.ROTATION_180: degrees = 180; break;
case Surface.ROTATION_270: degrees = 270; break;
default : break;
}
int result;
if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
}
ManagerCamera.mCamera.setDisplayOrientation(result);
}
}