Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture picture from front camera and save it on SD card without preview

Tags:

android

camera

How can i capture a picture from front camera without preview and save it to SD card. Kindly help me with source code.

like image 601
Midhun Sivaraj Avatar asked Mar 16 '12 09:03

Midhun Sivaraj


People also ask

Which method of the camera class display the preview of the image captured?

Create a Preview Class - Create a camera preview class that extends SurfaceView and implements the SurfaceHolder interface. This class previews the live images from the camera.

How do you open camera through intent and display captured image?

This is done as follows: Intent camera_intent = new Intent(MediaStore. ACTION_IMAGE_CAPTURE); startActivityForResult(camera_intent, pic_id); Now use the onActivityResult() method to get the result, here is the captured image.


1 Answers

 public void takePictureNoPreview(Context context){
          // open back facing camera by default
          Camera myCamera=Camera.open();

          if(myCamera!=null){
            try{
              //set camera parameters if you want to
              //...

              // here, the unused surface view and holder
              SurfaceView dummy=new SurfaceView(context)
              myCamera.setPreviewDisplay(dummy.getHolder());    
              myCamera.startPreview(); 

              myCamera.takePicture(null, null, getJpegCallback()):

            }finally{
              myCamera.close();
            }      

          }else{
            //booo, failed!
          }


          private PictureCallback getJpegCallback(){
            PictureCallback jpeg=new PictureCallback() {   
              @Override
              public void onPictureTaken(byte[] data, Camera camera) {
                FileOutputStream fos;
                try {
                  fos = new FileOutputStream("test.jpeg");
                  fos.write(data);
                  fos.close();
                }  catch (IOException e) {
                  //do something about it
                }
              }
            };
          }
        }
like image 162
city0666 Avatar answered Sep 30 '22 06:09

city0666