0

I need to use the phone's camera for an application, but i'm a new programmer and I couldn't find a site anywhere with a full answer.

This is my main Activity:

public class MainActivity extends Activity {

   private Camera cameraObject;
   private ShowCamera showCamera;
   public static Camera isCameraAvailiable(){
      Camera object = null;
      try {
         object = Camera.open(); // attempt to get a Camera instance
      }
      catch (Exception e){
        // Camera is not available (in use or does not exist)
      }
      return object; // returns null if camera is unavailable
   }

   private PictureCallback capturedIt = new PictureCallback() {

      @Override
      public void onPictureTaken(byte[] data, Camera camera) {

      Bitmap bitmap = BitmapFactory.decodeByteArray(data , 0, data .length);
      if(bitmap==null){
         Toast.makeText(getApplicationContext(), "not taken", Toast.LENGTH_SHORT).show();
      }
      else
      {
         Toast.makeText(getApplicationContext(), "taken", Toast.LENGTH_SHORT).show();       
      }
      cameraObject.release();
   }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
      cameraObject = isCameraAvailiable();
      showCamera = new ShowCamera(this, cameraObject);
      FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
      preview.addView(showCamera);
}

  public void snapIt(View view){
      cameraObject.takePicture(null, null, capturedIt);
   }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

Jonah G
  • 91
  • 1
  • 1
  • 14
  • you can run a loop which calls `cameraObject.takePicture(null, null, capturedIt);` multiple times after certain amount of time. – Sagar Pilkhwal Nov 17 '14 at 12:29
  • alternatively you can use touch handler for your `showCamera` object which internally calls your `snapIt` method. – MohK Nov 17 '14 at 12:34
  • can you please give a more detailed answer, if I have another button which is if I want to take a new picture and it starts the process all over. – Jonah G Nov 17 '14 at 12:54
  • 1
    If you want to take a series of pictures after one button press, see _[How to make burst mode available to Camera](http://stackoverflow.com/questions/6889271/how-to-make-burst-mode-available-to-camera)_ – Alex Cohn Nov 17 '14 at 19:09

2 Answers2

0

Create a Fragment that has a Layout to receive the images the use is taking (can be an ImageView just for the last one, a GridView with all the taken pics, w/e makes more sense for your app) and with the "New Picture" Button. Then you just need to add the onClickListener to the Button that calls again the Camera's Activity and adds the photo to where you're planning on storing them. And then just call that Fragment when you take the pic (Inside the callback when he takes a pic) and profit!

lucianohgo
  • 319
  • 1
  • 9
0

Please check this link http://developer.android.com/training/camera/photobasics.html It will allow to open default camera of device.