PageRenderTime 37ms CodeModel.GetById 15ms app.highlight 16ms RepoModel.GetById 3ms app.codeStats 0ms

/ocr/textscanner/src/com/google/marvin/textscanner/PrefsActivity.java

http://eyes-free.googlecode.com/
Java | 150 lines | 98 code | 28 blank | 24 comment | 14 complexity | 1d58c94ef8b618ccd5f3019d58dd85e6 MD5 | raw file
  1/*
  2 * Copyright (C) 2009 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 */
 16package com.google.marvin.textscanner;
 17
 18import android.hardware.Camera;
 19import android.os.Bundle;
 20import android.preference.ListPreference;
 21import android.preference.Preference;
 22import android.preference.PreferenceActivity;
 23import android.util.Log;
 24
 25import com.android.ocr.client.Language;
 26import com.android.ocr.client.Ocr;
 27
 28/**
 29 * Displays preferences for Text Scanner.
 30 * 
 31 * @author alanv@google.com (Alan Viverette)
 32 */
 33public class PrefsActivity extends PreferenceActivity {
 34  private static final String TAG = "PrefsActivity";
 35
 36  protected static final String RESOLUTION = "resolution";
 37  protected static final String LANGUAGE = "language";
 38  protected static final String CLIPBOARD = "clipboard";
 39  protected static final String NORMALIZE = "normalize";
 40  protected static final String TEXTDETECT = "textdetect";
 41  protected static final String DEBUG = "debug";
 42
 43  protected static final String DEFAULT_RESOLUTION = "1024x768";
 44  protected static final String DEFAULT_LANGUAGE = null;
 45  protected static final boolean DEFAULT_CLIPBOARD = true;
 46  protected static final boolean DEFAULT_NORMALIZE = false;
 47  protected static final boolean DEFAULT_TEXTDETECT = false;
 48  protected static final boolean DEFAULT_DEBUG = false;
 49
 50  private Ocr mOcr;
 51
 52  @Override
 53  protected void onCreate(Bundle savedInstanceState) {
 54    super.onCreate(savedInstanceState);
 55
 56    addPreferencesFromResource(R.xml.prefs);
 57
 58    Preference temp_pref;
 59
 60    temp_pref = findPreference("language");
 61    temp_pref.setEnabled(false);
 62    temp_pref = findPreference("resolution");
 63    temp_pref.setEnabled(false);
 64
 65    Ocr.InitCallback onInit = new Ocr.InitCallback() {
 66      @Override
 67      public void onInitialized(int status) {
 68        if (status == Ocr.STATUS_SUCCESS) {
 69          loadLanguages();
 70        } else {
 71          Log.e(TAG, "OCR service initialized with status " + status);
 72        }
 73      }
 74    };
 75
 76    mOcr = new Ocr(this, onInit);
 77
 78    loadResolutions();
 79  }
 80
 81  @Override
 82  protected void onDestroy() {
 83    if (mOcr != null) {
 84      mOcr.release();
 85    }
 86
 87    super.onDestroy();
 88  }
 89
 90  private void loadResolutions() {
 91    Log.i(TAG, "Loading resolutions...");
 92
 93    ListPreference res_pref = (ListPreference) findPreference("resolution");
 94
 95    Camera camera = Camera.open();
 96    Camera.Parameters params = camera.getParameters();
 97    camera.release();
 98
 99    String current = params.get("picture-size");
100    String delimited = params.get("picture-size-values");
101    String[] values;
102
103    if (delimited == null || delimited.length() == 0) {
104      values = new String[1];
105      values[0] = current;
106    } else {
107      values = delimited.split(",");
108    }
109
110    res_pref.setEntries(values);
111    res_pref.setEntryValues(values);
112    res_pref.setEnabled(true);
113
114    Log.i(TAG, "Loaded resolutions!");
115  }
116
117  /**
118   * Loads languages from the Ocr object into the default language preference
119   * list.
120   */
121  private void loadLanguages() {
122    ListPreference lang_pref = (ListPreference) findPreference("language");
123    Language[] langs = mOcr.getLanguages();
124    String[] entries;
125    String[] values;
126
127    if (langs != null) {
128      entries = new String[langs.length];
129      values = new String[langs.length];
130
131      for (int i = 0; i < langs.length; i++) {
132        values[i] = langs[i].iso_639_2;
133        entries[i] = langs[i].english;
134      }
135    } else {
136      entries = new String[1];
137      values = new String[1];
138
139      entries[0] = getString(R.string.default_language);
140      values[0] = "";
141    }
142
143    lang_pref.setEntries(entries);
144    lang_pref.setEntryValues(values);
145    lang_pref.setEnabled(true);
146
147    mOcr.release();
148    mOcr = null;
149  }
150}