Qualcomm技术论坛
直播中

魏霖

7年用户 298经验值
私信 关注
[经验]

如何设定视频图像窗口大小并添加自定义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场景主界面画面了。

更多回帖

发帖
×
20
完善资料,
赚取积分