Reputation: 21
I am trying to develop an android application that automatically takes picture using camera intent without any interaction from a user but cannot get the code to trigger the image capturing action automatically. anybody with help??. Here is my code
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pictureButton = findViewById(R.id.captureFront);
countDownTimer = new MyCountDownTimer(startTime, interval);
if (!timerHasStarted) {
countDownTimer.start();
timerHasStarted = true;
} else {
countDownTimer.cancel();
timerHasStarted = false;
// startB.setText("RESTART");
}
}
public void pictureCapture() throws IOException {
pictureButton.setEnabled(true);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File sampleDir = Environment.getExternalStorageDirectory();
try {
imagefile = File.createTempFile("image", ".jpeg", sampleDir);
} catch (IOException e) {
Log.e(TAG, "sdcard access error");
return;
}
takePicture(shutter, raw, postview, jpeg);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
public final void takePicture(Camera.ShutterCallback shutter,
Camera.PictureCallback raw, Camera.PictureCallback postview,
Camera.PictureCallback jpeg) {
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
Toast.makeText(this, "Image saved to:\n" + data.getData(),
Toast.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
Toast.makeText(this, "Image cancelled", Toast.LENGTH_LONG)
.show();
} else {
// Image capture failed, advise user
Toast.makeText(this, "Image failed", Toast.LENGTH_LONG).show();
}
}
}
@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;
}
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
@Override
public void onFinish() {
// text.setText("Time's up!, finishes");
countDownTimer.cancel();
try {
pictureCapture();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onTick(long millisUntilFinished) {
// text.setText("" + millisUntilFinished / 1000);
}
}
};
Upvotes: 2
Views: 3297
Reputation: 28349
Something like this...
mPreview.getCamera().autoFocus(autoFocusCallback);
Camera.AutoFocusCallback autoFocusCallback = new Camera.AutoFocusCallback() {
public void onAutoFocus(boolean success, Camera camera) {
mPreview.getCamera().takePicture(shutterCallback, null, jpegCallback);
}
};
So you'd just call autoFocus
to get the ball rolling on the photo taking process.
Upvotes: 0
Reputation: 4258
Yes you can do it using Camera Api.
No need to do any UI for capturing then Image just call the camera api using service or any background thread or as you want.
Upvotes: 1