动态手势识别--手势采集与手势匹配界面(四)

MainActivity:

public class MainActivity extends ActionBarActivity implements SensorEventListener, OnCheckedChangeListener, OnTouchListener ,OnClickListener { List model=new ArrayList(); List sensorDatas; private TextView gyroView, accView; private Switch s; private SensorManager sManager; private Sensor gyroSensor, accSensor; boolean switchIsChecked; private int SENSOR_RATE_MIDDLE = 10000;// 采样频率:100HZ List alist;// 存放加速度 List glist;// 存放角速度 List acctime; List gyrotime; List newglist = new ArrayList();// 存放修改过的角速度 List> sampleCollection=new ArrayList>();//存放输入的样本 不能没有new啊,不然取一次就报错 List initialsample=new ArrayList(); private OperateDatabase operateDatabase; private SharedPreferences sampleCollectionSizesp; private MyTask mTask; private static final String TAG = "ASYNC_TASK"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // textView组件用于显示角速度信息 gyroView = (TextView) findViewById(R.id.gyroView); accView = (TextView) findViewById(R.id.accView); s = (Switch) findViewById(R.id.switch1); s.setOnCheckedChangeListener(this);// 给switch按钮设置监听器 Button longButton = (Button) findViewById(R.id.longButton); longButton.setOnTouchListener(this);// 给长按按钮设置监听器 Button computeButton = (Button) findViewById(R.id.computeButton); computeButton.setOnClickListener(this); Button enterButton = (Button)findViewById(R.id.enterButton); enterButton.setOnClickListener(this); //数据库操作 operateDatabase=new OperateDatabase(MainActivity.this); //存入SharedPreferences sampleCollectionSizesp = MainActivity.this.getSharedPreferences("data",Context.MODE_PRIVATE); // 获取系统的传感器管理服务 sManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); gyroSensor = sManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE); accSensor = sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); } @Override // switch按钮被点击时,该方法会被执行 1)被点击的空间 2)被点击控件的状态 public void onCheckedChanged(CompoundButton button, boolean isChecked) { switchIsChecked = isChecked; } @Override protected void onResume() { super.onResume(); } @Override protected void onStop() { // 解除监听器注册 sManager.unregisterListener(this);// this指的是sensorEventListener对象 super.onStop(); } @Override // 当传感器值改变时回调该方法 public void onSensorChanged(SensorEvent event) { if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE) { float[] args = event.values; glist.add(event.values);// glist中存角速度!!问题!!:共执行五次手势,每次都会把数据add到glist和alist中! gyrotime.add(System.nanoTime());//currentTimeMillis()把每次距离1970年1月1日0点0分0秒的毫秒数加入gyrotime的list StringBuffer gsb = new StringBuffer(); gsb.append("x轴角速度:" + args[0] + "\n"); gsb.append("y轴角速度:" + args[1] + "\n"); gsb.append("z轴角速度:" + args[2] + "\n"); gyroView.setText(gsb); } else if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { float[] args = event.values; alist.add(event.values);// alist中存加速度 acctime.add(System.nanoTime()); StringBuffer asb = new StringBuffer(); asb.append("x轴加速度:" + args[0] + "\n"); asb.append("y轴加速度:" + args[1] + "\n"); asb.append("z轴加速度:" + args[2] + "\n"); accView.setText(asb); //System.out.println(alist.toString());// 输出glist } } @Override // 当传感器精度发生改变时回调该方法 public void onAccuracyChanged(Sensor sensor, int accuracy) { } @Override public boolean onTouch(View view, MotionEvent event) { // TODO Auto-generated method stub if (event.getAction() == MotionEvent.ACTION_DOWN) { if (switchIsChecked) { alist = new ArrayList();// 存放加速度 glist = new ArrayList();// 存放角速度 acctime = new ArrayList(); gyrotime = new ArrayList(); sManager.registerListener(this, gyroSensor, SENSOR_RATE_MIDDLE);// this指的是sensorEventListener对象!!!! sManager.registerListener(this, accSensor, SENSOR_RATE_MIDDLE);// SENSOR_RATE_MIDDLE定义为80HZ } } else if (event.getAction() == MotionEvent.ACTION_UP) { sManager.unregisterListener(this); DTW dtw=new DTW(); newglist=dtw.linearInsert(acctime,gyrotime,alist,glist); //new一下sensorDatas 每次置空 sensorDatas = new ArrayList(); for (int i = 0; i < newglist.size(); i++) {//删了 new SensorData sensorData = new SensorData(alist.get(i)[0], alist.get(i)[1], alist.get(i)[2], newglist.get(i)[0], newglist.get(i)[1], newglist.get(i)[2]); sensorDatas.add(sensorData); } //把采样结果加入到list中 sampleCollection.add(sensorDatas); } return true; } @Override public void onClick(View v){//开始计算模型 == computeButton switch(v.getId()){ case R.id.computeButton: //把sampleCollectionsize写入sp Editor editor = sampleCollectionSizesp.edit(); editor.putInt("size", sampleCollection.size()); editor.commit(); System.out.println("sampleCollection's length: " +sampleCollection.size());//到这可以运行 不会发生ANR,一写sampleCollection就ANR mTask = new MyTask(); mTask.execute(); //把sampleCollection写入数据库 break; case R.id.enterButton: Intent intent=new Intent(MainActivity.this,MatchingActivity.class); startActivity(intent); break; } } private class MyTask extends AsyncTask{ @Override protected void onPreExecute() { Log.i(TAG, "onPreExecute() called"); } @Override protected String doInBackground(String... arg0) { Log.i(TAG, "doInBackground(Params... params) called"); try { operateDatabase.addsampleCollection(sampleCollection); //输出sampleCollection的长度,logcat里说是0,为啥啊!!! ComputeModel cMode=new ComputeModel(); initialsample=cMode.selectinitialsample(sampleCollection); //有问题 ,一调就返回home界面!! 妈的 这为啥是0了! //调一下压缩扩张方法 for(int i=0;i<5;i++){ if(sampleCollection.get(i).size()!=initialsample.size()){//与初始模板长度比较 cMode.expansionORcompression(sampleCollection.get(i),initialsample); } } model=cMode.avgSample(sampleCollection,initialsample); //此sampleCollection是扩张压缩过后的!! 再调一下求平均值方法,计算出模板 //把model写入文件 //这时的sampleCollection不用读文件里的 operateDatabase.addModel(model); }catch (Exception e) { Log.e(TAG, e.getMessage()); } // TODO Auto-generated method stub return null; } @Override protected void onPostExecute(String result) { super.onPostExecute(result); Toast.makeText(MainActivity.this, "Compute Over", Toast.LENGTH_SHORT).show(); } } }

 

MatchingActivity:

public class MatchingActivity extends ActionBarActivity implements SensorEventListener, OnCheckedChangeListener, OnTouchListener,OnClickListener { private TextView gyroView, accView; private Switch s; private SensorManager sManager; private Sensor gyroSensor, accSensor; public List sensorDatas; boolean switchIsChecked; private int SENSOR_RATE_MIDDLE = 10000;// 采样频率:100HZ List alist;// 存放加速度 List glist;// 存放角速度 List acctime; List gyrotime; float n; List newglist = new ArrayList();// 存放修改过的角速度 private OperateDatabase operateDatabase; private SharedPreferences sampleCollectionSizesp; private MyTask mTask; private static final String TAG = "ASYNC_TASK"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.matching); // textView组件用于显示角速度信息 gyroView = (TextView) findViewById(R.id.gyroView); accView = (TextView) findViewById(R.id.accView); s = (Switch) findViewById(R.id.switch1); s.setOnCheckedChangeListener(this);// 给switch按钮设置监听器 Button longButton = (Button) findViewById(R.id.longButton); longButton.setOnTouchListener(this);// 给长按按钮设置监听器 Button matchingButton = (Button) findViewById(R.id.matchingButton); matchingButton.setOnClickListener(this); sampleCollectionSizesp = MatchingActivity.this.getSharedPreferences("data",Context.MODE_PRIVATE); // 获取系统的传感器管理服务 sManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); gyroSensor = sManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE); accSensor = sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); //数据库操作 operateDatabase=new OperateDatabase(MatchingActivity.this); } @Override // switch按钮被点击时,该方法会被执行 1)被点击的空间 2)被点击控件的状态 public void onCheckedChanged(CompoundButton button, boolean isChecked) { switchIsChecked = isChecked; } @Override protected void onResume() { super.onResume(); } @Override protected void onStop() { // 解除监听器注册 sManager.unregisterListener(this);// 需要两次吗??????不用!!!!!!this指的是什么?指的是sensorEventListener对象!!!!不是两个传感器! super.onStop(); } @Override // 当传感器值改变时回调该方法 public void onSensorChanged(SensorEvent event) { if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE) { float[] args = event.values; glist.add(event.values);// glist中存角速度 gyrotime.add(System.nanoTime()); StringBuffer gsb = new StringBuffer(); gsb.append("x轴角速度:" + args[0] + "\n"); gsb.append("y轴角速度:" + args[1] + "\n"); gsb.append("z轴角速度:" + args[2] + "\n"); gyroView.setText(gsb); } else if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { float[] args = event.values; alist.add(event.values);// alist中存加速度 acctime.add(System.nanoTime()); StringBuffer asb = new StringBuffer(); asb.append("x轴加速度:" + args[0] + "\n"); asb.append("y轴加速度:" + args[1] + "\n"); asb.append("z轴加速度:" + args[2] + "\n"); accView.setText(asb); } } @Override // 当传感器精度发生改变时回调该方法 public void onAccuracyChanged(Sensor sensor, int accuracy) { } @Override public boolean onTouch(View view, MotionEvent event) { // TODO Auto-generated method stub if (event.getAction() == MotionEvent.ACTION_DOWN) { if (switchIsChecked) { alist = new ArrayList();// 存放加速度 glist = new ArrayList();// 存放角速度 acctime = new ArrayList(); gyrotime = new ArrayList(); sManager.registerListener(this, gyroSensor, SENSOR_RATE_MIDDLE);// this指的是sensorEventListener对象!!!! sManager.registerListener(this, accSensor, SENSOR_RATE_MIDDLE);// SENSOR_RATE_MIDDLE定义为80HZ } } else if (event.getAction() == MotionEvent.ACTION_UP) { sManager.unregisterListener(this); DTW dtw=new DTW(); newglist=dtw.linearInsert(acctime,gyrotime,alist,glist);//alist+newglist //改2!!!!!!!new一下sensorDatas 每次置空 sensorDatas = new ArrayList(); for (int i = 0; i < newglist.size(); i++) { SensorData sensorData = new SensorData(alist.get(i)[0], alist.get(i)[1], alist.get(i)[2], newglist.get(i)[0], newglist.get(i)[1], newglist.get(i)[2]); sensorDatas.add(sensorData); } } return true; } @Override public void onClick(View v){//开始匹配模版 == matchingButton mTask = new MyTask(); mTask.execute(); } private class MyTask extends AsyncTask{ boolean isSucceed = false; @Override protected void onPreExecute() { Log.i(TAG, "onPreExecute() called"); } @Override protected String doInBackground(String... arg0) { Log.i(TAG, "doInBackground(Params... params) called"); try { int sampleCollectionSize = sampleCollectionSizesp.getInt("size",0); List model=operateDatabase.getModel(); System.out.println("model size=initial sample size?: "+model.size());//证明model从数据库获取的是对的 //一开始的sampleCollection,没压缩扩张过的 List> sampleCollection=operateDatabase.getSampleCollection(sampleCollectionSize); DTW dtw=new DTW(); //把n和threshold显示一下 进行比较 看看Q应该取什么值? 或者哦 把threshold显示一下 来了Q是多少 他的范围在[1.3,3.0]之间 n=dtw.getDistance(model, sensorDatas);//model为计算的模板 float q=1.9875f;//q待定!!! 3太大了 1号手势认证输二号手势总能认证成功 //2.5还可以----区间可能是[1.3,3.0] float threshold=q*((dtw.getDistance(model, sampleCollection.get(0)) +dtw.getDistance(model, sampleCollection.get(1)) +dtw.getDistance(model, sampleCollection.get(2)) +dtw.getDistance(model, sampleCollection.get(3)) +dtw.getDistance(model, sampleCollection.get(4)))/5); //显示一下threshold System.out.println("threshold : "+threshold); System.out.println("n : "+n); if(n<0){ //认证失败 isSucceed = false; } else{ if(n

你可能感兴趣的:(动态手势识别--手势采集与手势匹配界面(四))