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

/ocr/ocrservice/src/com/googlecode/eyesfree/ocr/intent/CameraActivity.java

http://eyes-free.googlecode.com/
Java | 122 lines | 68 code | 22 blank | 32 comment | 3 complexity | d4bba29d047f91fcf2035a92df42a8e2 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
 17package com.googlecode.eyesfree.ocr.intent;
 18
 19import android.app.Activity;
 20import android.os.Bundle;
 21import android.view.SurfaceHolder;
 22import android.view.SurfaceView;
 23import android.view.WindowManager.LayoutParams;
 24
 25import com.googlecode.eyesfree.ocr.R;
 26
 27import java.io.IOException;
 28
 29/**
 30 * A basic activity capable of rendering a camera preview as its background.
 31 *
 32 * @author alanv@google.com (Alan Viverette)
 33 */
 34public abstract class CameraActivity extends Activity {
 35    /** Used for controlling the camera. */
 36    private CameraManager mCameraManager;
 37
 38    /** Whether the preview is ready for drawing. */
 39    private boolean mHasSurface;
 40
 41    @Override
 42    protected void onCreate(Bundle savedInstanceState) {
 43        super.onCreate(savedInstanceState);
 44
 45        mCameraManager = new CameraManager(this);
 46        mHasSurface = false;
 47
 48        getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);
 49    }
 50
 51    @Override
 52    protected void onResume() {
 53        super.onResume();
 54
 55        SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview);
 56        SurfaceHolder surfaceHolder = surfaceView.getHolder();
 57
 58        if (mHasSurface) {
 59            initializeCamera(surfaceHolder);
 60        } else {
 61            surfaceHolder.addCallback(surfaceHolderCallback);
 62            surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
 63        }
 64    }
 65
 66    @Override
 67    protected void onPause() {
 68        synchronized (mCameraManager) {
 69            mHasSurface = false;
 70            mCameraManager.closeDriver();
 71        }
 72
 73        super.onPause();
 74    }
 75
 76    /**
 77     * @return the camera manager
 78     */
 79    protected CameraManager getCameraManager() {
 80        return mCameraManager;
 81    }
 82
 83    /**
 84     * Initializes the camera by opening the surface holder, starting preview,
 85     * and starting focusing.
 86     *
 87     * @param surfaceHolder
 88     */
 89    private void initializeCamera(SurfaceHolder surfaceHolder) {
 90        try {
 91            mCameraManager.openDriver(surfaceHolder);
 92            mCameraManager.startPreview();
 93
 94            onCameraStarted();
 95        } catch (IOException e) {
 96            e.printStackTrace();
 97        }
 98    }
 99
100    private final SurfaceHolder.Callback surfaceHolderCallback = new SurfaceHolder.Callback() {
101        @Override
102        public void surfaceCreated(SurfaceHolder holder) {
103            if (!mHasSurface) {
104                mHasSurface = true;
105
106                initializeCamera(holder);
107            }
108        }
109
110        @Override
111        public void surfaceDestroyed(SurfaceHolder holder) {
112            mHasSurface = false;
113        }
114
115        @Override
116        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
117            // Do nothing
118        }
119    };
120
121    protected abstract void onCameraStarted();
122}