私のカメラアプリには、カメラをプレビューするための2つのサーフェスビューと、tFrameと呼ばれる形状を描画するための2つのサーフェスビューがあります(そのタイプは、サーフェスビューを拡張するisTarget_Frame)。最初は正しく動作しますが、アプリが表示されていないときに onResume の後、tFrame surfaceview は表示されず、カメラをプレビューするものだけが表示されます。
ここに私のコードがあります
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;
}
}
}