/
CameraView.java
143 lines (127 loc) · 4.24 KB
/
CameraView.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
package apc.examples;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import android.content.Context;
import android.content.res.Configuration;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ZoomControls;
public class CameraView extends SurfaceView implements SurfaceHolder.Callback {
SurfaceHolder mHolder;
int width;
int height;
ZoomControls _zoomControls;
int currentZoomLevel = 0, maxZoomLevel = 0;
Camera mCamera;
public CameraView(Context context, AttributeSet attrs) {
super(context, attrs);
holderCreation();
}
public CameraView(Context context) {
super(context);
holderCreation();
}
public void setZoomControls(ZoomControls zoomControls) {
_zoomControls = zoomControls;
}
public void takePicture(Camera.ShutterCallback shutter,
Camera.PictureCallback raw, Camera.PictureCallback postview,
Camera.PictureCallback jpeg) {
mCamera.takePicture(shutter, raw, postview, jpeg);
}
public void holderCreation() {
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, acquire the camera and tell it where to
// draw.
mCamera = Camera.open();
Parameters params = mCamera.getParameters();
mCamera.setDisplayOrientation(90);
params.set("orientation", "portait");
// If we aren't landscape (the default), tell the camera we want
// portrait mode
if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
params.set("orientation", "portrait"); // "landscape"
// And Rotate the final picture if possible
// This works on 2.0 and higher only
// params.setRotation(90);
// Use reflection to see if it exists and to call it so you can
// support older versions
try {
Method rotateSet = Camera.Parameters.class.getMethod(
"setRotation", new Class[] { Integer.TYPE });
Object arguments[] = new Object[] { new Integer(90) };
rotateSet.invoke(params, arguments);
} catch (NoSuchMethodException nsme) {
// Older Device
Log.v("CAMERAVIEW", "No Set Rotation");
} catch (IllegalArgumentException e) {
Log.v("CAMERAVIEW", "Exception IllegalArgument");
} catch (IllegalAccessException e) {
Log.v("CAMERAVIEW", "Illegal Access Exception");
} catch (InvocationTargetException e) {
Log.v("CAMERAVIEW", "Invocation Target Exception");
}
}
mCamera.setParameters(params);
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException exception) {
mCamera.release();
mCamera = null;
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// Surface will be destroyed when we return, so stop the preview.
// Because the CameraDevice object is not a shared resource, it's very
// important to release it when the activity is paused.
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
width = w;
height = h;
// Now that the size is known, set up the camera parameters and begin
// the preview.
Camera.Parameters parameters = mCamera.getParameters();
// parameters.setPreviewSize(w, h);
mCamera.setParameters(parameters);
mCamera.startPreview();
if (parameters.isZoomSupported()) {
maxZoomLevel = parameters.getMaxZoom();
_zoomControls.setIsZoomInEnabled(true);
_zoomControls.setIsZoomOutEnabled(true);
_zoomControls.setOnZoomInClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (currentZoomLevel < 500) {
currentZoomLevel++;
mCamera.startSmoothZoom(currentZoomLevel);
}
}
});
_zoomControls.setOnZoomOutClickListener(new OnClickListener() {
public void onClick(View v) {
if (currentZoomLevel > 0) {
currentZoomLevel--;
mCamera.startSmoothZoom(currentZoomLevel);
}
}
});
}
else {
_zoomControls.hide();
}
}
}