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