Advertisement
Guest User

Untitled

a guest
Feb 9th, 2017
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.07 KB | None | 0 0
  1. float[] projectCoord(float[] coord, CameraCalibration cameraCalibration, Matrix34F pose, float[] offset, float scale)
  2. {
  3.     float[] converted = new float[2];
  4.  
  5.     Vec3F vec = new Vec3F(coord[0], coord[1], 0);
  6.     Vec2F sc = Tool.projectPoint(cameraCalibration, pose, vec);
  7.     converted[0] = sc.getData()[0]*scale - offset[0];
  8.     converted[1] = sc.getData()[1]*scale - offset[1];
  9.  
  10.     return converted;
  11. }
  12.  
  13. void calcScreenCoordsOf(float[] target, Matrix34F pose)
  14. {
  15.     // 0,0 is at centre of target so extremities are at w/2,h/2
  16.     float w = target[0];
  17.     float h = target[1];
  18.  
  19.     // need to account for the orientation on view size
  20.     DisplayMetrics metrics = new DisplayMetrics();
  21.     mActivity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
  22.     float viewHeight = metrics.widthPixels; // Portrait
  23.     float viewWidth = metrics.heightPixels - getStatusBarHeight(); // Portrait
  24.  
  25.     // calculate any mismatch of screen to video size
  26.     CameraDevice cameraDevice = CameraDevice.getInstance();
  27.     CameraCalibration cameraCalibration = cameraDevice.getCameraCalibration();
  28.     VideoMode videoMode = cameraDevice.getVideoMode(CameraDevice.MODE.MODE_DEFAULT);
  29.  
  30.     float scale = viewWidth/videoMode.getWidth();
  31.     if (videoMode.getHeight() * scale < viewHeight)
  32.         scale = viewHeight/videoMode.getHeight();
  33.     float scaledWidth = videoMode.getWidth() * scale;
  34.     float scaledHeight = videoMode.getHeight() * scale;
  35.  
  36.     float[] margin = {(scaledWidth - viewWidth)/2, (scaledHeight - viewHeight)/2};
  37.  
  38.     // now project the 4 corners of the target
  39.     final float[] x1 = projectCoord(new float[]{-w,h}, cameraCalibration, pose, margin, scale);
  40.     final float[] x2 = projectCoord(new float[]{-w,-h}, cameraCalibration, pose, margin, scale);
  41.     final float[] x3 = projectCoord(new float[]{w,-h}, cameraCalibration, pose, margin, scale);
  42.     final float[] x4 = projectCoord(new float[]{w,h}, cameraCalibration, pose, margin, scale);
  43.  
  44.     // 4 views
  45.     mActivity.runOnUiThread(new Runnable() {
  46.         @Override
  47.         public void run() {
  48.             RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(10, 10);
  49.             params1.leftMargin = Math.round(x1[0]);
  50.             params1.topMargin = Math.round(x1[1]);
  51.             mActivity.view1.setLayoutParams(params1);
  52.  
  53.             RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(10, 10);
  54.             params2.leftMargin = Math.round(x2[0]);
  55.             params2.topMargin = Math.round(x2[1]);
  56.             mActivity.view2.setLayoutParams(params2);
  57.  
  58.             RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(10, 10);
  59.             params3.leftMargin = Math.round(x3[0]);
  60.             params3.topMargin = Math.round(x3[1]);
  61.             mActivity.view3.setLayoutParams(params3);
  62.  
  63.             RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(10, 10);
  64.             params4.leftMargin = Math.round(x4[0]);
  65.             params4.topMargin = Math.round(x4[1]);
  66.             mActivity.view4.setLayoutParams(params4);
  67.         }
  68.     });
  69.  
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement