PageRenderTime 25ms CodeModel.GetById 13ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/ocr/translate/src/com/google/marvin/translate/CaptureActivity.java

http://eyes-free.googlecode.com/
Java | 198 lines | 157 code | 38 blank | 3 comment | 3 complexity | 61fe0b9ce58eebbb6ad7d324aa9f5b1b MD5 | raw file
  1package com.google.marvin.translate;
  2
  3import android.app.Activity;
  4import android.content.Intent;
  5import android.hardware.Camera;
  6import android.hardware.Camera.Size;
  7import android.os.Bundle;
  8import android.util.Log;
  9import android.view.KeyEvent;
 10import android.view.SurfaceHolder;
 11import android.view.SurfaceView;
 12import android.view.View;
 13import android.view.Window;
 14import android.view.WindowManager;
 15import android.widget.AdapterView;
 16import android.widget.ImageButton;
 17import android.widget.Spinner;
 18import android.widget.AdapterView.OnItemSelectedListener;
 19
 20import com.android.ocr.client.Language;
 21
 22import java.io.IOException;
 23
 24public class CaptureActivity extends Activity implements SurfaceHolder.Callback,
 25    Camera.AutoFocusCallback, Camera.PictureCallback {
 26  private static final String TAG = "CaptureActivity";
 27
 28  public static final String EXTRA_IMAGE = "image";
 29  public static final String EXTRA_WIDTH = "width";
 30  public static final String EXTRA_HEIGHT = "height";
 31  public static final String EXTRA_FORMAT = "format";
 32  public static final String EXTRA_OCR_SOURCE = "ocr_source";
 33
 34  private static final int DEFAULT_WIDTH = 1024;
 35  private static final int DEFAULT_HEIGHT = 768;
 36
 37  private int mParamWidth;
 38  private int mParamHeight;
 39
 40  private Camera mCameraDevice;
 41  private SurfaceView mSurfaceView;
 42  private SurfaceHolder mSurfaceHolder = null;
 43  private Spinner mOcrSource;
 44  private ImageButton mShutter;
 45  private boolean mBusy;
 46
 47  @Override
 48  public void onCreate(Bundle savedInstanceState) {
 49    super.onCreate(savedInstanceState);
 50
 51    // To reduce startup time, we open camera device in another thread.
 52    // We make sure the camera is opened at the end of onCreate.
 53    Thread openCameraThread = new Thread(new Runnable() {
 54      public void run() {
 55        try {
 56          mCameraDevice = Camera.open();
 57        } catch (RuntimeException e) {
 58          Log.e(TAG, "Failed to open camera: " + e.toString());
 59        }
 60      }
 61    });
 62    openCameraThread.start();
 63
 64    Window win = getWindow();
 65    win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
 66
 67    setContentView(R.layout.capture);
 68
 69    mSurfaceView = (SurfaceView) findViewById(R.id.preview);
 70    mSurfaceHolder = mSurfaceView.getHolder();
 71    mSurfaceHolder.addCallback(this);
 72    mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
 73
 74    mOcrSource = (Spinner) findViewById(R.id.ocr_source);
 75
 76    View.OnClickListener onClick = new View.OnClickListener() {
 77      @Override
 78      public void onClick(View v) {
 79        takePicture();
 80      }
 81    };
 82    mShutter = (ImageButton) findViewById(R.id.btn_shutter);
 83    mShutter.setOnClickListener(onClick);
 84
 85    processIntent(getIntent());
 86
 87    try {
 88      openCameraThread.join();
 89    } catch (InterruptedException e) {
 90      Log.e(TAG, e.toString());
 91    }
 92  }
 93
 94  @Override
 95  protected void onDestroy() {
 96    mCameraDevice.release();
 97
 98    super.onDestroy();
 99  }
100
101  @Override
102  public boolean onKeyDown(int keyCode, KeyEvent event) {
103    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
104
105    switch (keyCode) {
106      case KeyEvent.KEYCODE_DPAD_CENTER:
107      case KeyEvent.KEYCODE_CAMERA:
108        takePicture();
109        return true;
110    }
111
112    return super.onKeyDown(keyCode, event);
113  }
114
115  @Override
116  public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
117    Camera.Parameters parameters = mCameraDevice.getParameters();
118    parameters.setPreviewSize(w, h);
119    parameters.setPictureSize(mParamHeight, mParamWidth);
120    mCameraDevice.setParameters(parameters);
121
122    if (holder.isCreating()) {
123      mCameraDevice.startPreview();
124    }
125  }
126
127  @Override
128  public void surfaceCreated(SurfaceHolder holder) {
129    try {
130      mCameraDevice.setPreviewDisplay(holder);
131    } catch (IOException e) {
132      Log.e(TAG, e.toString());
133    }
134  }
135
136  @Override
137  public void surfaceDestroyed(SurfaceHolder holder) {
138    mCameraDevice.stopPreview();
139  }
140
141  @Override
142  public void onAutoFocus(boolean success, Camera camera) {
143    Camera.Parameters parameters = camera.getParameters();
144    parameters.setPictureSize(mParamWidth, mParamHeight);
145    parameters.set("orientation", "landscape");
146
147    camera.setParameters(parameters);
148    camera.takePicture(null, null, this);
149  }
150
151  @Override
152  public void onPictureTaken(byte[] image, Camera camera) {
153    Language ocr_source = (Language) mOcrSource.getSelectedItem();
154    Camera.Parameters params = camera.getParameters();
155    Size size = params.getPictureSize();
156
157    Intent data = new Intent();
158    data.putExtra(CaptureActivity.EXTRA_IMAGE, image);
159    data.putExtra(CaptureActivity.EXTRA_WIDTH, size.width);
160    data.putExtra(CaptureActivity.EXTRA_HEIGHT, size.height);
161    data.putExtra(CaptureActivity.EXTRA_FORMAT, params.getPictureFormat());
162    data.putExtra(CaptureActivity.EXTRA_OCR_SOURCE, ocr_source.iso_639_2);
163
164    setResult(RESULT_OK, data);
165    finish();
166
167    mBusy = false;
168  }
169
170  private void processIntent(Intent intent) {
171    mParamWidth = intent.getIntExtra(EXTRA_WIDTH, DEFAULT_WIDTH);
172    mParamHeight = intent.getIntExtra(EXTRA_HEIGHT, DEFAULT_HEIGHT);
173
174    OnItemSelectedListener onSelected = new OnItemSelectedListener() {
175      @Override
176      public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
177        Translate.mSource.setSelection(position);
178      }
179
180      @Override
181      public void onNothingSelected(AdapterView<?> arg0) {
182        // do... nothing!
183      }
184    };
185
186    Spinner mSource = Translate.mSource;
187    mOcrSource.setAdapter(mSource.getAdapter());
188    mOcrSource.setSelection(mSource.getSelectedItemPosition());
189    mOcrSource.setOnItemSelectedListener(onSelected);
190  }
191
192  private void takePicture() {
193    if (mBusy) return;
194
195    mBusy = true;
196    mCameraDevice.autoFocus(this);
197  }
198}