开发者

resuming camera.preview() after photo has been taken

开发者 https://www.devze.com 2023-04-02 07:34 出处:网络
I have a surfaceview which is displaying a camera preview I have it taking a picture and display it on the surfaceview but when I press the back button it closes the application but I want it to displ

I have a surfaceview which is displaying a camera preview I have it taking a picture and display it on the surfaceview but when I press the back button it closes the application but I want it to display the original camera preview.

I also only want to reset the display when the picture is being displayed and have normally functionality returned to back button when camera preview is showing.

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_OF开发者_开发知识库F);
                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);



        }
    };

above is my entire code (minus the imports) of my activity.

any help is greatly appreciated.


What you need to do is override the onBackPressed() method:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号