1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of 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, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.apis.graphics; 18 19 import android.app.Activity; 20 import android.content.Context; 21 import android.hardware.Camera; 22 import android.os.Bundle; 23 import android.view.SurfaceHolder; 24 import android.view.SurfaceView; 25 import android.view.Window; 26 import java.io.IOException; 27 28 // ---------------------------------------------------------------------- 29 30 public class CameraPreview extends Activity { 31 private Preview mPreview; 32 33 @Override onCreate(Bundle savedInstanceState)34 protected void onCreate(Bundle savedInstanceState) { 35 super.onCreate(savedInstanceState); 36 37 // Hide the window title. 38 requestWindowFeature(Window.FEATURE_NO_TITLE); 39 40 // Create our Preview view and set it as the content of our activity. 41 mPreview = new Preview(this); 42 setContentView(mPreview); 43 } 44 45 } 46 47 // ---------------------------------------------------------------------- 48 49 class Preview extends SurfaceView implements SurfaceHolder.Callback { 50 SurfaceHolder mHolder; 51 Camera mCamera; 52 Preview(Context context)53 Preview(Context context) { 54 super(context); 55 56 // Install a SurfaceHolder.Callback so we get notified when the 57 // underlying surface is created and destroyed. 58 mHolder = getHolder(); 59 mHolder.addCallback(this); 60 mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 61 } 62 surfaceCreated(SurfaceHolder holder)63 public void surfaceCreated(SurfaceHolder holder) { 64 // The Surface has been created, acquire the camera and tell it where 65 // to draw. 66 mCamera = Camera.open(); 67 try { 68 mCamera.setPreviewDisplay(holder); 69 } catch (IOException exception) { 70 mCamera.release(); 71 mCamera = null; 72 // TODO: add more exception handling logic here 73 } 74 } 75 surfaceDestroyed(SurfaceHolder holder)76 public void surfaceDestroyed(SurfaceHolder holder) { 77 // Surface will be destroyed when we return, so stop the preview. 78 // Because the CameraDevice object is not a shared resource, it's very 79 // important to release it when the activity is paused. 80 mCamera.stopPreview(); 81 mCamera.release(); 82 mCamera = null; 83 } 84 surfaceChanged(SurfaceHolder holder, int format, int w, int h)85 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { 86 // Now that the size is known, set up the camera parameters and begin 87 // the preview. 88 Camera.Parameters parameters = mCamera.getParameters(); 89 parameters.setPreviewSize(w, h); 90 mCamera.setParameters(parameters); 91 mCamera.startPreview(); 92 } 93 94 } 95