mCamera.setpreview{@override onPreviewFrame()}不起作用

时间:2015-03-27 21:12:14

标签: java android

  • 我想录制视频并分析服务中的当前位图
  • 所以我在surfaceHolder.addCallback函数中注册了onStartCommand
  • 并在mCamera.setPreviewCallback函数中设置surfaceCreated
  • 当我启动服务时,onPreviewFrame函数似乎永远不起作用。

我不知道为什么,有人可以帮我一把吗?

以下是我在服务中onStartCommand功能的代码。

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

1 个答案:

答案 0 :(得分:0)

您似乎正在使用surfacechanged

停止相机预览
mCamera.stopPreview();

尝试在onSurfaceChanged

中重新启动预览

Quoting the docs

  

还必须在预览类中重新启动预览   surfaceChanged()回调方法。