在我的相机应用程序中,我有两个表面视图,一个用于预览相机,一个用于绘制名为 tFrame 的形状(其类型为扩展表面视图的目标框架)。一开始它可以正常工作,但是在应用程序不可见时 onResume 之后,tFrame 表面视图不可见,我只查看预览相机的视图。
这是我的代码
public class CameraActivity extends Activity {
private Camera mCamera;
private CameraPreview mPreview;
public Target_Frame tFrame;
private ColorView cViewActual;
private ColorView bestMatchCView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.d("Function", "onCreate iniciado");
setContentView(R.layout.camera_layout);
tFrame=new Target_Frame(this);
cViewActual=(ColorView)findViewById(R.id.colorViewActual);
bestMatchCView=(ColorView)findViewById(R.id.colorViewBestMatch);
mCamera=getCameraInstance();
mPreview=new CameraPreview(this, mCamera,cViewActual,bestMatchCView,tFrame,colorNametextView);
FrameLayout preview=(FrameLayout)findViewById(R.id.camera_preview);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.gravity=Gravity.CENTER;
tFrame.setLayoutParams(params);
preview.addView(tFrame);//the first to put is the one on the bottom of the view
preview.addView(mPreview);
}
/** Check if this device has a camera only if not specified in the manifest */
public boolean checkCameraHardware(Context context) {
Log.d("Function", "checkCameraHardware iniciado");
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
// this device has a camera
return true;
} else {
// no camera on this device
return false;
}
}
/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(){
Log.d("Function", "getCameraInstance iniciado");
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
}
catch (Exception e){
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
/**Check if the device has flash*/
public boolean checkFlash(Context context){
Log.d("Function", "checkFlash iniciado");
if(context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)){
//the device has flash
return true;
}else{
//no flash
return false;
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.d("Function", "onDestroy CameraActivity iniciado");
releaseCamera();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.d("Function", "onPause CameraActivity iniciado");
releaseCamera();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.d("Function", "onResume CameraActivity iniciado");
//Test if i have to put all this code like in onCreate
if(mCamera!=null){
return;
}
mCamera=getCameraInstance();
//if(mPreview!=null){
// return;
//}
mPreview=new CameraPreview(this, mCamera,cViewActual,bestMatchCView,tFrame,colorNametextView);
FrameLayout preview=(FrameLayout)findViewById(R.id.camera_preview);
preview.addView(mPreview);
}
private void releaseCamera(){
Log.d("Function", "releaseCamera iniciado");
if (mCamera != null){
mCamera.setPreviewCallback(null);
mCamera.release(); // release the camera for other applications
mCamera = null;
}
}
}