title: OCR with text to speech
body: <p>I'm in trouble with this android java class.
  The goal that I'm trying to reach with this activity is:
  start camera-->take photo-->recognize text-->(tts and recognized text in the edittext)
  TTS would had to start automatically after the text recognition. But it didn't. The recognized text also must appear in the edit text (and it's work)
  I'm ok with the three first steps. TTS gives me an issue: "speak failed: not bound to TTS engine". I'm trying to understand where is the problem. I also followed some guide to use tts, I think the problem is about stop or shutdown of tts. Any idea? Thanks a lot</p>
  
  <p>Code</p>
  
  <pre><code> public class CameraActivity extends Activity {
      public TextToSpeech tts1;
      public String voce;
      public String text;
      public static final String DATA_PATH = Environment
              .getExternalStorageDirectory().toString() + "/OcrTesiDiLaurea/";
  
      public static final String lingua= Locale.getDefault().getISO3Language();
      public static final String lang = lingua;
      private static final String TAG = MainActivity.class.getName();
      protected Button _button;
      protected EditText _field;
      protected String _path;
      protected boolean _taken;
      protected static final String PHOTO_TAKEN = "foto catturata";
  
  
  
      @Override
      public void onDestroy(){
          if(tts1 !=null){
              tts1.stop();
              tts1.shutdown();
          }
          super.onDestroy();
      }
      @Override
      public void onCreate(Bundle savedInstanceState) {
  
          String[] paths = new String[] { DATA_PATH, DATA_PATH + "tessdata/" };
          for (String path : paths) {
              File dir = new File(path);
              if (!dir.exists()) {
                  if (!dir.mkdirs()) {
                      Log.v(TAG, "ERRORE: creazione directory " + path + " in memoria fallita");
                      return;
                  } else {
                      Log.v(TAG, "creazione directory " + path + " eseguita con successo");
                  }
              }
          }
  
          if (!(new File(DATA_PATH + "tessdata/" + lang + ".traineddata")).exists()) {
              try {
                  AssetManager assetManager = getAssets();
                  InputStream in = assetManager.open("tessdata/" + lang + ".traineddata");
  
                  OutputStream out = new FileOutputStream(DATA_PATH
                          + "tessdata/" + lang + ".traineddata");
                  // Transferimento dati
                  byte[] buf = new byte[1024];
                  int len;
  
                  while ((len = in.read(buf)) &gt; 0) {
                      out.write(buf, 0, len);
                  }
                  in.close();
  
                  out.close();
                  Log.v(TAG, "file " + lang + " copiato con successo in  traineddata");
              } catch (IOException e) {
                  Log.e(TAG, "impossibile copiare " + lang + " traineddata " + e.toString());
              }
          }
  
  
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_camera);
  
          _field = (EditText) findViewById(R.id.field);
          _button = (Button) findViewById(R.id.button);
          _button.setOnClickListener(new ButtonClickHandler());
          _path = DATA_PATH + "/ocr.jpg";
  
  
  
          tts1=new TextToSpeech(getApplicationContext(),
                  new TextToSpeech.OnInitListener() {
                      @Override
                      public void onInit(int status) {
                          if(status != TextToSpeech.ERROR){
  
                              tts1.setLanguage(Locale.getDefault());
                          }
                      }
                  });
  
  
      }
  
      public class ButtonClickHandler implements View.OnClickListener {
          public void onClick(View view) {
              Log.v(TAG, "avvio fotocamera");
              startCameraActivity();
          }
      }
  
  
  
      // http://labs.makemachine.net/2010/03/simple-android-photo-capture/
      protected void startCameraActivity() {
          File file = new File(_path);
          Uri outputFileUri = Uri.fromFile(file);
          final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
          intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
          startActivityForResult(intent, 0);
      }
      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          Log.i(TAG, "resultCode: " + resultCode);
          if (resultCode == -1) {
              onPhotoTaken();
          } else {
              Log.v(TAG, "User cancelled");
          }
      }
      @Override
      protected void onSaveInstanceState(Bundle outState) {
          outState.putBoolean(CameraActivity.PHOTO_TAKEN, _taken);
      }
      @Override
      protected void onRestoreInstanceState(Bundle savedInstanceState) {
          Log.i(TAG, "onRestoreInstanceState()");
          if (savedInstanceState.getBoolean(CameraActivity.PHOTO_TAKEN)) {
              onPhotoTaken();
          }
      }
  
  
  
      protected void onPhotoTaken() {
          _taken = true;
          BitmapFactory.Options options = new BitmapFactory.Options();
          options.inSampleSize = 4;
          Bitmap bitmap = BitmapFactory.decodeFile(_path, options);
          try {
              ExifInterface exif = new ExifInterface(_path);
              int exifOrientation = exif.getAttributeInt(
                      ExifInterface.TAG_ORIENTATION,
                      ExifInterface.ORIENTATION_NORMAL);
              Log.v(TAG, "Orient: " + exifOrientation);
              int rotate = 0;
              switch (exifOrientation) {
                  case ExifInterface.ORIENTATION_ROTATE_90:
                      rotate = 90;
                      break;
                  case ExifInterface.ORIENTATION_ROTATE_180:
                      rotate = 180;
                      break;
                  case ExifInterface.ORIENTATION_ROTATE_270:
                      rotate = 270;
                      break;
              }
              Log.v(TAG, "Rotation: " + rotate);
              if (rotate != 0) {
              // Getting width &amp; height of the given image.
                  int w = bitmap.getWidth();
                  int h = bitmap.getHeight();
              // Setting pre rotate
                  Matrix mtx = new Matrix();
                  mtx.preRotate(rotate);
              // Rotating Bitmap
                  bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, false);
              }
  
              bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
          } catch (IOException e) {
              Log.e(TAG, "Couldn't correct orientation: " + e.toString());
          }
  
          Log.v(TAG, "Inizio utilizzo librerie tesseract");
          TessBaseAPI baseApi = new TessBaseAPI();
          baseApi.setDebug(true);
          baseApi.init(DATA_PATH, lang);
          baseApi.setImage(bitmap);
          String recognizedText = baseApi.getUTF8Text();
          baseApi.end();
  
  
          _field.getText().clear();
  
  
          Log.v(TAG, "OCRED TEXT: " + recognizedText);
          if ( lang.equalsIgnoreCase("eng") ) {
              recognizedText = recognizedText.replaceAll("[^a-zA-Z0-9]+", " ");
          }
          if ( lang.equalsIgnoreCase("ita") ) {
              recognizedText = recognizedText.replaceAll("[^a-zA-Z0-9]+", " ");
          }
          recognizedText = recognizedText.trim();
          if ( recognizedText.length() != 0 ) {
              _field.setText(_field.getText().toString().length() == 0 ? recognizedText : _field.getText() + " " + recognizedText);
              _field.setSelection(_field.getText().toString().length());
          }
          voce=recognizedText;
      }
  
      public void convertTextToSpeech() {
          text = voce;
          if (null == text || "".equals(text)) {
              text = "NON POSSO PRONUNCIARE NESSUNA PAROLA, MANCA IL TESTO";
          }
          tts1.speak(text, TextToSpeech.QUEUE_FLUSH, null);
      }
  }
  </code></pre>