Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- float[] projectCoord(float[] coord, CameraCalibration cameraCalibration, Matrix34F pose, float[] offset, float scale)
- {
- float[] converted = new float[2];
- Vec3F vec = new Vec3F(coord[0], coord[1], 0);
- Vec2F sc = Tool.projectPoint(cameraCalibration, pose, vec);
- converted[0] = sc.getData()[0]*scale - offset[0];
- converted[1] = sc.getData()[1]*scale - offset[1];
- return converted;
- }
- void calcScreenCoordsOf(float[] target, Matrix34F pose)
- {
- // 0,0 is at centre of target so extremities are at w/2,h/2
- float w = target[0];
- float h = target[1];
- // need to account for the orientation on view size
- DisplayMetrics metrics = new DisplayMetrics();
- mActivity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
- float viewHeight = metrics.widthPixels; // Portrait
- float viewWidth = metrics.heightPixels - getStatusBarHeight(); // Portrait
- // calculate any mismatch of screen to video size
- CameraDevice cameraDevice = CameraDevice.getInstance();
- CameraCalibration cameraCalibration = cameraDevice.getCameraCalibration();
- VideoMode videoMode = cameraDevice.getVideoMode(CameraDevice.MODE.MODE_DEFAULT);
- float scale = viewWidth/videoMode.getWidth();
- if (videoMode.getHeight() * scale < viewHeight)
- scale = viewHeight/videoMode.getHeight();
- float scaledWidth = videoMode.getWidth() * scale;
- float scaledHeight = videoMode.getHeight() * scale;
- float[] margin = {(scaledWidth - viewWidth)/2, (scaledHeight - viewHeight)/2};
- // now project the 4 corners of the target
- final float[] x1 = projectCoord(new float[]{-w,h}, cameraCalibration, pose, margin, scale);
- final float[] x2 = projectCoord(new float[]{-w,-h}, cameraCalibration, pose, margin, scale);
- final float[] x3 = projectCoord(new float[]{w,-h}, cameraCalibration, pose, margin, scale);
- final float[] x4 = projectCoord(new float[]{w,h}, cameraCalibration, pose, margin, scale);
- // 4 views
- mActivity.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(10, 10);
- params1.leftMargin = Math.round(x1[0]);
- params1.topMargin = Math.round(x1[1]);
- mActivity.view1.setLayoutParams(params1);
- RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(10, 10);
- params2.leftMargin = Math.round(x2[0]);
- params2.topMargin = Math.round(x2[1]);
- mActivity.view2.setLayoutParams(params2);
- RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(10, 10);
- params3.leftMargin = Math.round(x3[0]);
- params3.topMargin = Math.round(x3[1]);
- mActivity.view3.setLayoutParams(params3);
- RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(10, 10);
- params4.leftMargin = Math.round(x4[0]);
- params4.topMargin = Math.round(x4[1]);
- mActivity.view4.setLayoutParams(params4);
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement