文章目录
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
class Preview extends ViewGroup implements SurfaceHolder.Callback {
SurfaceView mSurfaceView;
SurfaceHolder mHolder;

Perview(Context context){
super(context);

mSurfaceView = new SurfaceView(context);
addView(mSurfaceView);
mHolder = mSurfaceView.getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

}

public void setCamera(Camera camera) {
if (mCamera == camera) { return; }

stopPreviewAndFreeCamera();

mCamera = camera;

if (mCamera != null) {
List<Size> localSizes = mCamera.getParameters().getSupportedPreviewSizes();
mSupportedPreviewSizes = localSizes;
requestLayout();

try {
mCamera.setPreviewDisplay(mHolder);
} catch (IOException e) {
e.printStackTrace();
}
mCamera.startPreview();
}
}
// 修改相机设置
public void surfaceChanged(SurfaceHolder holder,int format,int w,int h) {
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(mPreviewSize.with,mPreviewSize.height);
requestLayout();
mCamera.setParameters(parameters);
mCamera.startPreview();
}
// 设置预览方向 setCameraDisplayOrientation()方法 Android Api level 14之前 必须先体质预览

// 拍摄照片
Camera.takePicture()拍下图片
创建Camera.PictureCallback与Camera.ShutterCallback 对象并传递到Camera.takePocture()中
如果想做连拍动作,可以创建一个Camera.PreviewCallback并实现onPerviewFrame()
// 停止预览并释放相机
public void surfaceDestroyed(SurfaceHolder holder) {
if (mCamera != null) {
mCamera.stopPreview();
}
}
public void stopPreviewAndFreeCamera() {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
文章目录