嘿伙计们,我正在使用以下代码从我的应用程序访问相机。该应用程序能够访问相机我还添加了一个按钮,其 onclicklistener 添加了这行代码:-
camera.takePicture(mShutterCallback, mPictureCallbackRaw, mPictureCallbackJpeg);
现在我不知道会发生什么,但是应用程序卡住了,我必须强制关闭它,然后我什至无法访问本机相机应用程序。我认为它会在不释放 Camera 对象的情况下离开应用程序。加上图像永远不会被保存。
代码 :-
SurfaceHolder holder;
SurfaceView surface;
Camera camera;
Boolean isPreviewRunning, fromOnResume;
//Preview mpreview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
isPreviewRunning = false;
fromOnResume = false;
// requestWindowFeature(Window.FEATURE_NO_TITLE);
surface = (SurfaceView)findViewById(R.id.surface);
holder = surface.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
Button btn = (Button)findViewById(R.id.click);
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
camera.takePicture(mShutterCallback, mPictureCallbackRaw, mPictureCallbackJpeg);
//onCreate(null);
}
});
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
/* if (isPreviewRunning) {
camera.stopPreview();
} */
Camera.Parameters parameters = camera.getParameters();
List<Size> sizes = parameters.getSupportedPreviewSizes();
Size size = sizes.get(0);
parameters.setPreviewSize(size.width, size.height);
camera.setParameters(parameters);
camera.startPreview();
isPreviewRunning=true;
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
camera = Camera.open();
try {
camera.setPreviewDisplay(holder);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
camera.stopPreview();
camera.release();
camera=null;
}
Camera.PictureCallback mPictureCallbackRaw = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera c) {
Log.e(getClass().getSimpleName(), "PICTURE CALLBACK RAW: " + data);
camera.startPreview();
}
};
Camera.PictureCallback mPictureCallbackJpeg= new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera c) {
Log.e(getClass().getSimpleName(), "PICTURE CALLBACK JPEG: data.length = " + data);
camera.startPreview();
}
};
Camera.ShutterCallback mShutterCallback = new Camera.ShutterCallback() {
public void onShutter() {
Log.e(getClass().getSimpleName(), "SHUTTER CALLBACK");
}
};
/*
* protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
}
protected void onResume()
{
Log.e(getClass().getSimpleName(), "onResume");
camera.open();
fromOnResume=true;
super.onResume();
}
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
camera.release();
super.onPause();
}
protected void onStop()
{
Log.e(getClass().getSimpleName(), "onStop");
super.onStop();
}*/