1. 程式人生 > >如何設定視訊影象視窗大小並新增自定義View

如何設定視訊影象視窗大小並新增自定義View


Vuforia預設的視訊是全屏的,可能你的應用中需要控制視訊的大小,並且需要新增自定義的View在圖一個介面中。

在例子工程中有下面這個包:

com.qualcomm.vuforia.samples.SampleApplication,這個包裡有三個類:

SampleApplicationControl、SampleApplicationException和SampleApplicationSession。

其中SampleApplicationControl主要是對VuforiaSDK進行實時控制,即正在識別時;

SampleApplicationSession主要是對sdk進行初始化的操作

SampleApplicationException這個就是對異常進行處理。

我們需要對SampleApplicationSession這個類中configureVideoBackground()方法進行操作。

該方法主要作用就是配置攝像頭獲取的視訊影象模式和大小。

// Configures the video mode and sets offsets for the camera's image
    private void configureVideoBackground()
    {
        CameraDevice cameraDevice = CameraDevice.getInstance();
        VideoMode vm = cameraDevice.getVideoMode(CameraDevice.MODE.MODE_DEFAULT);
        
        VideoBackgroundConfig config = new VideoBackgroundConfig();
        config.setEnabled(true);
        config.setSynchronous(true);
        config.setPosition(new Vec2I(0, 0));
        
        int xSize = 0, ySize = 0;
        if (mIsPortrait)
        {
            xSize = (int) (vm.getHeight() * (mScreenHeight / (float) vm
                .getWidth()));
            ySize = mScreenHeight;
            
            if (xSize < mScreenWidth)
            {
                xSize = mScreenWidth;
                ySize = (int) (mScreenWidth * (vm.getWidth() / (float) vm
                    .getHeight()));
            }
        } else
        {
            xSize = mScreenWidth;
            ySize = (int) (vm.getHeight() * (mScreenWidth / (float) vm
                .getWidth()));
            
            if (ySize < mScreenHeight)
            {
                xSize = (int) (mScreenHeight * (vm.getWidth() / (float) vm
                    .getHeight()));
                ySize = mScreenHeight;
            }
        }
        
        config.setSize(new Vec2I(xSize, ySize));
        
        Log.i(LOGTAG, "Configure Video Background : Video (" + vm.getWidth()
            + " , " + vm.getHeight() + "), Screen (" + mScreenWidth + " , "
            + mScreenHeight + "), mSize (" + xSize + " , " + ySize + ")");
        
        Renderer.getInstance().setVideoBackgroundConfig(config);
        
    }
按照自己的需要對應修改xSize和ySize的大小即可。

上面這個是Vuforia原始碼中共有的部分,對所以demo都適用。

那麼如何新增自定義view呢?

這個需要到每個不同的demo包下的主類中進行修改。

在主類onInitARDone()方法中新增即可,使用addContentView.

@Override
    public void onInitARDone(SampleApplicationException exception)
    {
        
        if (exception == null)
        {
            initApplicationAR();
            
            mRenderer.mIsActive = true;
            
            // Now add the GL surface view. It is important
            // that the OpenGL ES surface view gets added
            // BEFORE the camera is started and video
            // background is configured.
            addContentView(mGlView, new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));
            
            // Sets the UILayout to be drawn in front of the camera
            mUILayout.bringToFront();
            
            // Hides the Loading Dialog
            loadingDialogHandler
                .sendEmptyMessage(LoadingDialogHandler.HIDE_LOADING_DIALOG);
            
            // Sets the layout background to transparent
            mUILayout.setBackgroundColor(Color.TRANSPARENT);
            // add our own View here
//            showButton();
            try
            {
                vuforiaAppSession.startAR(CameraDevice.CAMERA.CAMERA_DEFAULT);
            } catch (SampleApplicationException e)
            {
                Log.e(LOGTAG, e.getString());
            }
            
            boolean result = CameraDevice.getInstance().setFocusMode(
                CameraDevice.FOCUS_MODE.FOCUS_MODE_CONTINUOUSAUTO);
            
            if (result)
                mContAutofocus = true;
            else
                Log.e(LOGTAG, "Unable to enable continuous autofocus");
            
            mSampleAppMenu = new SampleAppMenu(this, this, "Video Playback",
                mGlView, mUILayout, null);
            setSampleAppMenuSettings();
            
        } else
        {
            Log.e(LOGTAG, exception.getString());
            finish();
        }
        
    }
其中showButton是我自定義的View:
private void showButton()
    {
//    	Button button = new Button(this);
//    	button.setText("test");
//    	addContentView(button, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    	
    	View buttonView = View.inflate(VideoPlayback.this, R.layout.search_button_view, null);
    	addContentView(buttonView, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
//    	Button button2 = new Button(this);
//    	button2.setText("test2");
//    	addContentView(button2, params);
    	
    	Button button1 = (Button)findViewById(R.id.button1);
    	Button button2 = (Button)findViewById(R.id.button2);
    	Button button3 = (Button)findViewById(R.id.button3);
    	Button button4 = (Button)findViewById(R.id.button4);
    	
    	button1.setOnClickListener(new ButtonListen());
    	button2.setOnClickListener(new ButtonListen());
    	button3.setOnClickListener(new ButtonListen());
    	button4.setOnClickListener(new ButtonListen());
    	
    }
這樣就可以自定義自己的AR場景主介面畫面了。