1 /*
2  * Copyright (C) 2013 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 package com.android.cts.verifier.sensors;
17 
18 // ----------------------------------------------------------------------
19 
20 import android.content.Context;
21 import android.hardware.Camera;
22 import android.util.AttributeSet;
23 import android.util.Log;
24 import android.view.SurfaceHolder;
25 import android.view.SurfaceView;
26 import android.view.ViewGroup;
27 
28 import java.io.IOException;
29 
30 /** Camera preview class */
31 public class RVCVCameraPreview extends SurfaceView implements SurfaceHolder.Callback {
32     private static final String TAG = "RVCVCameraPreview";
33     private static final boolean LOCAL_LOGD = true;
34 
35     private SurfaceHolder mHolder;
36     private Camera mCamera;
37     private float mAspect;
38     private int mRotation;
39 
40     /**
41      * Constructor
42      * @param context Activity context
43      */
RVCVCameraPreview(Context context)44     public RVCVCameraPreview(Context context) {
45         super(context);
46         mCamera = null;
47         initSurface();
48     }
49 
50     /**
51      * Constructor
52      * @param context Activity context
53      * @param attrs
54      */
RVCVCameraPreview(Context context, AttributeSet attrs)55     public RVCVCameraPreview(Context context, AttributeSet attrs) {
56         super(context, attrs);
57     }
58 
init(Camera camera, float aspectRatio, int rotation)59     public void init(Camera camera, float aspectRatio, int rotation)  {
60         this.mCamera = camera;
61         mAspect = aspectRatio;
62         mRotation = rotation;
63         initSurface();
64     }
65 
initSurface()66     private void initSurface() {
67         // Install a SurfaceHolder.Callback so we get notified when the
68         // underlying surface is created and destroyed.
69         mHolder = getHolder();
70         mHolder.addCallback(this);
71 
72         // deprecated
73         // TODO: update this code to match new API level.
74         mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
75     }
76 
77     /**
78      *  SurfaceHolder.Callback
79      *  Surface is created, it is OK to start the camera preview now.
80      */
surfaceCreated(SurfaceHolder holder)81     public void surfaceCreated(SurfaceHolder holder) {
82         // The Surface has been created, now tell the camera where to draw the preview.
83 
84         if (mCamera == null) {
85             // preview camera does not exist
86             return;
87         }
88 
89         try {
90             mCamera.setPreviewDisplay(holder);
91             mCamera.startPreview();
92             int v_height = getHeight();
93             int v_width = getWidth();
94             ViewGroup.LayoutParams layout = getLayoutParams();
95             if ( (float)v_height/v_width  >
96                     mAspect) {
97                 layout.height = (int)(v_width * mAspect);
98                 layout.width = v_width;
99             }else {
100                 layout.width = (int)(v_height / mAspect);
101                 layout.height = v_height;
102             }
103             Log.d(TAG, String.format("Layout (%d, %d) -> (%d, %d)", v_width, v_height,
104                     layout.width, layout.height));
105             setLayoutParams(layout);
106         } catch (IOException e) {
107             if (LOCAL_LOGD) Log.d(TAG, "Error when starting camera preview: " + e.getMessage());
108         }
109     }
110     /**
111      *  SurfaceHolder.Callback
112      */
surfaceDestroyed(SurfaceHolder holder)113     public void surfaceDestroyed(SurfaceHolder holder) {
114         // empty. Take care of releasing the Camera preview in your activity.
115     }
116 
117     /**
118      *  SurfaceHolder.Callback
119      *  Restart camera preview if surface changed
120      */
surfaceChanged(SurfaceHolder holder, int format, int w, int h)121     public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
122 
123         if (mHolder.getSurface() == null || mCamera == null){
124             // preview surface or camera does not exist
125             return;
126         }
127 
128         // stop preview before making changes
129         mCamera.stopPreview();
130 
131         mCamera.setDisplayOrientation(mRotation);
132 
133         //do the same as if it is created again
134         surfaceCreated(holder);
135     }
136 }
137