mirror of
https://gitee.com/shuto/customCamera.git
synced 2026-05-02 00:07:24 +08:00
Mise en place de la camera qui change en fonction de l'orientation + une image par dessus.
This commit is contained in:
@@ -1,39 +1,62 @@
|
||||
package org.geneanet.testcustomcamera;
|
||||
|
||||
import org.geneanet.testcustomcamera.utils.CameraPreview;
|
||||
import org.geneanet.testcustomcamera.utils.CustomCamera;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.res.Configuration;
|
||||
import android.graphics.Matrix;
|
||||
import android.graphics.Point;
|
||||
import android.hardware.Camera;
|
||||
import android.os.Bundle;
|
||||
import android.view.Display;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.Window;
|
||||
import android.view.WindowManager;
|
||||
import android.widget.FrameLayout;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.ImageView.ScaleType;
|
||||
|
||||
public class CameraView extends Activity {
|
||||
|
||||
private Camera mCamera;
|
||||
private CameraPreview mPreview;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
//Remove title bar
|
||||
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||
//Remove notification bar
|
||||
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
||||
|
||||
setContentView(R.layout.activity_camera_view);
|
||||
|
||||
Camera cameraTest = CustomCamera.getCameraInstance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
// Inflate the menu; this adds items to the action bar if it is present.
|
||||
getMenuInflater().inflate(R.menu.camera_view, menu);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
// Handle action bar item clicks here. The action bar will
|
||||
// automatically handle clicks on the Home/Up button, so long
|
||||
// as you specify a parent activity in AndroidManifest.xml.
|
||||
int id = item.getItemId();
|
||||
if (id == R.id.action_settings) {
|
||||
return true;
|
||||
// Récupère les infos sur le device.
|
||||
Display display = getWindowManager().getDefaultDisplay();
|
||||
|
||||
// récupère l'objet gérant la camera puis l'oriente en fonction de l'écran.
|
||||
mCamera = CustomCamera.getCameraInstance();
|
||||
switch (display.getRotation()) {
|
||||
case CustomCamera.LANDSCAPE:
|
||||
mCamera.setDisplayOrientation(0);
|
||||
break;
|
||||
case CustomCamera.PORTRAIT:
|
||||
mCamera.setDisplayOrientation(90);
|
||||
break;
|
||||
case CustomCamera.LANDSCAPE_INVERSED:
|
||||
mCamera.setDisplayOrientation(180);
|
||||
break;
|
||||
case CustomCamera.PORTRAIT_INVERSED:
|
||||
mCamera.setDisplayOrientation(270);
|
||||
break;
|
||||
}
|
||||
return super.onOptionsItemSelected(item);
|
||||
|
||||
// On assigne le rendu à la vue.
|
||||
mPreview = new CameraPreview(this, mCamera);
|
||||
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
|
||||
preview.addView(mPreview);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,8 +3,22 @@ package org.geneanet.testcustomcamera.utils;
|
||||
import android.hardware.Camera;
|
||||
|
||||
public class CustomCamera {
|
||||
|
||||
protected static Camera mCamera = null;
|
||||
|
||||
// constantes sur les orientations de téléphones.
|
||||
public final static int PORTRAIT = 0;
|
||||
public final static int LANDSCAPE = 1;
|
||||
public final static int PORTRAIT_INVERSED = 2;
|
||||
public final static int LANDSCAPE_INVERSED = 3;
|
||||
|
||||
/** A safe way to get an instance of the Camera object. */
|
||||
public static Camera getCameraInstance(){
|
||||
// si on a déjà une camera récupérée dans l'application, on la retourne directement.
|
||||
if (CustomCamera.mCamera != null) {
|
||||
return mCamera;
|
||||
}
|
||||
// si non, on va chercher la camera de derrière.
|
||||
Camera c = null;
|
||||
try {
|
||||
c = Camera.open(0); // attempt to get a Camera instance
|
||||
@@ -14,6 +28,9 @@ public class CustomCamera {
|
||||
// Camera is not available (in use or does not exist)
|
||||
System.err.println("rt"+e);
|
||||
}
|
||||
|
||||
CustomCamera.mCamera = c;
|
||||
|
||||
return c; // returns null if camera is unavailable
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user