/ocr/ocrservice/src/com/googlecode/eyesfree/ocr/service/LanguageManager.java

http://eyes-free.googlecode.com/ · Java · 180 lines · 97 code · 38 blank · 45 comment · 14 complexity · 20630df8bb304ca24e7a8ce5806c7c63 MD5 · raw file

  1. /*
  2. * Copyright (C) 2011 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.googlecode.eyesfree.ocr.service;
  17. import android.content.Context;
  18. import android.content.Intent;
  19. import android.content.res.Resources;
  20. import com.googlecode.eyesfree.ocr.R;
  21. import com.googlecode.eyesfree.ocr.client.Intents;
  22. import com.googlecode.eyesfree.ocr.client.Language;
  23. import java.io.File;
  24. import java.io.FilenameFilter;
  25. import java.lang.ref.WeakReference;
  26. import java.util.ArrayList;
  27. import java.util.Collections;
  28. import java.util.List;
  29. import java.util.Map;
  30. import java.util.TreeMap;
  31. /**
  32. * Provides a list of installed languages.
  33. *
  34. * @author alanv@google.com (Alan Viverette)
  35. */
  36. public class LanguageManager {
  37. private static final String TESSDATA = "tessdata";
  38. private static final String EXTENSION = ".traineddata";
  39. private static final String ENGLISH_UNKNOWN = "Unknown (%s)";
  40. private static final String ISO6391_UNKNOWN = "??";
  41. private File mDatapath;
  42. private File mTessdata;
  43. private Map<String, Language> mSupported;
  44. private List<Language> mAvailable;
  45. private WeakReference<Context> mContext;
  46. public LanguageManager(Context context) {
  47. mContext = new WeakReference<Context>(context);
  48. mSupported = new TreeMap<String, Language>();
  49. mAvailable = new ArrayList<Language>();
  50. }
  51. public boolean loadLanguages() {
  52. Context context = mContext.get();
  53. if (context == null)
  54. return false;
  55. mDatapath = context.getExternalFilesDir(null);
  56. if (mDatapath == null) {
  57. return false;
  58. }
  59. mTessdata = new File(mDatapath, TESSDATA);
  60. loadSupported();
  61. loadAvailable();
  62. // Broadcast intent to let everyone know we've updated the language list
  63. Intent intent = new Intent(Intents.Actions.LANGUAGES_UPDATED);
  64. context.sendBroadcast(intent);
  65. return true;
  66. }
  67. public Map<String, Language> getSupported() {
  68. return mSupported;
  69. }
  70. /**
  71. * Returns the list of available languages. You must call loadLanguages()
  72. * prior to calling this method.
  73. *
  74. * @return the list of available languages
  75. */
  76. public List<Language> getAvailable() {
  77. return mAvailable;
  78. }
  79. /**
  80. * Returns the directory containing tessdata. You must call loadLanguages()
  81. * prior to calling this method.
  82. *
  83. * @return the directory containing tessdata
  84. */
  85. public File getDatapath() {
  86. return mDatapath;
  87. }
  88. /**
  89. * Returns the tessdata directory. You must call loadLanguages() prior to
  90. * calling this method.
  91. *
  92. * @return the tessdata directory
  93. */
  94. public File getTessdata() {
  95. return mTessdata;
  96. }
  97. // *******************
  98. // * Private methods *
  99. // *******************
  100. private void loadSupported() {
  101. mSupported.clear();
  102. Context context = mContext.get();
  103. if (context == null)
  104. return;
  105. Resources res = context.getResources();
  106. String[] english = res.getStringArray(R.array.english);
  107. String[] iso6391 = res.getStringArray(R.array.iso_639_1);
  108. String[] iso6392 = res.getStringArray(R.array.iso_639_2);
  109. for (int i = 0; i < english.length; i++) {
  110. Language lang = new Language(english[i], iso6391[i], iso6392[i]);
  111. mSupported.put(iso6392[i], lang);
  112. }
  113. }
  114. private void loadAvailable() {
  115. mAvailable.clear();
  116. if (mTessdata == null)
  117. return;
  118. String[] files = mTessdata.list(trainedDataFilter);
  119. if (files == null)
  120. return;
  121. for (String file : files) {
  122. int lastIndex = file.lastIndexOf('.');
  123. String iso6392 = file.substring(0, lastIndex);
  124. Language lang = mSupported.get(iso6392);
  125. if (lang == null) {
  126. String english = String.format(ENGLISH_UNKNOWN, iso6392);
  127. lang = new Language(english, ISO6391_UNKNOWN, iso6392);
  128. }
  129. mAvailable.add(lang);
  130. }
  131. Collections.sort(mAvailable);
  132. }
  133. // *******************
  134. // * Private classes *
  135. // *******************
  136. private FilenameFilter trainedDataFilter = new FilenameFilter() {
  137. @Override
  138. public boolean accept(File dir, String filename) {
  139. return filename.endsWith(EXTENSION);
  140. }
  141. };
  142. }