【发布时间】:2016-01-14 20:27:50
【问题描述】:
我是编码的初学者,但我设法制作了一个程序,当建立一个 arduino 连接时,它会自动在附加的网络摄像头上拍摄并将该照片直接上传到 Twitter 页面。
我现在遇到的问题是,现在每次触发连接时,它都会上传我每次拍摄/上传的第一张照片,而不是相机当前正在查看的新照片。
我不知道如何解决这个问题,任何帮助将不胜感激。
这是我的代码:
import twitter4j.conf.*;
import twitter4j.*;
import twitter4j.auth.*;
import twitter4j.api.*;
import java.util.*;
import processing.video.*;
import processing.serial.*;
int picCount = 0;
Capture webcam;
Twitter twitter;
Serial myPort; // The serial port
String inString; // Input string from serial port
int lf = 10; // ASCII linefeed
void setup()
{
size(640, 480);
webcam = new Capture(this, 640, 480);
String[] devices = Capture.list();
println(devices);
webcam.start();
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey("****");
cb.setOAuthConsumerSecret("****");
cb.setOAuthAccessToken("****");
cb.setOAuthAccessTokenSecret("****");
TwitterFactory tf = new TwitterFactory(cb.build());
twitter = tf.getInstance();
myPort = new Serial(this, Serial.list()[0], 9600);
myPort.bufferUntil(lf);
}
void draw()
{
if (webcam.available() == true) {
webcam.read();
image(webcam, 0, 0);
}
if (inString != null) {
inString = inString.trim();
int val = int(inString);
if (val == 1) {
println("saving...");
save("cam" + picCount + ".png");
picCount++;
//send tweet
File file = new File("C:\\Users\\Jake\\Documents\\Processing\\Twitter test 2\\Twittertest2\\cam" + picCount + ".png");
delay (3000);
tweetPic(file, "");
}
}
}
void testPassingFile(File _file)
{
println(_file.exists());
println(_file.getName());
println(_file.getPath());
println(_file.canRead());
}
void tweetPic(File _file, String theTweet)
{
try
{
StatusUpdate status = new StatusUpdate(theTweet);
status.setMedia(_file);
twitter.updateStatus(status);
}
catch (TwitterException te)
{
println("Error: "+ te.getMessage());
}
}
void serialEvent(Serial p) {
inString = p.readString();
}
【问题讨论】:
-
这一行是否保存了不同文件名的文件:
save("cam" + picCount + ".png");? -
我相信这是为了保存名为 cam 的图像,然后是一个比上次保存的数字高 1 的数字,然后是 .png 作为文件类型
标签: java upload processing webcam