使用传感器检测运动时如何拍照

时间:2016-07-07 12:11:37

标签: android android-fragments fragment android-camera android-sensors

我想开发一个可以使用ACCELEROMETER或PROXIMITY Sensor拍照的应用程序。我可以使用按钮拍照,但是当我调用相同的方法时,public BigInteger squareDigits(int n) { BigInteger tmp = null; String returnvalue = ""; while (n > 0) { tmp = BigInteger.valueOf(n % 10); tmp = tmp.pow(2); returnvalue = returnvalue + String.valueOf(tmp); n /= 10; } return new BigInteger(returnvalue); } ... System.out.println(squareDigits(123456789)); // Print 816449362516941 来自camera.takePicture。请帮帮我

1 个答案:

答案 0 :(得分:0)

我没有测试过,但它应该可以工作:

public class Main extends Activity implements SensorEventListener {
    private SensorManager senSensorManager;
    private Sensor senAccelerometer;

    private long lastUpdate = 0;
    private float last_x, last_y, last_z;
    private static final int SHAKE_THRESHOLD = 600;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        senSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        senAccelerometer = senSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        senSensorManager.registerListener(this, senAccelerometer , SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        Sensor mySensor = sensorEvent.sensor;

        if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            float x = sensorEvent.values[0];
            float y = sensorEvent.values[1];
            float z = sensorEvent.values[2];
            long curTime = System.currentTimeMillis();

            if ((curTime - lastUpdate) > 100) {
                long diffTime = (curTime - lastUpdate);
                lastUpdate = curTime;

                float speed = Math.abs(x + y + z - last_x - last_y - last_z)/ diffTime * 10000;

                if (speed > SHAKE_THRESHOLD) {
                     //CALL TAKE PHOTO FUNCTION
                }

                last_x = x;
                last_y = y;
                last_z = z;
            }

        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }

    //Disable sensor on hibernate
    protected void onPause() {
        super.onPause();
        senSensorManager.unregisterListener(this);
    }

    protected void onResume() {
        super.onResume();
        senSensorManager.registerListener(this, senAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
    }
}

您需要添加一些依赖于IDE的库