2015-11-03 75 views
0

理想情況下,我試圖在voice_trigger.xml中使用用戶的語音輸入並將其存儲在strings.xml中,以便我可以比較相機中的字符串變量活動,如果它匹配,拍照並保存。Android Studio Google Glass:如何使用語音命令拍攝圖片

我不太清楚如何實現這一點。這聽起來像我有解決這個問題的正確想法嗎?

下圖:voice_trigger.xml,strings.xml中,CameraActivity.java

voice_trigger.xml:

<?xml version="1.0" encoding="utf-8"?> 

<!-- For more information about voice trigger, check out: https://developers.google.com/glass/develop/gdk/starting-glassware --> 
<trigger keyword="Visual tracker"> 
<!-- <input prompt="@string/glass_tracking_prompt" /> --> 
     <input interaction ="@string/take_picture"/> 
    <constraints network="true" 
     camera="true" /> 
    </trigger> 

的strings.xml:

<resources> 
    <string name="app_name">GlassTracker</string> 
    <string name="title_activity_live_card_service">Tracking Prime LiveCard</string> 
    <string name="title_activity_live_card_renderer">Tracking Prime Activity</string> 
    <string name="action_stop">Close App</string> 
    <string name="action_tune_track">Tune Tracker</string> 
    <string name="action_start_track">Start Tracking</string> 
    <string name="action_stop_track">Stop Tracking</string> 
    <string name="hello_world">Hello visual tracker!</string> 
    <string name="glass_tracking_trigger">Visual tracker</string> 
    <string name="glass_tracking_prompt">Tap to tune</string> 
    <string name="take_picture">Take a picture</string> 
</resources> 

CameraActivity.java:

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.os.FileObserver; 
import android.provider.MediaStore; 
import android.speech.RecognizerIntent; 

import com.google.android.glass.content.Intents; 

import java.io.File; 
import java.util.ArrayList; 
import java.util.List; 

/** 
* Created by 
*/ 
public class CameraActivity extends Activity { 

    private CameraSurfaceView cameraView; 


    private static final int TAKE_PICTURE_REQUEST = 1; 

    //Take the picture only if the string take_picture from voice control allows for it. 
    private void takePicture() { 

     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     startActivityForResult(intent, TAKE_PICTURE_REQUEST); 
    } 




    @Override 
    /*Take out ints */ 
    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 processPictureWhenReady(final String picturePath) { 
     final File pictureFile = new File(picturePath); 

     if (pictureFile.exists()) { 
      // The picture is ready; process it. 
     } 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(); 
     } 
    } 




    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // Initiate CameraView 
     cameraView = new CameraSurfaceView(this); //Calls CameraSurfaceView 

     // Set the view 
     this.setContentView(cameraView); 


    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 

     // Do not hold the camera during onResume 
     if (cameraView != null) { 
      cameraView.releaseCamera(); 
     } 


    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 

     // Do not hold the camera during onPause 
     if (cameraView != null) { 
      cameraView.releaseCamera(); 
     } 
    } 






} 

非常感謝!

回答

0

在voice_triggers.xml文件中,將偵聽語音命令。當你向設備說出你的命令時,voiceRes會填充你說的內容,因爲這是一個字符串類型的數組,所以理想情況下需要將它轉換爲一個字符串。然後根據用戶所說的做比較很簡單。另外,確保在比較中包含括號,否則if語句將不起作用。這是我很快發現的解決方案,我確信有很多方法可以做到這一點。

onCreate()方法,之後this.setContentView(cameraView);

ArrayList<String> voiceRes =getIntent().getExtras().getStringArrayList(RecognizerIntent.EXTRA_RESULTS); 
String Voicedata = voiceRes.toString(); 


if(Voicedata.equals("[take a picture]") 
    { 
     takepicture(); 

    } 
+0

介意[解釋您的解決方案(http://stackoverflow.com/help/how-to-answer)一點? –

+0

您應該使用編輯按鈕將解釋添加到您的答案中。 ;) –

相關問題