1

我想用 Android 传感器检测运动。例如,我只握住手机底部并将手机顶部向上移动。我想我需要采样算法。我可以编写一个简单的应用程序来记录传感器的数据。为了比较实时数据和记录数据,有没有库?如果我能做到的话,我对性能问题持怀疑态度。是否有不同的拘留运动路径?

4

2 回答 2

0

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

我修改了 RgbMotionDetection 类中的 isDifferent 方法来检测相机视图中心部分(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;
    }
于 2013-10-09T15:41:19.937 回答