5

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

Midhun Sivaraj
  • 493
  • 5
  • 13
  • have a look here, http://stackoverflow.com/questions/3601928/camera-capture-without-preview-in-android-2-2 even if it is not relevant it will help – Rahul Borkar Mar 16 '12 at 09:44
  • possible duplicate of [Android Camera without Preview](http://stackoverflow.com/questions/2386025/android-camera-without-preview) – Andreas Eriksson Mar 16 '12 at 09:45

2 Answers2

7
 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
                }
              }
            };
          }
        }
city0666
  • 427
  • 1
  • 7
  • 13
  • 1
    @city0666 , it points errors to myCamera.setPreviewDisplay(dummy.getHolder()); and private PictureCallback getJpegCallback() { – eawedat Dec 24 '13 at 14:07
  • To make it work on every device, surface must be added somewhere and actually created. See comments in http://stackoverflow.com/questions/2386025/android-camera-without-preview – Lucy Mar 20 '14 at 11:57
-1

Not possible, however, there's work-arounds.

See this previous answer, and please, try searching before asking in the future: https://stackoverflow.com/a/3881027/181002

Community
  • 1
  • 1
Andreas Eriksson
  • 8,979
  • 8
  • 47
  • 65
  • 1
    Nothing is impossible bro... Check this out http://stackoverflow.com/questions/9744790/android-possible-to-camera-capture-without-a-preview – Midhun Sivaraj Mar 16 '12 at 22:34