Skip to content

Instantly share code, notes, and snippets.

@arcatdmz
Last active December 10, 2015 04:28
Show Gist options
  • Save arcatdmz/4380932 to your computer and use it in GitHub Desktop.
Save arcatdmz/4380932 to your computer and use it in GitHub Desktop.
Upload images from the connected camera every 10 minutes with help of Phybots and Twitter4j libraries.
import twitter4j.*;
import twitter4j.auth.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Date;
import javax.imageio.ImageIO;
import com.phybots.service.Camera;
import com.phybots.service.ImageProvider.ImageListener;
/**
* Upload images from the connected camera every 10 minutes.
*
* <dl>
* <dt>Required libraries:</dt>
* <dd><ul>
* <li>phybots-core-*.jar, capture-*.jar from <a href="http://phybots.com">http://phybots.com</a></li>
* <li>dsj.jar, dsj.dll (for Windows) from <a href="http://www.humatic.de/htools/dsj.htm">http://www.humatic.de/htools/dsj.htm</a></li>
* <li>twitter4j-core-*.jar, twitter4j-media-support-*.jar from <a href="http://twitter4j.org">http://twitter4j.org</a></li>
* </ul></dd>
* <dt>Other requirements:</dt>
* <dd>Twitter consumer key and secret, access token and access token secret
* from <a href="http://dev.twitter.com">http://dev.twitter.com</a></dd>
* </dl>
*
* @author Jun Kato
*/
public class TwitterCam {
public static final String CONSUMER_KEY = "*";
public static final String CONSUMER_SECRET = "*";
public static final String ACCESS_TOKEN = "*";
public static final String ACCESS_TOKEN_SECRET = "*";
private int lastMinute = 0;
public static void main(String[] args) throws Exception {
new TwitterCam(args);
}
public TwitterCam(String[] args) {
// Start capturing images from the camera.
Camera camera = new Camera();
camera.start();
camera.addImageListener(new ImageListener() {
@SuppressWarnings("deprecation")
public void imageUpdated(BufferedImage image) {
Date date = new Date();
if (lastMinute == date.getMinutes())
return;
if (date.getMinutes() % 5 != 0)
return;
// Save the captured image as a file every 5 minutes.
lastMinute = date.getMinutes();
String fileName = date.getYear() + "-"
+ date.getMonth() + "_" + date.getDay() + "-"
+ date.getHours() + "_" + date.getMinutes() + ".jpg";
File file = new File(fileName);
try {
ImageIO.write(image, "JPEG", file);
} catch (IOException e) {
e.printStackTrace();
}
if (date.getMinutes() % 10 != 0)
return;
// Post the captured image to Twitter every 10 minutes.
final Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
twitter.setOAuthAccessToken(new AccessToken(ACCESS_TOKEN, ACCESS_TOKEN_SECRET));
final StatusUpdate status = new StatusUpdate(
date.getHours() + ":" + date.getMinutes() + " #christmas");
status.media(file);
try {
twitter.updateStatus(status);
} catch (TwitterException e) {
e.printStackTrace();
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment