SHOW:
|
|
- or go back to the newest paste.
1 | - | package com.blutechnologies.scancard; |
1 | + | |
2 | ||
3 | - | import java.io.IOException; |
3 | + | static final String TAG="CameraPreview"; |
4 | - | import java.util.ArrayList; |
4 | + | |
5 | - | import java.util.List; |
5 | + | |
6 | SurfaceHolder mHolder; | |
7 | - | import android.annotation.TargetApi; |
7 | + | |
8 | - | import android.app.Activity; |
8 | + | |
9 | - | import android.content.Context; |
9 | + | |
10 | - | import android.hardware.Camera; |
10 | + | |
11 | - | import android.hardware.Camera.Parameters; |
11 | + | |
12 | - | import android.hardware.Camera.Size; |
12 | + | |
13 | - | import android.os.Build; |
13 | + | |
14 | - | import android.util.Log; |
14 | + | |
15 | - | import android.view.Surface; |
15 | + | |
16 | - | import android.view.SurfaceHolder; |
16 | + | |
17 | - | import android.view.SurfaceView; |
17 | + | |
18 | ||
19 | @Override | |
20 | public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { | |
21 | if(holder.getSurface()==null) | |
22 | { | |
23 | return; | |
24 | } | |
25 | try | |
26 | { | |
27 | mCamera.stopPreview(); | |
28 | } | |
29 | catch(Exception e) | |
30 | { | |
31 | Log.e("CamTestActivity","Tried to shut down non existant preview",e); | |
32 | } | |
33 | //Set orientation and display size here: | |
34 | Parameters params=mCamera.getParameters(); | |
35 | Size previewSize=getBestPreviewSize(params); | |
36 | Size desiredPictureSize=getDesiredPictureSize(params); | |
37 | if(holder!=null && hasMacroFocus(params)) | |
38 | params.setFocusMode(Parameters.FOCUS_MODE_MACRO);//only added in API 14... | |
39 | else if(holder!=null && hasAutoFocus(params)){ | |
40 | params.setFocusMode(Parameters.FOCUS_MODE_AUTO); | |
41 | } | |
42 | else | |
43 | { | |
44 | Log.d("CameraPreview", "The holder has not been created yet or there is no autofocus"); | |
45 | } | |
46 | ||
47 | params.setWhiteBalance(Camera.Parameters.WHITE_BALANCE_AUTO); | |
48 | setCameraDisplayOrientation(ctx,Camera.CameraInfo.CAMERA_FACING_BACK, mCamera); | |
49 | params.setPreviewSize(previewSize.width, previewSize.height); | |
50 | params.setPictureSize(desiredPictureSize.width, desiredPictureSize.height); | |
51 | mCamera.setParameters(params); | |
52 | try { | |
53 | mCamera.setPreviewDisplay(holder); | |
54 | } catch (IOException e) { | |
55 | Log.e("CameraPreview", "Camera preview displayed could not be changed to mHolder",e); | |
56 | } | |
57 | mCamera.startPreview(); | |
58 | } | |
59 | ||
60 | @Override | |
61 | public void surfaceCreated(SurfaceHolder holder) { | |
62 | try | |
63 | { | |
64 | if(mCamera==null) | |
65 | { | |
66 | Log.e(TAG,"Camera is null here"); | |
67 | } | |
68 | if(holder==null) | |
69 | { | |
70 | Log.d(TAG,"Holder provided by surfaceCreated is null"); | |
71 | } | |
72 | mCamera.setPreviewDisplay(mHolder); | |
73 | mCamera.startPreview(); | |
74 | } | |
75 | catch(IOException e) | |
76 | { | |
77 | Log.e("CameraPreview","Error getting camera preview",e); | |
78 | } | |
79 | } | |
80 | ||
81 | @Override | |
82 | public void surfaceDestroyed(SurfaceHolder holder) { | |
83 | // TODO Auto-generated method stub | |
84 | //empty method...take care of this in your Activity | |
85 | } | |
86 | ||
87 | private Size getDesiredPictureSize(Parameters params) | |
88 | { | |
89 | //Resolution is widthxheight | |
90 | Size result=null; | |
91 | //boolean isDesiredValue=false; | |
92 | final int minArea=500*500; | |
93 | final int maxArea=1000*1000; | |
94 | List<Size> supportedSizeList=params.getSupportedPictureSizes(); | |
95 | for(Size size:supportedSizeList) | |
96 | { | |
97 | if(size.width*size.height>minArea && size.width*size.height<maxArea) | |
98 | { | |
99 | if(result==null) | |
100 | result=size; | |
101 | else | |
102 | { | |
103 | int resultArea=result.width*result.height; | |
104 | int sizeArea=size.width*size.height; | |
105 | if(resultArea<sizeArea) | |
106 | { | |
107 | result=size; | |
108 | } | |
109 | } | |
110 | } | |
111 | } | |
112 | return result; | |
113 | } | |
114 | ||
115 | private Size getBestPreviewSize(Parameters params) | |
116 | { | |
117 | Size result=null; | |
118 | ArrayList<Size> mSupportedSizes=(ArrayList<Size>) params.getSupportedPreviewSizes(); | |
119 | for(Size size:mSupportedSizes) | |
120 | { | |
121 | ||
122 | if(result==null) | |
123 | result=size; | |
124 | else | |
125 | { | |
126 | int resultArea=result.width*result.height; | |
127 | int sizeArea=size.width*size.height; | |
128 | if(resultArea<sizeArea) | |
129 | result=size; | |
130 | } | |
131 | ||
132 | } | |
133 | Log.d("CameraPreview", "Camera Preview size set to "+result.width+"X"+result.height); | |
134 | return result; | |
135 | } | |
136 | ||
137 | public boolean hasMacroFocus(Parameters params) | |
138 | { | |
139 | List<String> supportedFocusModes=params.getSupportedFocusModes(); | |
140 | if(supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_MACRO)) | |
141 | return true; | |
142 | return false; | |
143 | } | |
144 | ||
145 | public boolean hasAutoFocus(Parameters params) | |
146 | { | |
147 | List<String> focusModes=params.getSupportedFocusModes(); | |
148 | ||
149 | if(focusModes!=null && focusModes.contains(Parameters.FOCUS_MODE_AUTO)){ | |
150 | return true; | |
151 | } | |
152 | else if(focusModes==null) | |
153 | { | |
154 | Log.d("CameraPreview", "Error getting autofocus mode list"); | |
155 | } | |
156 | return false; | |
157 | } | |
158 | ||
159 | public boolean hasAutoFlash(Parameters params) | |
160 | { | |
161 | List<String> supportedFlashModes=params.getSupportedFlashModes(); | |
162 | if(supportedFlashModes.contains(Parameters.FLASH_MODE_AUTO)) | |
163 | return true; | |
164 | return false; | |
165 | } | |
166 | ||
167 | ||
168 | //I have set orientation to landscape...if orientation issues persist...use this | |
169 | //code from developer.android.com-javadoc | |
170 | @TargetApi(Build.VERSION_CODES.GINGERBREAD) | |
171 | public static void setCameraDisplayOrientation(Context context, | |
172 | int cameraId, android.hardware.Camera camera) { | |
173 | android.hardware.Camera.CameraInfo info = | |
174 | new android.hardware.Camera.CameraInfo(); | |
175 | android.hardware.Camera.getCameraInfo(cameraId, info); | |
176 | int rotation = ((Activity) context).getWindowManager().getDefaultDisplay() | |
177 | .getRotation(); | |
178 | int degrees = 0; | |
179 | switch (rotation) { | |
180 | case Surface.ROTATION_0: degrees = 0; break; | |
181 | case Surface.ROTATION_90: degrees = 90; break; | |
182 | case Surface.ROTATION_180: degrees = 180; break; | |
183 | case Surface.ROTATION_270: degrees = 270; break; | |
184 | } | |
185 | ||
186 | int result; | |
187 | if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { | |
188 | result = (info.orientation + degrees) % 360; | |
189 | result = (360 - result) % 360; // compensate the mirror | |
190 | } else { // back-facing | |
191 | result = (info.orientation - degrees + 360) % 360; | |
192 | } | |
193 | camera.setDisplayOrientation(result); | |
194 | Camera.Parameters params=camera.getParameters(); | |
195 | params.setRotation(result); | |
196 | camera.setParameters(params); | |
197 | } | |
198 | } |