/
CameraSurfaceView.java
284 lines (234 loc) · 8.4 KB
/
CameraSurfaceView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package com.elk.wxplayer.record;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.os.Environment;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import com.elk.wxplayer.CameraHelper;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
/**
* author maimingliang
*/
public class CameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
private Context mContext;
private SurfaceHolder mSurfaceHolder;
private Camera mCamera;
private boolean isBackCameraOn = true;
private boolean mIsPortrait = true;
private int mSurfaceWidth;
private int mSurfaceHeight;
public CameraSurfaceView(Context context) {
this(context,null);
}
public CameraSurfaceView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public CameraSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
init();
}
private void init() {
mSurfaceHolder = getHolder();
mSurfaceHolder.addCallback(this);
}
public void start(){
if(mCamera == null && checkCameraHardware(mContext)){
mCamera = getCamera();
setStartPreview(mCamera,mSurfaceHolder);
}
}
/** Check if this device has a camera */
private boolean checkCameraHardware(Context context) {
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
// this device has a camera
return true;
} else {
// no camera on this device
return false;
}
}
public void switchCamera() {
int cameraCount;
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
cameraCount = Camera.getNumberOfCameras();
//遍历可用摄像头
for(int i = 0 ;i < cameraCount;i++){
Camera.getCameraInfo(i, cameraInfo);
if(isBackCameraOn){
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
releaseCamera();
mCamera = Camera.open(i);
setStartPreview(mCamera, mSurfaceHolder);
isBackCameraOn = false;
break;
}
}else{
if(cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK){
releaseCamera();
mCamera = Camera.open(i);
setStartPreview(mCamera, mSurfaceHolder);
isBackCameraOn = true;
break;
}
}
}
}
public void autoFocus(){
if (mCamera != null) {
mCamera.autoFocus(null);
}
}
public void capture(View view) {
//自动对焦
mCamera.autoFocus(new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
Log.e("autoFocus", "----- success = " + success);
if (success) {
mCamera.takePicture(null,null,null,mPictureCallback);
}
}
});
}
@Override
protected void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
////竖屏显示
mIsPortrait = true;
} else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
//横屏显示
mIsPortrait = false;
}
}
public void setCameraParams(int width, int height){
// mSurfaceHolder.setFixedSize(width, height);
Camera.Parameters parameters = mCamera.getParameters();
List<Camera.Size> supportedPictureSizes = parameters.getSupportedPictureSizes();
List<Camera.Size> videoSizes = parameters.getSupportedVideoSizes();
Camera.Size optimalVideoSize = CameraHelper.getOptimalVideoSize(videoSizes, supportedPictureSizes, width, height);
//
// parameters.setPreviewFpsRange(4,10); //fps
// parameters.setJpegQuality(100);
parameters.setPictureFormat(ImageFormat.JPEG);
parameters.setPreviewSize(optimalVideoSize.width, optimalVideoSize.height);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
mCamera.setParameters(parameters);
}
private Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File file = getOutputMediaFile();
if (file == null) {
return;
}
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(data);
fos.close();
Log.e("onPictureTaken", " ----- fos -=== " + file.getAbsolutePath());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
};
/**
* 获取图片保持路径
*
* @return pic Path
*/
private File getOutputMediaFile() {
File mediaStorageDir = new File(Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyCameraApp");
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
return null;
}
}
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator + "temp.png");
return mediaFile;
}
/**
* 使用Camera.open()时始终检查异常。如果相机正在使用或不存在,则无法检查异常,将导致您的应用程序被系统关闭。
* @return
*/
public Camera getCamera(){
Camera camera = null;
try{
camera = Camera.open();
}catch (Exception e){
camera = null;
}
return camera;
}
/**
* 释放相机
*/
public void releaseCamera(){
if(mCamera != null){
mCamera.setPreviewCallback(null);
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
public void setStartPreview(Camera camera, SurfaceHolder surfaceHolder) {
try {
camera.setPreviewDisplay(surfaceHolder);
if(mIsPortrait){
camera.setDisplayOrientation(90);
}else{
camera.setDisplayOrientation(0);
}
camera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
setStartPreview(mCamera, holder);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Log.e("surfaceChanged", " ---- width = " + width + " height = " + height);
Log.e("surfaceChanged", "-----> getDeviceHeight = " + getDeviceHeight());
mSurfaceWidth = width;
mSurfaceHeight = height;
if (mSurfaceHolder.getSurface() == null || mCamera == null) {
return;
}
try {
mCamera.stopPreview();
}catch (Exception e){
e.printStackTrace();
}
// Camera.Parameters parameters = mCamera.getParameters();
// List<Camera.Size> supportedPictureSizes = parameters.getSupportedPictureSizes();
// Camera.Size size = getCloselyPreSize(width, height, supportedPictureSizes);
// setCameraParams(mIsPortrait == true ? size.height : size.width, mIsPortrait == true ? size.width : size.height);
setStartPreview(mCamera,mSurfaceHolder);
setCameraParams(width, height);
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
releaseCamera();
}
public int getDeviceHeight(){
return mContext.getResources().getDisplayMetrics().heightPixels;
}
}