我正在开发自定义相机应用程序。我设置了android清单文件屏幕模式的肖像。它适用于HTC和其他人,但在三星手机上,会出现方向问题。如何为所有设备开发定制相机..
相机模块
public class Cameramodule extends Activity {
Camerapriview preview;
FrameLayout cameraframe;
Button b,camswitch;
ImageButton camera_settings,Take_picture;
public static int second_select=0;
Animation animation;
int orienation;
SensorManager sensorManager;
ImageButton flash,camera_switching;
RelativeLayout second_overlay;
int flag=1;
public static int cam_switch_flag=1;
Button one,two,five,eight,ten,infinity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cameramodule);
one=(Button) findViewById(R.id.one);
two=(Button) findViewById(R.id.two);
five=(Button) findViewById(R.id.five);
eight=(Button) findViewById(R.id.eight);
ten=(Button) findViewById(R.id.ten);
infinity=(Button) findViewById(R.id.infinity);
one.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
second_select=1;
second_overlay.setVisibility(View.GONE);
}
});
two.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
second_select=2;
second_overlay.setVisibility(View.GONE);
}
});
five.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
second_select=5;
second_overlay.setVisibility(View.GONE);
}
});
eight.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
second_select=8;
second_overlay.setVisibility(View.GONE);
}
});
ten.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
second_select=10;
second_overlay.setVisibility(View.GONE);
}
});
infinity.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
second_overlay.setVisibility(View.GONE);
second_select=0;//zero means infinity;
}
});
preview=new Camerapriview(this,Cameramodule.this);
second_overlay=(RelativeLayout) findViewById(R.id.second_overlay);
second_overlay.setVisibility(View.GONE);
//sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
cameraframe=(FrameLayout) findViewById(R.id.mframe);
camera_settings=(ImageButton) findViewById(R.id.camera_settings);
Take_picture=(ImageButton) findViewById(R.id.taje_picture);
camera_switching=(ImageButton) findViewById(R.id.camera_switch);
orienation=getResources().getConfiguration().orientation;
cameraframe.addView(preview);
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth(); // deprecated
int height = display.getHeight();
//create dialog box..
/*
*
* Camera record setting options
*/
Take_picture.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
preview.camera.takePicture(null, new Rwacallbackhandler(getApplicationContext()), new Photohandler(getApplicationContext()));
}
});
/*
* Switch the front and back camera...
*/
camera_switching.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
preview.callswitch(Cameramodule.this);
if(cam_switch_flag==1)
{
cam_switch_flag=2;
}
else if (cam_switch_flag==2) {
cam_switch_flag=1;
}
}
});
//create flash option
camera_settings.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(flag==1)
{
second_overlay.setVisibility(View.VISIBLE);
startAnimationPopOut();
flag=2;
}
else if(flag==2)
{
second_overlay.setVisibility(View.GONE);
//startAnimationPopOut();
flag=1;
}
}
});
}
private void startAnimationPopOut() {
animation = AnimationUtils.loadAnimation(this,R.anim.layout_animation);
animation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
}
});
second_overlay.clearAnimation();
second_overlay.startAnimation(animation);
}
}
Camerapriview
public class Camerapriview extends SurfaceView implements SurfaceHolder.Callback {
public static Activity mactivity;
SurfaceHolder holder;
public Camera camera;
List<String> coloreffect;
public Context mcoContext;
int currentCameraId=Camera.CameraInfo.CAMERA_FACING_BACK;
int cameraId=Camera.CameraInfo.CAMERA_FACING_BACK;
public Camerapriview(Context context, Cameramodule cameramodule) {
super(context);
// TODO Auto-generated constructor stub
mcoContext=context;
mactivity=cameramodule;
holder=getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
//setCameraDisplayOrientation(mactivity, cameraId, camera);
camera.setDisplayOrientation(90);
camera.startPreview();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
camera=Camera.open(cameraId);
Camera.Parameters param=camera.getParameters();
param.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
camera.setParameters(param);
holder.setFixedSize(mactivity.getWindow().getWindowManager()
.getDefaultDisplay().getWidth(),mactivity.getWindow().getWindowManager()
.getDefaultDisplay().getHeight());
try
{
camera.setPreviewDisplay(this.holder);
}
catch (Exception e) {
// TODO: handle exception
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
//preview.this.getHolder().removeCallback(this);
camera.setPreviewCallback(null);
camera.stopPreview();
camera.release();
holder = null;
camera = null;
}
//Set the orientation of the camera.....
public void setCameraDisplayOrientation(Activity activity,
int cameraId, android.hardware.Camera camera)
{
camera.stopPreview();
android.hardware.Camera.CameraInfo info =
new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId, info);
int rotation = activity.getWindowManager().getDefaultDisplay()
.getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0: degrees = 0;
Toast.makeText(mcoContext,"degree zero", 1000).show();
break;
case Surface.ROTATION_90: degrees = 90;
Toast.makeText(mcoContext,"degree 90", 1000).show();
break;
case Surface.ROTATION_180: degrees = 180;
Toast.makeText(mcoContext,"degree 180", 1000).show();
break;
case Surface.ROTATION_270: degrees = 270;
Toast.makeText(mcoContext,"degree 270", 1000).show();
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);
}
public void callswitch(Cameramodule cameramodule) {
// TODO Auto-generated method stub
try
{
Activity myactivity=cameramodule;
camera.release();
//swap the id of the camera to be used
if(currentCameraId == Camera.CameraInfo.CAMERA_FACING_BACK){
currentCameraId = Camera.CameraInfo.CAMERA_FACING_FRONT;
}
else {
currentCameraId = Camera.CameraInfo.CAMERA_FACING_BACK;
}
camera = Camera.open(currentCameraId);
//Code snippet for this method from somewhere on android developers, i forget where
setCameraDisplayOrientation(myactivity, currentCameraId, camera);
try {
//this step is critical or preview on new camera will no know where to render to
camera.setPreviewDisplay(holder);
} catch (IOException e) {
e.printStackTrace();
}
camera.startPreview();
}
catch (Exception e) {
// TODO: handle exception
Toast.makeText(mcoContext, e.getMessage().toString(), 1000).show();
}
}
//set the flash modes...
public void setflash() {
// TODO Auto-generated method stub
Toast.makeText(mcoContext, "Inside set", 1000).show();
Log.i("flash", "My flash");
Camera.Parameters param=camera.getParameters();
List<String> flash=param.getSupportedFlashModes();
if(camera.getParameters().getFlashMode()==Camera.Parameters.FLASH_MODE_AUTO)
{
if(flash.contains(Camera.Parameters.FLASH_MODE_ON))
param.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
}
else
{
if(flash.contains(Camera.Parameters.FLASH_MODE_ON))
param.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
}
camera.setParameters(param);
}
}
我有方向问题。我想设置屏幕始终纵向模式。
答案 0 :(得分:0)
看起来三星Android开发者网站上有一些帮助,他们概述了Common Issues in Android。你有很多代码供我查看,但我看到了这一部分:
问题:预览显示方向错误。
并认为此页面对您有所帮助。
答案 1 :(得分:0)
您是获得异常,还是忽略了您的setDisplayOrientation()调用,还是其他什么? 尝试确保您的宽度/高度支持纵向。我曾经有过设置肖像模式的类似问题,在你的surfaceChanged中,试试:
final Camera.Parameters parameters = this.mCamera.getParameters();
List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();
Camera.Size cs = previewSizes.get(0);
mCamera.setDisplayOrientation(90);
parameters.setPreviewSize(cs.width,cs.height);
this.mCamera.setParameters(parameters);
this.mCamera.startPreview();