代码之家  ›  专栏  ›  技术社区  ›  gmmo

使用带有surfaceview和delay的API2启动相机预览

  •  0
  • gmmo  · 技术社区  · 6 年前

    我在使用camera2api和surface view时遇到了一个问题。这是一个旧的嵌入式系统和我

    我发现这个示例展示了如何实现:

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

    我需要在“onCreate”之后延迟开始预览。这一准则毫不迟延地起作用。但是如果我添加一个延迟,预览就不会出现(surface回调就不会被调用)

    这里有一段代码 不是

    @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只是设置了后台线程和surface回调

    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 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
    

    谢谢您。

    0 回复  |  直到 6 年前
        1
  •  1
  •   Alex Cohn    6 年前

    你可以试着输入延迟时间 之后 surfaceChanged( { // if (mCameraId == null) {

        2
  •  0
  •   gmmo    6 年前

    我找到了一个更简单的解决办法。我把表面藏了起来,只是耽搁了一会儿才看得见。这就是我要找的。谢谢你的意见。

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