How to capture a photo from the camera without intent
Asked Answered
C

3

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 ?

Cirrus answered 17/8, 2013 at 17:42 Comment(0)
D
5

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.

Dogma answered 17/8, 2013 at 17:51 Comment(8)
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 :#18290044Cirrus
What if i open the camera with camera = Camera.open(); and use Camera.takePicture() as seen on the documentation ?Cirrus
@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.Dogma
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 apiCirrus
@mremremre1: AFAIK, you cannot take a screenshot of a SurfaceView.Dogma
Damn. Then i ll try taking a photo and overlaying it with a screenshot of the layout xml.Cirrus
@mremremre1: If your app is only for API Level 14+, you could see if TextureView lets you take screenshots (developer.android.com/reference/android/view/TextureView.html), though I suspect that this will not work either.Dogma
@Dogma any library is there to capture image in background without preiview..??Telluride
C
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

Ce answered 8/7, 2014 at 10:5 Comment(1)
Thank God for that :DCe
C
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.

Connivent answered 12/11, 2018 at 1:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.