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
package com.hand.face.myinterface;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ImageFormat;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.hardware.Camera.Size;
import android.util.Log;
import android.view.SurfaceHolder;
import com.hand.face.ui.FaceCompareActivity;
import com.hand.face.utils.CamParaUtil;
import com.hand.face.utils.FileUtil;
import com.hand.face.utils.ImageUtil;
import java.io.IOException;
import java.util.List;
/**
* Created by USER on 2016/12/10.
*/
public class CameraInterface {
private static final String TAG = "CameraInterface";
private Camera mCamera;
private Camera.Parameters mParams;
private boolean isPreviewing = false;
private float mPreviwRate = -1f;
private int mCameraId = -1;
private boolean isGoolgeFaceDetectOn = false;
private static CameraInterface mCameraInterface;
private FaceCompareActivity.NV21ImgCallBack imgCallBack;
public interface CamOpenOverCallback{
public void cameraHasOpened();
}
private CameraInterface(){
}
public static synchronized CameraInterface getInstance(){
if(mCameraInterface == null){
mCameraInterface = new CameraInterface();
}
return mCameraInterface;
}
public void setCallBack(FaceCompareActivity.NV21ImgCallBack callBack){
imgCallBack = callBack;
}
/**打开Camera
* @param callback
*/
public void doOpenCamera(CamOpenOverCallback callback, int cameraId){
Log.i(TAG, "Camera open....");
try {
mCamera = Camera.open(cameraId);
mCameraId = cameraId;
if(callback != null){
callback.cameraHasOpened();
}
}catch (Exception e){
}
}
/**开启预览
* @param holder
* @param previewRate
*/
public void doStartPreview(SurfaceHolder holder, float previewRate){
Log.i(TAG, "doStartPreview...");
if(isPreviewing){
mCamera.stopPreview();
return;
}
if(mCamera != null){
mParams = mCamera.getParameters();
mParams.setPictureFormat(PixelFormat.JPEG);//设置拍照后存储的图片格式
CamParaUtil.getInstance().printSupportPictureSize(mParams);
CamParaUtil.getInstance().printSupportPreviewSize(mParams);
//设置PreviewSize和PictureSize
Size pictureSize = CamParaUtil.getInstance().getPropPictureSize(
mParams.getSupportedPictureSizes(),previewRate, 800);
mParams.setPictureSize(pictureSize.width, pictureSize.height);
Size previewSize = CamParaUtil.getInstance().getPropPreviewSize(
mParams.getSupportedPreviewSizes(), previewRate, 800);
mParams.setPreviewSize(previewSize.width, previewSize.height);
//设置视频格式为默认格式NV21
mParams.setPreviewFormat(ImageFormat.NV21);
//默认是横拍旋转90度
mCamera.setDisplayOrientation(90);
CamParaUtil.getInstance().printSupportFocusMode(mParams);
List<String> focusModes = mParams.getSupportedFocusModes();
if(focusModes.contains("continuous-video")){
mParams.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
}
//实时获取视频流数据
mCamera.setPreviewCallback(new Camera.PreviewCallback() {
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
if(imgCallBack!=null && data!=null){
imgCallBack.getImgBuffer(data);
}
}
});
mCamera.setParameters(mParams);
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();//开启预览
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
isPreviewing = true;
mPreviwRate = previewRate;
mParams = mCamera.getParameters(); //重新get一次
Log.i(TAG, "最终设置:PreviewSize--With = " + mParams.getPreviewSize().width
+ "Height = " + mParams.getPreviewSize().height);
Log.i(TAG, "最终设置:PictureSize--With = " + mParams.getPictureSize().width
+ "Height = " + mParams.getPictureSize().height);
}
}
/**
* 停止预览,释放Camera
*/
public void doStopCamera(){
if(null != mCamera)
{
mCamera.setPreviewCallback(null);
mCamera.stopPreview();
isPreviewing = false;
mPreviwRate = -1f;
mCamera.release();
mCamera = null;
}
}
/**
* 拍照
*/
public void doTakePicture(){
if(isPreviewing && (mCamera != null)){
mCamera.takePicture(mShutterCallback, null, mJpegPictureCallback);
}
}
/**获取Camera.Parameters
* @return
*/
public Camera.Parameters getCameraParams(){
if(mCamera != null){
mParams = mCamera.getParameters();
return mParams;
}
return null;
}
public Camera getmCamera(){
if(mCamera != null){
return mCamera;
}
return null;
}
/**获取Camera实例
* @return
*/
public Camera getCameraDevice(){
return mCamera;
}
public int getCameraId(){
return mCameraId;
}
/*为了实现拍照的快门声音及拍照保存照片需要下面三个回调变量*/
ShutterCallback mShutterCallback = new ShutterCallback()
//快门按下的回调,在这里我们可以设置类似播放“咔嚓”声之类的操作。默认的就是咔嚓。
{
public void onShutter() {
// TODO Auto-generated method stub
Log.i(TAG, "myShutterCallback:onShutter...");
}
};
PictureCallback mRawCallback = new PictureCallback()
// 拍摄的未压缩原数据的回调,可以为null
{
public void onPictureTaken(byte[] data, Camera camera) {
// TODO Auto-generated method stub
Log.i(TAG, "myRawCallback:onPictureTaken...");
}
};
PictureCallback mJpegPictureCallback = new PictureCallback()
//对jpeg图像数据的回调,最重要的一个回调
{
public void onPictureTaken(byte[] data, Camera camera) {
// TODO Auto-generated method stub
Log.i(TAG, "myJpegCallback:onPictureTaken...");
Bitmap b = null;
if(null != data){
b = BitmapFactory.decodeByteArray(data, 0, data.length);//data是字节数据,将其解析成位图
mCamera.stopPreview();
isPreviewing = false;
}
//保存图片到sdcard
if(null != b)
{
//设置FOCUS_MODE_CONTINUOUS_VIDEO)之后,myParam.set("rotation", 90)失效。
//图片竟然不能旋转了,故这里要旋转下
Bitmap rotaBitmap = ImageUtil.getRotateBitmap(b, 90.0f);
// FileUtil.saveBitmap(rotaBitmap);
}
//再次进入预览
mCamera.startPreview();
isPreviewing = true;
}
};
}