我正在创建一个应用程序,该应用程序具有自己使用相机预览构建的相机。它在我的Android 4.1
设备上运行良好(相机打开并拍照)。但是当我在我的 Android 2.3 设备上尝试时,应用程序崩溃了,framelayout.addView(camera_view)
因为当我评论这一行时,应用程序没有崩溃。这是我的代码(这些措施只是为了适应设备屏幕):
private void setUpCamera() {
camera_view = new CameraSurfaceView(getApplicationContext());
frame_layout = (FrameLayout) findViewById(R.id.image_frame);
// v it's a square that I draw
v.getLayoutParams().width = (int) ((720/4) + (5*screenDensity));
v.getLayoutParams().height = (int) ((720/4) + (5*screenDensity));
// img it's the imageview where I display the photo
img.getLayoutParams().width = (int) (720/4);
img.getLayoutParams().height = (int) (720/4);
frame_layout.getLayoutParams().width = (int) (720/4);
frame_layout.getLayoutParams().height = (int) (960/4);
frame_layout.addView(camera_view);
}
这是 camera_view 类:
public class CameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback{
private SurfaceHolder m_holder;
public Camera camera = null;
public CameraSurfaceView(Context context) {
super(context);
m_holder = getHolder();
m_holder.addCallback(this);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Camera.Parameters params = camera.getParameters();
if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
camera.setDisplayOrientation(90);
} else {
camera.setDisplayOrientation(0);
}
params.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
params.setSceneMode(Camera.Parameters.SCENE_MODE_AUTO);
params.setWhiteBalance(Camera.Parameters.WHITE_BALANCE_AUTO);
params.setExposureCompensation(0);
params.setJpegQuality(100);
List<Size> sizes = params.getSupportedPictureSizes();
Camera.Size size = sizes.get(0);
for(int i=0;i<sizes.size();i++)
{
if(sizes.get(i).width > size.width)
size = sizes.get(i);
}
params.setPreviewSize(size.width, size.height);
params.setPictureSize(size.width, size.height);
camera.setParameters(params);
camera.startPreview();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
try {
camera.setPreviewDisplay(m_holder);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
camera = null;
}
public void capture(Camera.PictureCallback jpeghandler){
camera.takePicture(null, null, jpeghandler);
}
}