dimanche 29 mars 2015

mCamera.setpreview{@override onPreviewFrame() } not work




  • I want to record videos and analysis the current bitmap in service.

  • So I register surfaceHolder.addCallback in onStartCommand function.

  • And set mCamera.setPreviewCallback in surfaceCreated function.

  • When I start the Service, it seems that the onPreviewFrame function never work.


I don't know why, Can anybody give me a hand?


Here is my code of onStartCommand function in Service.



@Override
public int onStartCommand(Intent intent, int flags, int startId) {
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();

sv.setZOrderOnTop(true);
sh.setFormat(PixelFormat.TRANSPARENT);


sh.addCallback(new SurfaceHolder.Callback() {
@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);
try {
mCamera.setPreviewDisplay(holder);

/***************************************************************************/
mCamera.setPreviewCallback(new Camera.PreviewCallback() {
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
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) {
e.printStackTrace();
}
mCamera.startPreview();
MessageHUB.get().sendMessage(MessageHUB.LOG_PREVIEW, null);
mCamera.unlock();
calibrate();
}

@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");

}
} else {
Log.d("TAG", "==== There is no camera hardware on device.");
}

return super.onStartCommand(intent, flags, startId);
}



Aucun commentaire:

Enregistrer un commentaire