将图像从Google Glass发送到HTTP服务器

时间:2015-04-12 02:31:39

标签: java android python google-glass

我们正在开发一个谷歌玻璃应用程序项目,其中一个目标是让玻璃使用相机将照片发送到我们已经运行的python服务器。到目前为止,我们看到谷歌玻璃代码成功尝试发送图片(从相机中拍摄),但服务器一直拒绝图片。我们知道图片需要采用某种格式才能让服务器接受它。我们有python代码应该将图片发送到服务器,但我们需要java等效的应用程序。 http://172.26.50.107是我们尝试过的服务器" post"图像到。

发送图像的python代码如下:

import requests

r = requests.post('http://172.26.50.107/push', files={'photo': open('coin.jpg', 'rb')})

到目前为止,我们在Android Studio的MainActivity中为应用程序提供的现有Java代码的相关部分如下所示:

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();
       }
   }


}

我们知道我们需要在java中使用等效的python代码来实现图像传输,但我们不知道应该如何,在哪里以及我们应该添加到现有的Java代码中。我们仍然是编码的初学者,因此非常感谢详细解释。

0 个答案:

没有答案