Advertisement
GeorgeIoak

TSCalibrationView.java

Feb 10th, 2013
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.96 KB | None | 0 0
  1. /* Copyright (C) 2010 0xlab.org
  2. * Authored by: Kan-Ru Chen <kanru@0xlab.org>
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.zeroxlab.util.tscal;
  17.  
  18. import org.zeroxlab.util.tscal.R;
  19.  
  20. import java.io.File;
  21. import java.io.FileNotFoundException;
  22. import java.io.FileOutputStream;
  23. import java.io.IOException;
  24.  
  25. import android.content.Context;
  26. import android.graphics.Canvas;
  27. import android.graphics.Color;
  28. import android.graphics.Paint;
  29. import android.view.MotionEvent;
  30. import android.view.View;
  31. import java.util.Properties;
  32. import java.io.FileInputStream;
  33. import android.util.Log;
  34. import android.content.res.Resources;
  35.  
  36. public class TSCalibrationView extends View {
  37.  
  38. final private static String TAG = "TSCalibration";
  39. final private static int LIMIT = 100;
  40. final private static int J_LIMIT = 100;
  41. final private static int P_DELTA = 50;
  42. final private static int X_COMPENSATE = 20;
  43. final private static int Y_COMPENSATE = 20;
  44. private static int P_LIMIT;
  45. private static int SCREEN_MAX_WIDTH;
  46. private static int SCREEN_MAX_HEIGHT;
  47.  
  48. private int mWidth;
  49. private int mHeight;
  50. private int P_X_RIGHT_CORNER_MAX_LIMIT = 0;
  51. private int P_X_RIGHT_CORNER_MIN_LIMIT = 0;
  52.  
  53. private int P_Y_RIGHT_CORNER_MAX_LIMIT = 0;
  54. private int P_Y_RIGHT_CORNER_MIN_LIMIT = 0;
  55.  
  56. private int P_X_LEFT_CORNER_MAX_LIMIT = 0;
  57. private int P_X_LEFT_CORNER_MIN_LIMIT = 0;
  58.  
  59. private int P_Y_LEFT_CORNER_MAX_LIMIT = 0;
  60. private int P_Y_LEFT_CORNER_MIN_LIMIT = 0;
  61.  
  62. private int P_X_CENTER_MAX_LIMIT = 0;
  63. private int P_X_CENTER_MIN_LIMIT = 0;
  64.  
  65. private int P_Y_CENTER_MAX_LIMIT = 0;
  66. private int P_Y_CENTER_MIN_LIMIT = 0;
  67.  
  68. boolean mFileExist = false;
  69. boolean mGetPara = false;
  70. int[] mCurTpPara = new int[7];
  71. Properties mProp;
  72.  
  73. private class TargetPoint
  74. {
  75. public int x;
  76. public int y;
  77. public int calx;
  78. public int caly;
  79. public int xfb;
  80. public int yfb;
  81.  
  82. public TargetPoint()
  83. {
  84. this.x = 0;
  85. this.y = 0;
  86. }
  87.  
  88. public void SetTargetPoint(int x,int y)
  89. {
  90. this.x = x;
  91. this.y = y;
  92. }
  93. }
  94.  
  95. private class TargetCal
  96. {
  97. int[] tppara = new int[7];
  98. TargetPoint[] mTargetPoints;
  99.  
  100. public TargetCal()
  101. {
  102.  
  103. mTargetPoints = new TargetPoint[5];
  104. mTargetPoints[0] = new TargetPoint();
  105. mTargetPoints[1] = new TargetPoint();
  106. mTargetPoints[2] = new TargetPoint();
  107. mTargetPoints[3] = new TargetPoint();
  108. mTargetPoints[4] = new TargetPoint();
  109. }
  110. }
  111.  
  112. private class TsSample
  113. {
  114. public int x;
  115. public int y;
  116.  
  117. public TsSample()
  118. {
  119. this.x = 0;
  120. this.y = 0;
  121. }
  122. }
  123.  
  124. private int mStep = 0;
  125. private TargetCal mTargetCaluate;
  126. private TSCalibration mContext;
  127.  
  128. public TSCalibrationView(TSCalibration context, int h, int w ,boolean fileexist) {
  129. super(context);
  130. Resources res = context.getResources();
  131. P_LIMIT=res.getInteger(R.integer.p_limit);
  132. SCREEN_MAX_WIDTH = res.getInteger(R.integer.screen_max_width);
  133. SCREEN_MAX_HEIGHT = res.getInteger(R.integer.sreen_max_height);
  134. Log.v(TAG,P_LIMIT+":"+SCREEN_MAX_WIDTH+":"+SCREEN_MAX_HEIGHT);
  135.  
  136. mHeight = h;
  137. mWidth = w;
  138. P_X_RIGHT_CORNER_MAX_LIMIT = mWidth - X_COMPENSATE - 50 + P_DELTA;
  139. P_X_RIGHT_CORNER_MIN_LIMIT = mWidth - X_COMPENSATE - 50 - P_DELTA;
  140.  
  141. P_Y_RIGHT_CORNER_MAX_LIMIT = mHeight - Y_COMPENSATE - 50 + P_DELTA;
  142. P_Y_RIGHT_CORNER_MIN_LIMIT = mHeight - Y_COMPENSATE - 50 - P_DELTA;
  143.  
  144. P_X_LEFT_CORNER_MAX_LIMIT = 50 + X_COMPENSATE + P_DELTA;
  145. P_X_LEFT_CORNER_MIN_LIMIT = 50 + X_COMPENSATE - P_DELTA;
  146.  
  147. P_Y_LEFT_CORNER_MAX_LIMIT = 50 + Y_COMPENSATE + P_DELTA;
  148. P_Y_LEFT_CORNER_MIN_LIMIT = 50 + Y_COMPENSATE - P_DELTA;
  149.  
  150. P_X_CENTER_MAX_LIMIT = mWidth/2 + P_DELTA;
  151. P_X_CENTER_MIN_LIMIT = mWidth/2 - P_DELTA;
  152.  
  153. P_Y_CENTER_MAX_LIMIT = mHeight/2 + P_DELTA;
  154. P_Y_CENTER_MIN_LIMIT = mHeight/2 - P_DELTA;
  155. mContext = context;
  156. mTargetCaluate = new TargetCal();
  157. mTargetCaluate.mTargetPoints[0].SetTargetPoint(50, 50);
  158. mTargetCaluate.mTargetPoints[1].SetTargetPoint(w - 50, 50);
  159. mTargetCaluate.mTargetPoints[2].SetTargetPoint(w - 50, h - 50);
  160. mTargetCaluate.mTargetPoints[3].SetTargetPoint(50, h - 50);
  161. mTargetCaluate.mTargetPoints[4].SetTargetPoint(w/2, h/2);
  162.  
  163. mProp = new Properties();
  164. mFileExist = fileexist;
  165. if(mFileExist == true)
  166. {
  167. ReadParaFromProp();
  168. }
  169. }
  170.  
  171. public void reset()
  172. {
  173. mStep = 0;
  174. }
  175.  
  176.  
  177. private int variance_judge(TsSample[] tp_data, int nr,int limit)
  178. {
  179. int i, j;
  180. int diff_x;
  181. int diff_y;
  182.  
  183.  
  184. for (i = 0; i < nr - 1; i++)
  185. {
  186. for (j = i + 1; j < nr; j++)
  187. {
  188. /*
  189. * Calculate the variance between sample 'i'
  190. * and sample 'j'. X and Y values are treated
  191. * separately.
  192. */
  193. diff_x = tp_data[i].x - tp_data[j].x;
  194. if (diff_x < 0)
  195. {
  196. diff_x = -diff_x;
  197. }
  198.  
  199. diff_y = tp_data[i].y - tp_data[j].y;
  200. if (diff_y < 0)
  201. {
  202. diff_y = -diff_y;
  203. }
  204.  
  205.  
  206.  
  207. /*
  208. * Is the variance between any two samples too large?
  209. */
  210. if (diff_x > limit || diff_y > limit)
  211. {
  212.  
  213. Log.w(TAG,"tp ="+diff_x+"diff_y="+diff_y+"\n");
  214. return -1;
  215. }
  216.  
  217. }
  218. }
  219.  
  220. return 0;
  221. }
  222.  
  223. /*----------------------------------------------------------------------------
  224. * FUNCTION NAME: variance_judge
  225. *
  226. * PURPOSE: judge points available
  227. *
  228. *
  229. * INPUT:
  230. * *data Is a pointer to the array to be judged
  231. * nr the number of elements
  232. * limit criterion
  233. *
  234. * OUTPUT:
  235. *
  236. *
  237. * RETURN VALUE:
  238. * ret 0 elements is a point
  239. * -1 elements is not a point
  240. *
  241. *
  242. *---------------------------------------------------------------------------*/
  243. /*
  244. * We have 7 complete samples. Calculate the variance between each,
  245. * if the variance beteween any two samples too large, return fial;
  246. *
  247. */
  248.  
  249.  
  250. private int point_to_point_judge(int[] data)
  251. {
  252. int ret;
  253. int diff;
  254. int p_limit;
  255.  
  256. p_limit = TSCalibrationView.P_LIMIT;
  257.  
  258. if(data[0] < data[1])
  259. diff = data[1] - data[0];
  260. else
  261. diff = data[0] - data[1];
  262.  
  263. if(diff < p_limit)
  264. {
  265.  
  266. Log.w(TAG,"tp ="+diff+"\n");
  267. ret = -1;
  268. }
  269. else
  270. ret = 0;
  271.  
  272. return ret;
  273. }
  274.  
  275. /*----------------------------------------------------------------------------
  276. * FUNCTION NAME: judge_point_location
  277. *
  278. * PURPOSE: judge whether the pointer is located in where u want.
  279. * RETURN VALUE:
  280. * ret 0 points all located where u want
  281. * -1 at least one point not located on where u expected.
  282. *
  283. *
  284. *---------------------------------------------------------------------------*/
  285. private int judge_point_location(TargetCal cal)
  286. {
  287. if(SCREEN_MAX_WIDTH != mWidth || SCREEN_MAX_HEIGHT != mHeight){
  288. Log.d(TAG, "screen mWidth" + mWidth + "!= " + SCREEN_MAX_WIDTH + "|| mHeight" + mHeight + "!=" + SCREEN_MAX_HEIGHT + ", the calibrate result may not as well as u expect.");
  289. }
  290. //judge y coordinate.
  291. if((cal.mTargetPoints[0].caly>P_Y_RIGHT_CORNER_MAX_LIMIT) || (cal.mTargetPoints[0].caly<P_Y_RIGHT_CORNER_MIN_LIMIT) ){
  292. if((cal.mTargetPoints[0].caly>P_Y_LEFT_CORNER_MAX_LIMIT) || (cal.mTargetPoints[0].caly<P_Y_LEFT_CORNER_MIN_LIMIT)){
  293. Log.d(TAG,"cal.mTargetPoints[0].caly = " + cal.mTargetPoints[0].caly + "not locate between" + P_Y_RIGHT_CORNER_MAX_LIMIT +" and " + P_Y_RIGHT_CORNER_MIN_LIMIT +
  294. "also not locate between" + P_Y_LEFT_CORNER_MAX_LIMIT + "and" + P_Y_LEFT_CORNER_MIN_LIMIT + "\n");
  295. return -1;
  296. }
  297. }
  298.  
  299. if((cal.mTargetPoints[1].caly>P_Y_RIGHT_CORNER_MAX_LIMIT) || (cal.mTargetPoints[1].caly<P_Y_RIGHT_CORNER_MIN_LIMIT) ){
  300. if((cal.mTargetPoints[1].caly>P_Y_LEFT_CORNER_MAX_LIMIT) || (cal.mTargetPoints[1].caly<P_Y_LEFT_CORNER_MIN_LIMIT)){
  301. Log.d(TAG,"cal.mTargetPoints[1].caly = " + cal.mTargetPoints[1].caly + "not locate between" + P_Y_RIGHT_CORNER_MAX_LIMIT +" and " + P_Y_RIGHT_CORNER_MIN_LIMIT +
  302. "also not locate between" + P_Y_LEFT_CORNER_MAX_LIMIT + "and" + P_Y_LEFT_CORNER_MIN_LIMIT + "\n");
  303. return -1;
  304. }
  305. }
  306.  
  307. if((cal.mTargetPoints[2].caly>P_Y_RIGHT_CORNER_MAX_LIMIT) || (cal.mTargetPoints[2].caly<P_Y_RIGHT_CORNER_MIN_LIMIT) ){
  308. if((cal.mTargetPoints[2].caly>P_Y_LEFT_CORNER_MAX_LIMIT) || (cal.mTargetPoints[2].caly<P_Y_LEFT_CORNER_MIN_LIMIT)){
  309. Log.d(TAG,"cal.mTargetPoints[2].caly = " + cal.mTargetPoints[2].caly + "not locate between" + P_Y_RIGHT_CORNER_MAX_LIMIT +" and " + P_Y_RIGHT_CORNER_MIN_LIMIT +
  310. "also not locate between" + P_Y_LEFT_CORNER_MAX_LIMIT + "and" + P_Y_LEFT_CORNER_MIN_LIMIT + "\n");
  311. return -1;
  312. }
  313. }
  314.  
  315. if((cal.mTargetPoints[3].caly>P_Y_RIGHT_CORNER_MAX_LIMIT) || (cal.mTargetPoints[3].caly<P_Y_RIGHT_CORNER_MIN_LIMIT) ){
  316. if((cal.mTargetPoints[3].caly>P_Y_LEFT_CORNER_MAX_LIMIT) || (cal.mTargetPoints[3].caly<P_Y_LEFT_CORNER_MIN_LIMIT)){
  317. Log.d(TAG,"cal.mTargetPoints[3].caly = " + cal.mTargetPoints[3].caly + "not locate between" + P_Y_RIGHT_CORNER_MAX_LIMIT +" and " + P_Y_RIGHT_CORNER_MIN_LIMIT +
  318. "also not locate between" + P_Y_LEFT_CORNER_MAX_LIMIT + "and" + P_Y_LEFT_CORNER_MIN_LIMIT + "\n");
  319. return -1;
  320. }
  321. }
  322.  
  323. //y center
  324. if((cal.mTargetPoints[4].caly>P_Y_CENTER_MAX_LIMIT) || (cal.mTargetPoints[4].caly<P_Y_CENTER_MIN_LIMIT) ){
  325. Log.d(TAG,"cal.mTargetPoints[4].caly = " + cal.mTargetPoints[4].caly + "not locate between" + P_Y_CENTER_MAX_LIMIT +" and " + P_Y_CENTER_MIN_LIMIT + "\n");
  326. return -1;
  327. }
  328. //judge x coordinate.
  329. if((cal.mTargetPoints[0].calx>P_X_RIGHT_CORNER_MAX_LIMIT) || (cal.mTargetPoints[0].calx<P_X_RIGHT_CORNER_MIN_LIMIT) ){
  330. if((cal.mTargetPoints[0].calx>P_X_LEFT_CORNER_MAX_LIMIT) || (cal.mTargetPoints[0].calx<P_X_LEFT_CORNER_MIN_LIMIT)){
  331. Log.d(TAG,"cal.mTargetPoints[0].calx = " + cal.mTargetPoints[0].calx + "not locate between" + P_X_RIGHT_CORNER_MAX_LIMIT +" and " + P_X_RIGHT_CORNER_MIN_LIMIT +
  332. "also not locate between" + P_X_LEFT_CORNER_MAX_LIMIT + "and" + P_X_LEFT_CORNER_MIN_LIMIT + "\n");
  333. return -1;
  334. }
  335. }
  336.  
  337. if((cal.mTargetPoints[1].calx>P_X_RIGHT_CORNER_MAX_LIMIT) || (cal.mTargetPoints[1].calx<P_X_RIGHT_CORNER_MIN_LIMIT) ){
  338. if((cal.mTargetPoints[1].calx>P_X_LEFT_CORNER_MAX_LIMIT) || (cal.mTargetPoints[1].calx<P_X_LEFT_CORNER_MIN_LIMIT)){
  339. Log.d(TAG,"cal.mTargetPoints[1].calx = " + cal.mTargetPoints[1].calx + "not locate between" + P_X_RIGHT_CORNER_MAX_LIMIT +" and " + P_X_RIGHT_CORNER_MIN_LIMIT +
  340. "also not locate between" + P_X_LEFT_CORNER_MAX_LIMIT + "and" + P_X_LEFT_CORNER_MIN_LIMIT + "\n");
  341. return -1;
  342. }
  343. }
  344.  
  345. if((cal.mTargetPoints[2].calx>P_X_RIGHT_CORNER_MAX_LIMIT) || (cal.mTargetPoints[2].calx<P_X_RIGHT_CORNER_MIN_LIMIT) ){
  346. if((cal.mTargetPoints[2].calx>P_X_LEFT_CORNER_MAX_LIMIT) || (cal.mTargetPoints[2].calx<P_X_LEFT_CORNER_MIN_LIMIT)){
  347. Log.d(TAG,"cal.mTargetPoints[2].calx = " + cal.mTargetPoints[2].calx + "not locate between" + P_X_RIGHT_CORNER_MAX_LIMIT +" and " + P_X_RIGHT_CORNER_MIN_LIMIT +
  348. "also not locate between" + P_X_LEFT_CORNER_MAX_LIMIT + "and" + P_X_LEFT_CORNER_MIN_LIMIT + "\n");
  349. return -1;
  350. }
  351. }
  352.  
  353. if((cal.mTargetPoints[3].calx>P_X_RIGHT_CORNER_MAX_LIMIT) || (cal.mTargetPoints[3].calx<P_X_RIGHT_CORNER_MIN_LIMIT) ){
  354. if((cal.mTargetPoints[3].calx>P_X_LEFT_CORNER_MAX_LIMIT) || (cal.mTargetPoints[3].calx<P_X_LEFT_CORNER_MIN_LIMIT)){
  355. Log.d(TAG,"cal.mTargetPoints[3].calx = " + cal.mTargetPoints[3].calx + "not locate between" + P_X_RIGHT_CORNER_MAX_LIMIT +" and " + P_X_RIGHT_CORNER_MIN_LIMIT +
  356. "also not locate between" + P_X_LEFT_CORNER_MAX_LIMIT + "and" + P_X_LEFT_CORNER_MIN_LIMIT + "\n");
  357. return -1;
  358. }
  359. }
  360.  
  361. //x center
  362. if((cal.mTargetPoints[4].calx>P_X_CENTER_MAX_LIMIT) || (cal.mTargetPoints[4].calx<P_X_CENTER_MIN_LIMIT) ){
  363. Log.d(TAG,"cal.mTargetPoints[4].calx = " + cal.mTargetPoints[4].calx + "not locate between" + P_X_CENTER_MAX_LIMIT +" and " + P_X_CENTER_MIN_LIMIT + "\n");
  364. return -1;
  365. }
  366.  
  367. return 0;
  368. }
  369. /*----------------------------------------------------------------------------
  370. * FUNCTION NAME: judge_points
  371. *
  372. * PURPOSE: judge that points is available point to calibration
  373. *
  374. *
  375. * INPUT:
  376. * *cal Is a pointer to the struct of calibration
  377. *
  378. * OUTPUT:
  379. *
  380. *
  381. * RETURN VALUE:
  382. * ret 0 points is avaliable
  383. * -1 points is unavaliable
  384. *
  385. *
  386. *---------------------------------------------------------------------------*/
  387. private int judge_points(TargetCal cal)
  388. {
  389. int ret = 0;
  390. TsSample[] tp_data;
  391. int[] data = {0,0};
  392. int limit;
  393. int j_limit;
  394.  
  395. tp_data = new TsSample[2];
  396. tp_data[0] = new TsSample();
  397. tp_data[1] = new TsSample();
  398.  
  399. limit = TSCalibrationView.LIMIT;
  400. j_limit = 2*TSCalibrationView.J_LIMIT;
  401.  
  402. tp_data[0].y = cal.mTargetPoints[0].caly;
  403. tp_data[1].y = cal.mTargetPoints[1].caly;
  404. tp_data[0].x = 0;
  405. tp_data[1].x = 0;
  406. data[0] = cal.mTargetPoints[0].calx;
  407. data[1] = cal.mTargetPoints[1].calx;
  408.  
  409. if(0!=judge_point_location(cal)){
  410. Log.d(TAG, "at least, one point not locate on expected location. \n");
  411. return -1;
  412. }
  413.  
  414. Log.d(TAG,"cal.mTargetPoints[0].caly = " + cal.mTargetPoints[0].caly + "cal.mTargetPoints[1].caly = " + cal.mTargetPoints[1].caly +
  415. "cal.mTargetPoints[0].calx =" + cal.mTargetPoints[0].calx + "cal.mTargetPoints[1].calx =" + cal.mTargetPoints[1].calx);
  416. if((variance_judge(tp_data ,2,limit) != 0)||(point_to_point_judge(data) != 0))
  417. {
  418. ret = -1;
  419. Log.d(TAG,"Unmath TOP left and Top Right \n");
  420. Log.d(TAG,"tp ="+cal.mTargetPoints[0].caly+", %d\n"+cal.mTargetPoints[0].caly+cal.mTargetPoints[1].caly);
  421. return ret;
  422. }
  423.  
  424. tp_data[0].x = cal.mTargetPoints[1].calx;
  425. tp_data[1].x = cal.mTargetPoints[2].calx;
  426. tp_data[0].y = 0;
  427. tp_data[1].y = 0;
  428. data[0] = cal.mTargetPoints[1].caly;
  429. data[1] = cal.mTargetPoints[2].caly;
  430. Log.d(TAG,"cal.mTargetPoints[1].calx = " + cal.mTargetPoints[1].calx + "cal.mTargetPoints[2].calx = " + cal.mTargetPoints[2].calx +
  431. "cal.mTargetPoints[1].caly =" + cal.mTargetPoints[1].caly + "cal.mTargetPoints[2].caly =" + cal.mTargetPoints[2].caly);
  432. if((variance_judge(tp_data ,2,limit) != 0)|| (point_to_point_judge(data) != 0))
  433. {
  434. ret = -1;
  435. Log.d(TAG,"Unmath TOP Right and Bot Right \n");
  436. Log.w(TAG,"tp = %d, %d\n"+cal.mTargetPoints[1].calx+cal.mTargetPoints[2].calx);
  437. return ret;
  438. }
  439.  
  440. tp_data[0].y = cal.mTargetPoints[2].caly;
  441. tp_data[1].y = cal.mTargetPoints[3].caly;
  442. tp_data[0].x = 0;
  443. tp_data[1].x = 0;
  444. data[0] = cal.mTargetPoints[2].calx;
  445. data[1] = cal.mTargetPoints[3].calx;
  446. Log.d(TAG,"cal.mTargetPoints[2].caly = " + cal.mTargetPoints[2].caly + "cal.mTargetPoints[3].caly = " + cal.mTargetPoints[3].caly +
  447. "cal.mTargetPoints[2].calx =" + cal.mTargetPoints[2].calx + "cal.mTargetPoints[3].calx =" + cal.mTargetPoints[3].calx);
  448. if((variance_judge(tp_data ,2,limit) != 0)|| (point_to_point_judge(data)!= 0))
  449. {
  450. ret = -1;
  451. Log.d(TAG,"Unmath Bot Right and Bot Left \n");
  452. Log.w(TAG,"tp = %d, %d\n"+cal.mTargetPoints[2].caly+cal.mTargetPoints[3].caly);
  453. return ret;
  454. }
  455.  
  456. tp_data[0].x = cal.mTargetPoints[0].calx;
  457. tp_data[1].x = cal.mTargetPoints[3].calx;
  458. tp_data[0].y = 0;
  459. tp_data[1].y = 0;
  460. data[0] = cal.mTargetPoints[0].caly;
  461. data[1] = cal.mTargetPoints[3].caly;
  462. Log.d(TAG,"cal.mTargetPoints[0].calx = " + cal.mTargetPoints[0].calx + "cal.mTargetPoints[3].calx = " + cal.mTargetPoints[3].calx +
  463. "cal.mTargetPoints[0].caly =" + cal.mTargetPoints[0].caly + "cal.mTargetPoints[3].caly =" + cal.mTargetPoints[3].caly);
  464. if((variance_judge(tp_data ,2,limit) != 0)|| (point_to_point_judge(data) != 0))
  465. {
  466. ret = -1;
  467. Log.d(TAG,"Unmath TOP Left and Bot Left \n");
  468. Log.d(TAG,"tp = %d, %d\n"+cal.mTargetPoints[0].calx+cal.mTargetPoints[3].calx);
  469. return ret;
  470. }
  471.  
  472. tp_data[0].x = (cal.mTargetPoints[0].calx + cal.mTargetPoints[1].calx + cal.mTargetPoints[2].calx + cal.mTargetPoints[3].calx)/4;
  473. tp_data[1].x = cal.mTargetPoints[4].calx;
  474.  
  475. tp_data[0].y = (cal.mTargetPoints[0].caly + cal.mTargetPoints[1].caly + cal.mTargetPoints[2].caly + cal.mTargetPoints[3].caly)/4;
  476. tp_data[1].y = cal.mTargetPoints[4].caly;
  477.  
  478. Log.d(TAG,"Center tx ="+cal.mTargetPoints[0].calx +cal.mTargetPoints[1].calx+cal.mTargetPoints[2].calx+cal.mTargetPoints[3].calx +cal.mTargetPoints[4].calx+tp_data[0].x + "\n");
  479. Log.d(TAG,"Center ty ="+cal.mTargetPoints[0].caly +cal.mTargetPoints[1].caly+cal.mTargetPoints[2].caly+cal.mTargetPoints[3].caly +cal.mTargetPoints[4].caly+tp_data[1].y + "\n");
  480. if((variance_judge(tp_data ,2, j_limit) != 0))
  481. {
  482. ret = -1;
  483. Log.d(TAG,"Unmath Center X ,y and The other point \n");
  484. Log.d(TAG,"tx = \n"+cal.mTargetPoints[0].calx +cal.mTargetPoints[1].calx+cal.mTargetPoints[2].calx+cal.mTargetPoints[3].calx +cal.mTargetPoints[4].calx+tp_data[0].x + "\n");
  485. Log.d(TAG,"ty = \n"+cal.mTargetPoints[0].caly +cal.mTargetPoints[1].caly+cal.mTargetPoints[2].caly+cal.mTargetPoints[3].caly +cal.mTargetPoints[4].caly+tp_data[1].y + "\n");
  486. return ret;
  487. }
  488.  
  489. ret = 0;
  490. return ret;
  491. }
  492.  
  493.  
  494. private int perform_calibration(int count, int[] para) {
  495. int mStep,ret,j;
  496. float n, x, y, x2, y2, xy, z, zx, zy;
  497. float det, a, b, c, e, f, i;
  498. float scaling = 65536.0f;
  499. int[] cx = new int[5];
  500. int[] cy = new int[5];
  501. int[] xfb = new int[5];
  502. int[] yfb = new int[5];
  503. int[] ca = new int[7] ;
  504.  
  505.  
  506. for(mStep = 0; mStep < count; mStep++)
  507. {
  508. cx[mStep] = (int)mTargetCaluate.mTargetPoints[mStep].calx ;
  509. cy[mStep] = (int)mTargetCaluate.mTargetPoints[mStep].caly;
  510. xfb[mStep] = (int)mTargetCaluate.mTargetPoints[mStep].x;
  511. yfb[mStep] = (int)mTargetCaluate.mTargetPoints[mStep].y;
  512. }
  513.  
  514.  
  515. // Get sums for matrix
  516. n = x = y = x2 = y2 = xy = 0;
  517. for(j=0;j<5;j++) {
  518. n += 1.0;
  519. x += (float)cx[j];
  520. y += (float)cy[j];
  521. x2 += (float)(cx[j]*cx[j]);
  522. y2 += (float)(cy[j]*cy[j]);
  523. xy += (float)(cx[j]*cy[j]);
  524. }
  525.  
  526. // Get determinant of matrix -- check if determinant is too small
  527. det = n*(x2*y2 - xy*xy) + x*(xy*y - x*y2) + y*(x*xy - y*x2);
  528. if(det < 0.1 && det > -0.1) {
  529. Log.v(TAG,"ts_calibrate: determinant is too small -- " + det + "\n");
  530. return 0;
  531. }
  532.  
  533. // Get elements of inverse matrix
  534. a = (x2*y2 - xy*xy)/det;
  535. b = (xy*y - x*y2)/det;
  536. c = (x*xy - y*x2)/det;
  537. e = (n*y2 - y*y)/det;
  538. f = (x*y - n*xy)/det;
  539. i = (n*x2 - x*x)/det;
  540.  
  541. // Get sums for x calibration
  542. z = zx = zy = 0;
  543. for(j=0;j<5;j++) {
  544. z += (float)xfb[j];
  545. zx += (float)(xfb[j]*cx[j]);
  546. zy += (float)(xfb[j]*cy[j]);
  547. }
  548.  
  549. // Now multiply out to get the calibration for framebuffer x coord
  550. ca[0] = (int)((a*z + b*zx + c*zy)*(scaling));
  551. ca[1] = (int)((b*z + e*zx + f*zy)*(scaling));
  552. ca[2] = (int)((c*z + f*zx + i*zy)*(scaling));
  553.  
  554. Log.v(TAG,ca[0]+ " " + ca[1] + " " + ca[2]);
  555.  
  556. // Get sums for y calibration
  557. z = zx = zy = 0;
  558. for(j=0;j<5;j++)
  559. {
  560. z += (float)yfb[j];
  561. zx += (float)(yfb[j]*cx[j]);
  562. zy += (float)(yfb[j]*cy[j]);
  563. }
  564.  
  565. // Now multiply out to get the calibration for framebuffer y coord
  566. ca[3] = (int)((a*z + b*zx + c*zy)*(scaling));
  567. ca[4] = (int)((b*z + e*zx + f*zy)*(scaling));
  568. ca[5] = (int)((c*z + f*zx + i*zy)*(scaling));
  569.  
  570.  
  571. // If we got here, we're OK, so assign scaling to a[6] and return
  572. ca[6] = (int)scaling;
  573.  
  574.  
  575. para[0] = ca[1];
  576. para[1] = ca[2];
  577. para[2] = ca[0];
  578. para[3] = ca[4];
  579. para[4] = ca[5];
  580. para[5] = ca[3];
  581. para[6] = ca[6];
  582.  
  583. return 1;
  584.  
  585. }
  586.  
  587.  
  588. public boolean isFinished()
  589. {
  590. int ret;
  591.  
  592. if(mStep >=5)
  593. {
  594. ret = judge_points(mTargetCaluate);
  595. if(ret == 0)
  596. {
  597. perform_calibration(5,mTargetCaluate.tppara);
  598. return true;
  599. }
  600.  
  601. mStep = 0;
  602.  
  603. return false;
  604. }
  605. else
  606. {
  607. return false;
  608. }
  609. }
  610.  
  611. /*
  612. public void dumpCalData(File file) {
  613. StringBuilder sb = new StringBuilder();
  614. for (TargetPoint point : mTargetCaluate.mTargetPoints) {
  615. sb.append(point.calx);
  616. sb.append(" ");
  617. sb.append(point.caly);
  618. sb.append(" ");
  619. }
  620. for (TargetPoint point : mTargetCaluate.mTargetPoints) {
  621. sb.append(point.x);
  622. sb.append(" ");
  623. sb.append(point.y);
  624. sb.append(" ");
  625. }
  626. try {
  627. FileOutputStream fos = new FileOutputStream(file);
  628. fos.write(sb.toString().getBytes());
  629. fos.flush();
  630. fos.getFD().sync();
  631. fos.close();
  632. } catch (FileNotFoundException e) {
  633. Log.e(TAG, "Cannot open file " + file);
  634. } catch (IOException e) {
  635. Log.e(TAG, "Cannot write file " + file);
  636. }
  637. }
  638. */
  639. public void WriteParaToProp()
  640. {
  641. if(mProp == null)
  642. {
  643. Log.d(TAG,"mProp is null");
  644. }
  645. else
  646. {
  647. mProp.setProperty("TP_PARA1",Integer.toString(mTargetCaluate.tppara[0]));
  648. mProp.setProperty("TP_PARA2",Integer.toString(mTargetCaluate.tppara[1]));
  649. mProp.setProperty("TP_PARA3",Integer.toString(mTargetCaluate.tppara[2]));
  650. mProp.setProperty("TP_PARA4",Integer.toString(mTargetCaluate.tppara[3]));
  651. mProp.setProperty("TP_PARA5",Integer.toString(mTargetCaluate.tppara[4]));
  652. mProp.setProperty("TP_PARA6",Integer.toString(mTargetCaluate.tppara[5]));
  653. mProp.setProperty("TP_PARA7",Integer.toString(mTargetCaluate.tppara[6]));
  654.  
  655. try
  656. {
  657. FileOutputStream fos = new FileOutputStream("/data/pointercal");
  658. mProp.store(fos, "Copyright");
  659. fos.close();
  660. }
  661. catch(Exception e)
  662. {
  663. Log.d(TAG,"file input stream error!");
  664. }
  665. }
  666. }
  667.  
  668. public void ReadParaFromProp()
  669. {
  670. if(mProp == null)
  671. {
  672. Log.d(TAG,"mProp is null");
  673. }
  674. else
  675. {
  676. try
  677. {
  678. if(mGetPara == false)
  679. {
  680. Log.d(TAG,"mProp is not null");
  681.  
  682. FileInputStream mFis = new FileInputStream("/data/pointercal");
  683. mProp.load(mFis);
  684. mCurTpPara[0] = Integer.parseInt(mProp.getProperty("TP_PARA1"));
  685. mCurTpPara[1] = Integer.parseInt(mProp.getProperty("TP_PARA2"));
  686. mCurTpPara[2] = Integer.parseInt(mProp.getProperty("TP_PARA3"));
  687. mCurTpPara[3] = Integer.parseInt(mProp.getProperty("TP_PARA4"));
  688. mCurTpPara[4] = Integer.parseInt(mProp.getProperty("TP_PARA5"));
  689. mCurTpPara[5] = Integer.parseInt(mProp.getProperty("TP_PARA6"));
  690. mCurTpPara[6] = Integer.parseInt(mProp.getProperty("TP_PARA7"));
  691. mFis.close();
  692.  
  693. mGetPara = true;
  694. }
  695. }
  696. catch(Exception e)
  697. {
  698. Log.d(TAG,"file input stream error!");
  699. }
  700.  
  701. }
  702. }
  703.  
  704. /*public void dumpCalData(File file) {
  705. int i;
  706. int k;
  707.  
  708. StringBuilder sb = new StringBuilder();
  709. for (i = 0; i < 7; i++) {
  710. k = i + 1;
  711. sb.append("TP_PARA" + k + "=" + mTargetCaluate.tppara[i] + "\n");
  712. //sb.append(" ");
  713. }
  714. WriteParaToProp();
  715.  
  716. try {
  717. FileOutputStream fos = new FileOutputStream(file);
  718. fos.write(sb.toString().getBytes());
  719. fos.flush();
  720. fos.getFD().sync();
  721. fos.close();
  722. } catch (FileNotFoundException e) {
  723. Log.e(TAG, "Cannot open file " + file);
  724. } catch (IOException e) {
  725. Log.e(TAG, "Cannot write file " + file);
  726. }
  727. }*/
  728.  
  729. public void dumpCalData(File file)
  730. {
  731. WriteParaToProp();
  732. }
  733.  
  734.  
  735. @Override
  736. public boolean onTouchEvent(MotionEvent ev) {
  737. if (isFinished())
  738. return true;
  739. if (ev.getAction() != MotionEvent.ACTION_UP)
  740. return true;
  741.  
  742. if(mFileExist == false)
  743. {
  744. mTargetCaluate.mTargetPoints[mStep].calx = (int)ev.getRawX();
  745. mTargetCaluate.mTargetPoints[mStep].caly = (int)ev.getRawY();
  746. }
  747. else
  748. {
  749. float x;
  750. float y;
  751. float rawX;
  752. float rawY;
  753.  
  754. ReadParaFromProp();
  755.  
  756. rawX = ev.getRawX();
  757. rawY = ev.getRawY();
  758.  
  759. x = (mCurTpPara[6] * rawX - mCurTpPara[2])/(mCurTpPara[0] + mCurTpPara[1]);
  760. y = (mCurTpPara[6] * rawY - mCurTpPara[5])/(mCurTpPara[3] + mCurTpPara[4]);
  761. mTargetCaluate.mTargetPoints[mStep].calx = (int)x;
  762. mTargetCaluate.mTargetPoints[mStep].caly = (int)y;
  763. }
  764. mStep++;
  765. mContext.onCalTouchEvent(ev);
  766. return true;
  767. }
  768.  
  769. @Override
  770. protected void onDraw(Canvas canvas)
  771. {
  772. if (isFinished())
  773. return;
  774. canvas.drawColor(Color.BLACK);
  775. drawTarget(canvas, mTargetCaluate.mTargetPoints[mStep].x, mTargetCaluate.mTargetPoints[mStep].y);
  776. }
  777.  
  778. private void drawTarget(Canvas c, int x, int y) {
  779. Paint white = new Paint(Paint.ANTI_ALIAS_FLAG);
  780. Paint red = new Paint(Paint.ANTI_ALIAS_FLAG);
  781. white.setColor(Color.WHITE);
  782. red.setColor(Color.RED);
  783. c.drawLine(x - 20,y,x+20,y,white);
  784. c.drawLine(x,y - 20,x,y + 20,white);
  785. /*
  786. c.drawCircle(x, y, 13, red);
  787. c.drawCircle(x, y, 11, white);
  788. c.drawCircle(x, y, 9, red);
  789. c.drawCircle(x, y, 7, white);
  790. c.drawCircle(x, y, 5, red);
  791. c.drawCircle(x, y, 3, white);
  792. c.drawCircle(x, y, 1, red);
  793. */
  794. }
  795. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement