We are working on a google glass app project and one of the goals is to make the glass send a picture it takes using the camera to a python server that we already have running. So far it looks like the google glass code we have is successfully attempting to send the picture (taken from the camera), but the server keeps rejecting the picture. We know that the picture needs to be in a certain format for the server to accept it. We have the python code that is supposed to send the picture to the server, but we need the java equivalent for our app. http://172.26.50.107 is the server to which we're trying to "post" the image to.
The python code that would send the image is below:
import requests
r = requests.post('http://172.26.50.107/push', files={'photo': open('coin.jpg', 'rb')})
The relevant part of our existing java code that we have for the app in the MainActivity in Android Studio so far is shown below:
private static final int TAKE_PICTURE_REQUEST = 1;
private void takePicture() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_PICTURE_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TAKE_PICTURE_REQUEST && resultCode == RESULT_OK) {
String thumbnailPath = data.getStringExtra(Intents.EXTRA_THUMBNAIL_FILE_PATH);
String picturePath = data.getStringExtra(Intents.EXTRA_PICTURE_FILE_PATH);
processPictureWhenReady(picturePath);
// TODO: Show the thumbnail to the user while the full picture is being
// processed.
}
super.onActivityResult(requestCode, resultCode, data);
}
private void sendImageServer(final String picturePath)
{
String url = "http://172.26.50.107/pushimg";
File file = new File(
// Environment.getExternalStorageDirectory().getAbsolutePath(),
picturePath);
Log.v("info", url);
Log.v("info", picturePath);
HttpClient httpclient = new DefaultHttpClient();
Log.v("info", "data 0");
HttpPost httppost = new HttpPost(url);
try {
Log.v("info", "data 1");
InputStreamEntity reqEntity = new InputStreamEntity(
new FileInputStream(file), -1);
Log.v("info", "data 2");
reqEntity.setContentType("binary/octet-stream");
reqEntity.setChunked(true); // Send in multiple parts if needed
httppost.setEntity(reqEntity);
//httppost.addHeader("Content-length", "0");
Log.v("info", "data 3");
HttpResponse response = httpclient.execute(httppost);
Log.v("info", response.getStatusLine().toString());
Log.v("info", "data DONE");
//Do something with response...
} catch (Exception e) {
// show error
Log.v("info", "ERROR");
}
}
private void processPictureWhenReady(final String picturePath) {
final File pictureFile = new File(picturePath);
if (pictureFile.exists()) {
// The picture is ready; process it.
Log.v("info", "-------------------------------------------");
Log.v("info", picturePath);
sendImageServer(picturePath);
} else {
// The file does not exist yet. Before starting the file observer, you
// can update your UI to let the user know that the application is
// waiting for the picture (for example, by displaying the thumbnail
// image and a progress indicator).
final File parentDirectory = pictureFile.getParentFile();
FileObserver observer = new FileObserver(parentDirectory.getPath(),
FileObserver.CLOSE_WRITE | FileObserver.MOVED_TO) {
// Protect against additional pending events after CLOSE_WRITE
// or MOVED_TO is handled.
private boolean isFileWritten;
@Override
public void onEvent(int event, String path) {
if (!isFileWritten) {
// For safety, make sure that the file that was created in
// the directory is actually the one that we're expecting.
File affectedFile = new File(parentDirectory, path);
isFileWritten = affectedFile.equals(pictureFile);
if (isFileWritten) {
stopWatching();
// Now that the file is ready, recursively call
// processPictureWhenReady again (on the UI thread).
runOnUiThread(new Runnable() {
@Override
public void run() {
processPictureWhenReady(picturePath);
}
});
}
}
}
};
observer.startWatching();
}
}
}
We know that we need the equivalent of the python code in our java to make the image transfer possible, but we do not know how, where and what we are supposed to add to our existing java code. We’re still beginners in coding, so a detailed explanation would be greatly appreciated.