拍完照片后恢复camera.preview()

时间:2011-09-05 14:49:21

标签: android android-camera

我有一个正在显示相机预览的surfaceview我有它拍照并在surfaceview上显示它但是当我按下后退按钮它会关闭应用程序但是我希望它显示原始相机预览。

我也只想在显示图片时重置显示,并在显示相机预览时正常返回功能。

public class cameraView extends Activity implements SurfaceHolder.Callback{
 private SurfaceView preview=null;
 private SurfaceHolder previewHolder=null;
 private Camera camera=null;
 private ImageButton bt = null;
 private Toast t = null;
 private Camera.Parameters param = null; 
 private Button b = null;

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.cam);

  t = Toast.makeText(this, "Just Click The Magnifying Glass To Search", 5000);//creates a new pop up message that lasts for 5 seconds

  t.setGravity(Gravity.CENTER|Gravity.CENTER,   0, 0);
     t.show();




 b = (Button)findViewById(R.id.test);
 b.setOnClickListener(search);
 bt = (ImageButton)findViewById(R.id.button);//creates instance of button
 bt.setOnClickListener(search);//starts an on click listener for button



  preview=(SurfaceView)findViewById(R.id.myview);//creates instance of surfaceview
  previewHolder=preview.getHolder();//creates a surfaceholder
  previewHolder.addCallback(this);//sets surfaceholder callback as the activity
  previewHolder.setType(3);//sets the type to SURFACE_TYPE_PUSH_BUFFERS 

}

    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { //creates a method that is called automatically when the surface is changed
        // TODO Auto-generated method stub
        Camera.Parameters param = camera.getParameters();//sets param to be equal to camera parametors
        param.setPreviewSize(width, height);//sets width and height to that of what is passed back to it when callback calls it 
        //param.setFlashMode(Parameters.FLASH_MODE_TORCH);
        camera.setParameters(param);//sets the camera parameters to param
        camera.startPreview();//starts the preview
        camera.autoFocus(cb);//calls autofocus callback method

    }

    public void surfaceCreated(SurfaceHolder holder) {//called when the surface has been created
        // TODO Auto-generated method stub
        camera = Camera.open();//opens the camera and sets it to the camera variable




        try{
            camera.setPreviewDisplay(previewHolder);//sets the display area to previewHolder


        }catch(Throwable t){
            Log.e(""+t, null);
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {//called when sureface is destroyed or when activity is closed
        // TODO Auto-generated method stub
        camera.stopPreview();//stops the preview
        camera.release();//releases the camera
        camera = null;// clears the camera so it contains no information 
    }





    AutoFocusCallback cb = new AutoFocusCallback(){
    public void onAutoFocus(boolean success, Camera camera) {
        // TODO Auto-generated method stub
        //return true;
    }

    };




    private OnClickListener search = new OnClickListener() {
        public void onClick(View v) {
          // do something when the button is clicked
            switch(v.getId()){
            case R.id.test:
                param = camera.getParameters();
            if(param.getFlashMode().equals(Parameters.FLASH_MODE_TORCH)){
                /*
                 * IF statement to check the current flash mode and change it appropriately
                 */
                param.setFlashMode(Parameters.FLASH_MODE_OFF);
                camera.setParameters(param); 
            }else{
            param.setFlashMode(Parameters.FLASH_MODE_TORCH);
            camera.setParameters(param);
            }

            break;

            case R.id.button:
                camera.autoFocus(cb);//calls autofocus with call back of cb
                  Handler handler = new Handler(); 
                    handler.postDelayed(new Runnable() { 
                         public void run() { 

                             camera.takePicture(null, mPictureCallback, mPictureCallback);

                         } 
                    }, 2000);           
                     break; 

                }

            }

    };





    Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
        public void onPictureTaken(byte[] imageData, Camera c) {

            bt.setVisibility(4);
            b.setVisibility(4);
            b.setEnabled(false);
            bt.setEnabled(false);



        }
    };

以上是我的活动的全部代码(减去进口)。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:3)

您需要做的是覆盖onBackPressed()方法:

@Override public void onBackPressed()
{    
  // Restart camera
}