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.hardware.Camera.Size; 23 import android.os.Bundle; 24 import android.view.SurfaceHolder; 25 import android.view.SurfaceView; 26 import android.view.Window; 27 28 import java.io.IOException; 29 import java.util.List; 30 31 // ---------------------------------------------------------------------- 32 33 public class CameraPreview extends Activity { 34 private Preview mPreview; 35 36 @Override 37 protected void onCreate(Bundle savedInstanceState) { 38 super.onCreate(savedInstanceState); 39 40 // Hide the window title. 41 requestWindowFeature(Window.FEATURE_NO_TITLE); 42 43 // Create our Preview view and set it as the content of our activity. 44 mPreview = new Preview(this); 45 setContentView(mPreview); 46 } 47 48 } 49 50 // ---------------------------------------------------------------------- 51 52 class Preview extends SurfaceView implements SurfaceHolder.Callback { 53 SurfaceHolder mHolder; 54 Camera mCamera; 55 56 Preview(Context context) { 57 super(context); 58 59 // Install a SurfaceHolder.Callback so we get notified when the 60 // underlying surface is created and destroyed. 61 mHolder = getHolder(); 62 mHolder.addCallback(this); 63 mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 64 } 65 66 public void surfaceCreated(SurfaceHolder holder) { 67 // The Surface has been created, acquire the camera and tell it where 68 // to draw. 69 mCamera = Camera.open(); 70 try { 71 mCamera.setPreviewDisplay(holder); 72 } catch (IOException exception) { 73 mCamera.release(); 74 mCamera = null; 75 // TODO: add more exception handling logic here 76 } 77 } 78 79 public void surfaceDestroyed(SurfaceHolder holder) { 80 // Surface will be destroyed when we return, so stop the preview. 81 // Because the CameraDevice object is not a shared resource, it's very 82 // important to release it when the activity is paused. 83 mCamera.stopPreview(); 84 mCamera.release(); 85 mCamera = null; 86 } 87 88 89 private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) { 90 final double ASPECT_TOLERANCE = 0.05; 91 double targetRatio = (double) w / h; 92 if (sizes == null) return null; 93 94 Size optimalSize = null; 95 double minDiff = Double.MAX_VALUE; 96 97 int targetHeight = h; 98 99 // Try to find an size match aspect ratio and size 100 for (Size size : sizes) { 101 double ratio = (double) size.width / size.height; 102 if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue; 103 if (Math.abs(size.height - targetHeight) < minDiff) { 104 optimalSize = size; 105 minDiff = Math.abs(size.height - targetHeight); 106 } 107 } 108 109 // Cannot find the one match the aspect ratio, ignore the requirement 110 if (optimalSize == null) { 111 minDiff = Double.MAX_VALUE; 112 for (Size size : sizes) { 113 if (Math.abs(size.height - targetHeight) < minDiff) { 114 optimalSize = size; 115 minDiff = Math.abs(size.height - targetHeight); 116 } 117 } 118 } 119 return optimalSize; 120 } 121 122 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { 123 // Now that the size is known, set up the camera parameters and begin 124 // the preview. 125 Camera.Parameters parameters = mCamera.getParameters(); 126 127 List<Size> sizes = parameters.getSupportedPreviewSizes(); 128 Size optimalSize = getOptimalPreviewSize(sizes, w, h); 129 parameters.setPreviewSize(optimalSize.width, optimalSize.height); 130 131 mCamera.setParameters(parameters); 132 mCamera.startPreview(); 133 } 134 135 } 136