锁屏上的相机预览

时间:2013-01-29 05:10:48

标签: android camera preview lockscreen

我想在锁屏中添加一个拍照功能,我需要在屏幕上打开预览,但它一直都会失败,logcat说错误是关于“setPreviewWindow - Null ANativeWindow传递给setPreviewWindow”和“startPreview - 预览未开始。预览进度标志设置“

在数字时钟视图后的keyguard_screen_tab_unlock.xml中,输入

<FrameLayout
        android:id="@+id/mPreview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

在LockScreen.java构造函数中:

FrameLayout mFrameLayout = (FrameLayout)findViewById(R.id.mPreview);
SurfacePreview preview = new SurfacePreview(mContext);
preview.setZOrderOnTop(true);
mFrameLayout.addView(preview);

SurfacePreview类:

public class SurfacePreview extends SurfaceView implements SurfaceHolder.Callback {

    private SurfaceHolder mSurfaceHolder;

    SurfacePreview(Context context) {
        super(context);
        mSurfaceHolder = this.getHolder();
        mSurfaceHolder.addCallback(this);
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        mCamera = Camera.open(CameraInfo.CAMERA_FACING_BACK);
        try {
            mCamera.setPreviewDisplay(holder);
        } catch (IOException e) {
            e.printStackTrace();
        }
        mCamera.startPreview();
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        Camera.Parameters parameters = mCamera.getParameters();
        mCamera.setParameters(parameters);
        mCamera.setDisplayOrientation(90);
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        mCamera.release();
    }
}

任何人都知道如何解决此问题并在LockScreen上启动prview?非常感谢。

0 个答案:

没有答案