使用具有Surfaceview和延迟功能的API2启动相机预览

时间:2019-02-14 01:37:30

标签: android embedded android-camera camera-api

使用相机2 API和表面视图时遇到问题。这是针对旧的嵌入式系统的,出于某些我无法在此处解释的原因,我必须使用surfaceview。

我在此示例中找到了如何实现的示例:

https://android.googlesource.com/platform/frameworks/base/+/ee699a6/tests/Camera2Tests?autodive=0

我需要在“ onCreate”之后延迟启动预览。该代码可以正常工作。但是,如果添加延迟,预览将永远不会进行(表面回调不会被调用)

以下是一段代码,该代码有效:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.surfaceviewtest);

    new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
        @Override
        public void run() {
            startCameraExec();
        }
    }, 2000);
}

但这有效

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.surfaceviewtest);
    startCameraExec();
}

我的startCameraExec只是设置后台线程并设置表面回调

private void startCameraExec() {
    Log.d(TAG, "Starting API2 camera...");
    // Start a background thread to manage camera requests
    mBackgroundThread = new HandlerThread("background");
    mBackgroundThread.start();
    mBackgroundHandler = new Handler(mBackgroundThread.getLooper());
    mForegroundHandler = new Handler(getMainLooper());
    mCameraManager = (CameraManager) getSystemService(CAMERA_SERVICE);

    mSurfaceView = (SurfaceView) findViewById(R.id.mainSurfaceView);
    mSurfaceView.getHolder().addCallback(mSurfaceHolderCallback);
}

.xml也非常简单:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    android:orientation="vertical">

    <SurfaceView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mainSurfaceView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerInParent="true"
        android:layout_margin="80dp"
        android:onClick="onClickOnSurfaceView" />
</RelativeLayout>

这是我认为的问题所在,如果延迟初始化就永远不会被调用

final SurfaceHolder.Callback mSurfaceHolderCallback = new SurfaceHolder.Callback() {

... a bunch of code
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        // On the first invocation, width and height were automatically set to the view's size
        if (mCameraId == null) {
            // Find the device's back-facing camera and set the destination buffer sizes
            try {
                for (String cameraId : mCameraManager.getCameraIdList()) {
                    CameraCharacteristics cameraCharacteristics =
            ... a bunch of code

有什么线索可以解决这个问题吗?

谢谢。

2 个答案:

答案 0 :(得分:1)

您可以尝试在之后 surfaceChanged( { // if (mCameraId == null) {

输入延迟

但是您必须跟踪获取和启动摄像机流所需的所有条件。如果比较安全,可以只延迟呼叫开始预览。

答案 1 :(得分:0)

我找到了一个更简单的解决方案。我隐藏了表面,仅在延迟后才使其可见。那就是我想要的。谢谢您的投入。

private void startCameraExec() {
    Log.d(TAG, "Starting API2 camera...");

    mSurfaceView = (SurfaceView) findViewById(R.id.mainSurfaceView);

    // This did the trick :)
    mSurfaceView.setVisibility(View.VISIBLE);
    mSurfaceView.getHolder().addCallback(mSurfaceHolderCallback);