2012-12-28 84 views
1

我想用Android傳感器偵測移動。例如,我只握住電話的底部並將電話的頂部向上移動。我想我需要採樣算法。我可以編寫一個簡單的應用程序來記錄傳感器的數據。爲了比較實時數據和記錄的數據,有沒有庫文件?如果我願意的話,我對性能問題持懷疑態度。檢測動作有不同的路徑嗎?Android移動偵測

回答

0

http://code.google.com/p/android-motion-detection/就是一個很好的例子。

我修改isDifferent方法在RgbMotionDetection類來檢測在相機視圖的中心部分(25%)的運動。

protected static boolean isDifferent(int[] first, int width, int height) { 
      if (first==null) throw new NullPointerException(); 

      if (mPrevious==null) return false; 
      if (first.length != mPrevious.length) return true; 
      if (mPreviousWidth != width || mPreviousHeight != height) return true; 

      int totDifferentPixels = 0; 
      int size = height * width; 

      int startHeight = height/4; 
      int endHeight = 3 * (height/4); 
      int startWidth = width/4; 
      int endWidth = 3 * (width/4); 
      int offSet = width/4; 

      Log.d("params", "start height " + startHeight + "end height " + endHeight + "start width " + startWidth + "end width " + endWidth); 

      Boolean offSetApplied; 

      for (int i = startHeight, ij=0; i < endHeight; i++) { 
       { 
        offSetApplied = false; 
        for (int j = startWidth; j < endWidth; j++, ij++) { 
          if (!offSetApplied){ 
           offSetApplied = true; 
           ij = startHeight * width + offSet; 
          } 

          int pix = (0xff & ((int)first[ij])); 
          int otherPix = (0xff & ((int)mPrevious[ij])); 

          //Catch any pixels that are out of range 
          if (pix < 0) pix = 0; 
          if (pix > 255) pix = 255; 
          if (otherPix < 0) otherPix = 0; 
          if (otherPix > 255) otherPix = 255; 

          if (Math.abs(pix - otherPix) >= mPixelThreshold) { 
            totDifferentPixels++; 
            //Paint different pixel red 
            //first[ij] = Color.RED; 
          } 
        } 
       } 
     } 



      if (totDifferentPixels <= 0) totDifferentPixels = 1; 
      //boolean different = totDifferentPixels > mThreshold; 

      int percent = 100/(size/totDifferentPixels); 

      //float percent = (float) totDifferentPixels/(float) size; 

      boolean different = percent > SENSITIVITY; 

      String output = "Number of different pixels: " + totDifferentPixels + "> " + percent + "%"; 
      if (different) { 
        Log.e(TAG, output); 
      } else { 
        Log.d(TAG, output); 
      } 

      return different; 
    }