I want to record videos and analysis meanwhile in service, so I register surfaceHolder.addCallback
in onStartCommand
function, and set mCamera.setPreviewCallback
in surfaceCreated
function. But it seems that the onPreviewFrame
function doesn't work. Here is my onStartCommand
function.
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("TAG", "======= service in onStartCommand1");
MessageHUB.get().sendMessage(MessageHUB.LOG_ONSERVICE, null);
Log.d("TAG", "======= service in onStartCommand2");
if (Util.checkCameraHardware(this)) {
mCamera = Util.getCameraInstance();
if (mCamera != null) {
SurfaceView sv = new SurfaceView(this);
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(1, 1,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
SurfaceHolder sh = sv.getHolder();// 绑定SurfaceView,取得SurfaceHolder对象
sv.setZOrderOnTop(true);
sh.setFormat(PixelFormat.TRANSPARENT);
sh.addCallback(new SurfaceHolder.Callback() {// SurfaceHolder加入回调接口
@Override
public void surfaceCreated(SurfaceHolder holder) {
Camera.Parameters params = mCamera.getParameters();
mCamera.setParameters(params);
Camera.Parameters p = mCamera.getParameters();
List<Camera.Size> listSize;
listSize = p.getSupportedPreviewSizes();
Camera.Size mPreviewSize = listSize.get(2);
Log.v("TAG", "preview width = " + mPreviewSize.width
+ " preview height = " + mPreviewSize.height);
p.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
listSize = p.getSupportedPictureSizes();
Camera.Size mPictureSize = listSize.get(2);
Log.v("TAG", "capture width = " + mPictureSize.width
+ " capture height = " + mPictureSize.height);
p.setPictureSize(mPictureSize.width, mPictureSize.height);
mCamera.setParameters(p);
MessageHUB.get().sendMessage(MessageHUB.LOG_PARA, null);
try {
mCamera.setPreviewDisplay(holder);
/***************************************************************************/
mCamera.setPreviewCallback(new Camera.PreviewCallback() {
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
Log.d("TAG", "onPreviewFrame");
MessageHUB.get().sendMessage(MessageHUB.LOG_PREVIEWFRAME, null);
//传递进来的data,默认是YUV420SP的
if (_calibrationsLeft == -1)
return;
if (_calibrationsLeft > 0) {
// Doing calibration !
if (_currentFaceDetectionThread != null
&& _currentFaceDetectionThread.isAlive()) {
// Drop Frame
return;
}
// No face detection started or already finished
_processTimeForLastFrame = System.currentTimeMillis()
- _lastFrameStart;
_lastFrameStart = System.currentTimeMillis();
if (_currentFaceDetectionThread != null) {
_calibrationsLeft--;
updateMeasurement(_currentFaceDetectionThread.getCurrentFace());
if (_calibrationsLeft == 0) {
doneCalibrating();
return;
}
}
_currentFaceDetectionThread = new FaceDetectionThread(data,
_previewSize);
_currentFaceDetectionThread.start();
} else {
// Simple Measurement
if (_currentFaceDetectionThread != null
&& _currentFaceDetectionThread.isAlive()) {
// Drop Frame
return;
}
// No face detection started or already finished
_processTimeForLastFrame = System.currentTimeMillis()
- _lastFrameStart;
_lastFrameStart = System.currentTimeMillis();
if (_currentFaceDetectionThread != null)
updateMeasurement(_currentFaceDetectionThread.getCurrentFace());
_currentFaceDetectionThread = new FaceDetectionThread(data,
_previewSize);
_currentFaceDetectionThread.start();
}
}
});
/****************************************************************************/
} catch (IOException e) {
MessageHUB.get().sendMessage(MessageHUB.setPreviewDisplay_error, null);
e.printStackTrace();
}
mCamera.startPreview();
MessageHUB.get().sendMessage(MessageHUB.LOG_PREVIEW, null);
mCamera.unlock();
Log.d("TAG","=======calibrate0");
calibrate();
Log.d("TAG","=======calibrate1");
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
});
wm.addView(sv, params);
} else {
Log.d("TAG", "==== get Camera from service failed");
MessageHUB.get().sendMessage(MessageHUB.LOG_CAMERA_FAILED, null);
}
} else {
Log.d("TAG", "==== There is no camera hardware on device.");
}
return super.onStartCommand(intent, flags, startId);
}
Aucun commentaire:
Enregistrer un commentaire