7

I want my app to be able to capture photos without using another application. The code i used :

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File photo = null;
    try
    {
        photo = this.createTemporaryFile("picture", ".jpg");
        photo.delete();
    }
    catch(Exception e)
    {
        Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_LONG).show();

    }
    mImageUri = Uri.fromFile(photo);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

But this code uses the phone's main camera app. Can anyone give me some code ?

mremremre1
  • 1,036
  • 3
  • 16
  • 34

3 Answers3

4

Taking a picture directly using the Camera class is insanely complicated to get right.

I am working on a library to simplify this, where you just add a CameraFragment to your app for the basic preview UI, and call takePicture() on it to take a picture, with various ways to configure the behavior (e.g., where the pictures get saved). However, this library is still a work in progress.

Can anyone give me some code ?

"Some code" is going to be thousands of lines long (for a complete implementation, including dealing with various device-specific oddities).

You are welcome to read the Android developer documentation on the subject.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I have an app that uses the mediarecorder to record video and its not that big. Isnt there something similar for taking images ? What i want is to get an image and overlay it with another bitmap to create a file.Please check out my full question on this matter :http://stackoverflow.com/questions/18289544/taking-screenshot-programmatically-doesnt-capture-the-contents-of-surfaceview – mremremre1 Aug 17 '13 at 17:54
  • What if i open the camera with camera = Camera.open(); and use Camera.takePicture() as seen on the documentation ? – mremremre1 Aug 17 '13 at 18:03
  • @mremremre1: "I have an app that uses the mediarecorder to record video and its not that big" -- then I suspect that you're not handling all the devices. Getting the previews correct alone, for portrait/landscape plus rear-facing/FFC, takes a lot. "What if i open the camera with camera = Camera.open(); and use Camera.takePicture() as seen on the documentation ?" -- that's certainly the starting point. – CommonsWare Aug 17 '13 at 18:06
  • Thanks i ll try it. Did you see the other question ? Because if there is a way to get a screenshot directly from the surfaceView i wont have to use the camera api – mremremre1 Aug 17 '13 at 18:08
  • @mremremre1: AFAIK, you cannot take a screenshot of a `SurfaceView`. – CommonsWare Aug 17 '13 at 18:08
  • Damn. Then i ll try taking a photo and overlaying it with a screenshot of the layout xml. – mremremre1 Aug 17 '13 at 18:11
  • @mremremre1: If your app is only for API Level 14+, you could see if `TextureView` lets you take screenshots (https://developer.android.com/reference/android/view/TextureView.html), though I suspect that this will not work either. – CommonsWare Aug 17 '13 at 18:17
  • @CommonsWare any library is there to capture image in background without preiview..?? – android learner May 06 '15 at 14:09
1

once you have the camera preview set, you need to do the following...

protected static final int MEDIA_TYPE_IMAGE = 0; 

public void capture(View v)
{   
    PictureCallback pictureCB = new PictureCallback() {
        public void onPictureTaken(byte[] data, Camera cam) {
          File picFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
          if (picFile == null) {
            Log.e(TAG, "Couldn't create media file; check storage permissions?");
            return;
          }

          try {
            FileOutputStream fos = new FileOutputStream(picFile);
            fos.write(data);
            fos.close();
          } catch (FileNotFoundException e) {
            Log.e(TAG, "File not found: " + e.getMessage());
            e.getStackTrace();
          } catch (IOException e) {
            Log.e(TAG, "I/O error writing file: " + e.getMessage());
            e.getStackTrace();
          }
        }
      };
      camera.takePicture(null, null, pictureCB);
}

And the getOutputMediaFile function:

private File getOutputMediaFile(int type) 
{
      File dir = new File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES), getPackageName());
      if (!dir.exists()) 
      {
        if (!dir.mkdirs()) 
        {
          Log.e(TAG, "Failed to create storage directory.");
          return null;
        }
      }
      String timeStamp = new SimpleDateFormat("yyyMMdd_HHmmss", Locale.UK).format(new Date());
      if (type == MEDIA_TYPE_IMAGE) 
      {
        return new File(dir.getPath() + File.separator + "IMG_"+ timeStamp + ".jpg");
      } 
      else 
      {
        return null;
      }
}

And you are done!!!

found it here

Pontios
  • 2,377
  • 27
  • 32
1

Camera was deprecated in API 21, the new way is the use android.hardware.camera2.

To enumerate, query, and open available camera devices, obtain a CameraManager instance.

To quickly summarize:

  1. Obtain a camera manager instance by calling Context.getSystemService(String)
  2. Get a string[] of device camera IDs by calling CameraManager.GetCameraIdList().
  3. Call CameraManager.OpenCamera(...) with the desired camera ID from the previous step.

Once the camera is opened, the callback provided in OpenCamera(...) will be called.

Moffen
  • 1,817
  • 1
  • 14
  • 34