今天我们来讲一下在Android平台上开发相机的问题!这里讲的是调用系统相机。
1、从xml文件中得到su易做图ceview对象
[html] su易做图ceView = (Su易做图ceView) findViewById(R.id.g3_server_preview);
su易做图ceView = (Su易做图ceView) findViewById(R.id.g3_server_preview);
2、之后声明一些回调方法,用于打开相机,设置相机的参数
[html] // Su易做图ceHodler 回调,处理打开相机,关闭相机以及照片尺寸的改变
Su易做图ceHolder.Callback su易做图ceCallback = new Su易做图ceHolder.Callback() {
@Override
public void su易做图ceCreated(Su易做图ceHolder holder) {
// 打开相机
camera = Camera.open();
try {
// 设置预览
camera.setPreviewDisplay(holder);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void su易做图ceChanged(Su易做图ceHolder holder, int format, int width,
int height) {
// 获得相机参数
Camera.Parameters parameters = camera.getParameters();
// SDK版本选择,兼容
if (Integer.parseInt(Build.VERSION.SDK) >= 8) {
setDisplayOrientation(camera, 90);
} else {
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
parameters.set("orientation", "portrait");
parameters.set("rotation", 90);
}
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
parameters.set("orientation", "landscape");
parameters.set("rotation", 90);
}
}
// 设置预览大小
parameters.setPreviewSize(width, height);
pic_Width = g3_setting.getInt(AppData.G3_PIC_WIDTH, 640);
pic_Height = g3_setting.getInt(AppData.G3_PIC_HEIGHT, 800);
Log.i("Travel", pic_Width + ":" + pic_Height);
// 设置照片大小
parameters.setPictureSize(pic_Width, pic_Height);
// 设置照片格式
// parameters.setPictureFormat(PixelFormat.RGB_565);
// 开始预览
camera.startPreview();
// 设置相机参数
camera.setParameters(parameters);
}
@Override
public void su易做图ceDestroyed(Su易做图ceHolder holder) {
// 停止预览
if (camera != null) {
camera.stopPreview();
// 释放相机资源
camera.release();
camera = null;
}
}
};
// Su易做图ceHodler 回调,处理打开相机,关闭相机以及照片尺寸的改变
Su易做图ceHolder.Callback su易做图ceCallback = new Su易做图ceHolder.Callback() {
@Override
public void su易做图ceCreated(Su易做图ceHolder holder) {
// 打开相机
camera = Camera.open();
try {
// 设置预览
camera.setPreviewDisplay(holder);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void su易做图ceChanged(Su易做图ceHolder holder, int format, int width,
int height) {
// 获得相机参数
Camera.Parameters parameters = camera.getParameters();
// SDK版本选择,兼容
if (Integer.parseInt(Build.VERSION.SDK) >= 8) {
setDisplayOrientation(camera, 90);
} else {
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
parameters.set("orientation", "portrait");
parameters.set("rotation", 90);
}
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
parameters.set("orientation", "landscape");
parameters.set("rotation", 90);
}
}
// 设置预览大小
parameters.setPreviewSize(width, height);
pic_Width = g3_setting.getInt(AppData.G3_PIC_WIDTH, 640);
pic_Height = g3_setting.getInt(AppData.G3_PIC_HEIGHT, 800);
Log.i("Travel", pic_Width + ":" + pic_Height);
// 设置照片大小
parameters.setPictureSize(pic_Width, pic_Height);
// 设置照片格式
// parameters.setPictureFormat(PixelFormat.RGB_565);
// 开始预览
camera.startPreview();
// 设置相机参数
camera.setParameters(parameters);
}
@Override
public void su易做图ceDestroyed(Su易做图ceHolder holder) {
// 停止预览
if (camera != null) {
camera.stopPreview();
// 释放相机资源
camera.release();
camera = null;
}
}
};[html] view plaincopyprint?protected void setDisplayOrientation(Camera camera, int angle) {
Method downPolymorphic;
try {
downPolymorphic = camera.getClass().getMethod(
"setDisplayOrientation", new Class[] { int.class });
if (downPolymorphic != null)
downPolymorphic.invoke(camera, new Object[] { angle });
} catch (Exception e1) {