0

I want to implement a service where, specified a time of the day, the phone will take a picture automatically.

Since now, I followed the official tutorial:

public class PhotoHandler {

Activity a;

public PhotoHandler(Activity a) {
    this.a = a;
}

public void takePicture() {
    Intent imageIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyITTTPictures");
    imagesFolder.mkdirs();
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File image = new File(imagesFolder, timeStamp+".jpg");
    Uri uriSavedImage = Uri.fromFile(image);
    imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
    a.startActivityForResult(imageIntent, 0);
}

}

Starting my app I have control of my camera, but I have to take manually a picture; I want her to take a picture when the timer I set is out. Is there a way to make her do this automatically?

elmazzun
  • 1,066
  • 2
  • 18
  • 44

1 Answers1

3

Use this tutorial to trigger the camera to take a picture whenever you want.

Manifest:

<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name="de.vogella.camera.api.MakePhotoActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Layout:

<Button
    android:id="@+id/captureFront"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:onClick="onClick"
    android:text="Make Photo" />

To save to SD card:

public class PhotoHandler implements PictureCallback {

  private final Context context;

  public PhotoHandler(Context context) {
    this.context = context;
  }

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

    File pictureFileDir = getDir();

    if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {

      Log.d(MakePhotoActivity.DEBUG_TAG, "Can't create directory to save image.");
      Toast.makeText(context, "Can't create directory to save image.",
          Toast.LENGTH_LONG).show();
      return;

    }

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
    String date = dateFormat.format(new Date());
    String photoFile = "Picture_" + date + ".jpg";

    String filename = pictureFileDir.getPath() + File.separator + photoFile;

    File pictureFile = new File(filename);

    try {
      FileOutputStream fos = new FileOutputStream(pictureFile);
      fos.write(data);
      fos.close();
      Toast.makeText(context, "New Image saved:" + photoFile,
          Toast.LENGTH_LONG).show();
    } catch (Exception error) {
      Log.d(MakePhotoActivity.DEBUG_TAG, "File" + filename + "not saved: "
          + error.getMessage());
      Toast.makeText(context, "Image could not be saved.",
          Toast.LENGTH_LONG).show();
    }
  }

  private File getDir() {
    File sdDir = Environment
      .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    return new File(sdDir, "CameraAPIDemo");
  }
} 

To take a photo

public class MakePhotoActivity extends Activity {
  private final static String DEBUG_TAG = "MakePhotoActivity";
  private Camera camera;
  private int cameraId = 0;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // do we have a camera?
    if (!getPackageManager()
        .hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
      Toast.makeText(this, "No camera on this device", Toast.LENGTH_LONG)
          .show();
    } else {
      cameraId = findFrontFacingCamera();
      if (cameraId < 0) {
        Toast.makeText(this, "No front facing camera found.",
            Toast.LENGTH_LONG).show();
      } else {
        camera = Camera.open(cameraId);
      }
    }
  }

  public void onClick(View view) {
    camera.takePicture(null, null,
        new PhotoHandler(getApplicationContext()));
  }

  private int findFrontFacingCamera() {
    int cameraId = -1;
    // Search for the front facing camera
    int numberOfCameras = Camera.getNumberOfCameras();
    for (int i = 0; i < numberOfCameras; i++) {
      CameraInfo info = new CameraInfo();
      Camera.getCameraInfo(i, info);
      if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
        Log.d(DEBUG_TAG, "Camera found");
        cameraId = i;
        break;
      }
    }
    return cameraId;
  }

  @Override
  protected void onPause() {
    if (camera != null) {
      camera.release();
      camera = null;
    }
    super.onPause();
  }

} 

EDIT: cameraId is not always set to -1, it is just a placeholder while you are looking for the front facing camera. When it is found, cameraId is set to the index of that camera

MrEngineer13
  • 38,642
  • 13
  • 74
  • 93
  • The is answers uses `camera.takePicture()` and I am pretty certain that that api call starts a camera activity in which the user must then press the shutter button, so this doesn't automatically take photos. Please confirm whether `takePicture()` takes a picture, or just opens the camera. The question implies to me that, once the camera is open, how can a picture be taken without user input, ie "automatically" like many timelapse camera apps do. – TwoFingerRightClick Jul 02 '22 at 01:56