Skip to content

Commit

Permalink
Made the disable the motion detection when the phone is moving
Browse files Browse the repository at this point in the history
git-svn-id: https://android-motion-detection.googlecode.com/svn/trunk@22 996b93ed-3e4d-4b21-1044-74bd19560d1d
  • Loading branch information
[email protected] committed Dec 2, 2011
1 parent 562141e commit 90922f5
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="gen"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
<classpathentry kind="output" path="bin"/>
<classpathentry kind="output" path="bin/classes"/>
</classpath>
2 changes: 2 additions & 0 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

<activity android:name=".SensorsActivity"/>
</application>

<uses-feature android:name="android.hardware.camera"/>
Expand Down
58 changes: 30 additions & 28 deletions src/com/jwetherell/motion_detection/MotionDetectionActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import java.io.FileOutputStream;
import java.util.concurrent.atomic.AtomicBoolean;

import com.jwetherell.motion_detection.data.GlobalData;
import com.jwetherell.motion_detection.detection.AggregateLumaMotionDetection;
import com.jwetherell.motion_detection.detection.IMotionDetection;
import com.jwetherell.motion_detection.detection.LumaMotionDetection;
import com.jwetherell.motion_detection.detection.RgbMotionDetection;
import com.jwetherell.motion_detection.image.ImageProcessing;

import android.app.Activity;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.hardware.Camera;
Expand All @@ -23,23 +23,23 @@
import android.view.SurfaceHolder;
import android.view.SurfaceView;


/**
* This class extends Activity to handle a picture preview, process the frame for motion, and then save the
* file to the SD card.
* This class extends Activity to handle a picture preview, process the frame for motion, and then
* save the file to the SD card.
*
* @author Justin Wetherell <[email protected]>
*/
public class MotionDetectionActivity extends Activity {
public class MotionDetectionActivity extends SensorsActivity {
private static final String TAG = "MotionDetectionActivity";

private static SurfaceView preview = null;
private static SurfaceHolder previewHolder = null;
private static Camera camera = null;
private static boolean inPreview = false;
private static long mReferenceTime = 0;

private static IMotionDetection detector = null;

private static volatile AtomicBoolean processing = new AtomicBoolean(false);

/**
Expand Down Expand Up @@ -73,6 +73,20 @@ public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}

/**
* {@inheritDoc}
*/
@Override
public void onPause() {
super.onPause();

camera.setPreviewCallback(null);
if (inPreview) camera.stopPreview();
inPreview = false;
camera.release();
camera = null;
}

/**
* {@inheritDoc}
*/
Expand All @@ -83,20 +97,6 @@ public void onResume() {
camera = Camera.open();
}

/**
* {@inheritDoc}
*/
@Override
public void onPause() {
super.onPause();

camera.setPreviewCallback(null);
if (inPreview) camera.stopPreview();
inPreview = false;
camera.release();
camera = null;
}

private PreviewCallback previewCallback = new PreviewCallback() {
/**
* {@inheritDoc}
Expand All @@ -107,8 +107,10 @@ public void onPreviewFrame(byte[] data, Camera cam) {
Camera.Size size = cam.getParameters().getPreviewSize();
if (size == null) return;

DetectionThread thread = new DetectionThread(data,size.width,size.height);
thread.start();
if (!GlobalData.isPhoneInMotion()) {
DetectionThread thread = new DetectionThread(data,size.width,size.height);
thread.start();
}
}
};

Expand Down Expand Up @@ -169,8 +171,8 @@ private static Camera.Size getBestPreviewSize(int width, int height, Camera.Para

return result;
}
private class DetectionThread extends Thread {

private static final class DetectionThread extends Thread {
private byte[] data;
private int width;
private int height;
Expand All @@ -188,9 +190,8 @@ public DetectionThread(byte[] data, int width, int height) {
public void run() {
if (!processing.compareAndSet(false, true)) return;

Looper.prepare();
Log.d(TAG, "BEGIN PROCESSING...");
try {
try {
//Previous frame
int[] pre = null;
if (Globals.SAVE_PREVIOUS) pre = detector.getPrevious();
Expand Down Expand Up @@ -237,6 +238,7 @@ public void run() {
}

Log.i(TAG,"Saving.. previous="+previous+" original="+original+" bitmap="+bitmap);
Looper.prepare();
new SavePhotoTask().execute(previous,original,bitmap);
} else {
Log.i(TAG, "Not taking picture because not enough time has passed since the creation of the Surface");
Expand All @@ -245,15 +247,15 @@ public void run() {
} catch (Exception e) {
e.printStackTrace();
} finally {
processing.set(false);
processing.set(false);
}
Log.d(TAG, "END PROCESSING...");

processing.set(false);
}
};

private class SavePhotoTask extends AsyncTask<Bitmap, Integer, Integer> {
private static final class SavePhotoTask extends AsyncTask<Bitmap, Integer, Integer> {
/**
* {@inheritDoc}
*/
Expand Down
160 changes: 160 additions & 0 deletions src/com/jwetherell/motion_detection/SensorsActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package com.jwetherell.motion_detection;

import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import com.jwetherell.motion_detection.data.GlobalData;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;


/**
* This class extends Activity and processes sensor data and location data.
*
* @author Justin Wetherell <[email protected]>
*/
public class SensorsActivity extends Activity implements SensorEventListener {
private static final String TAG = "SensorsActivity";
private static final AtomicBoolean computing = new AtomicBoolean(false);

private static final float grav[] = new float[3]; //Gravity (a.k.a accelerometer data)
private static final float mag[] = new float[3]; //Magnetic

private static final float gravThreshold = 0.5f;
private static final float magThreshold = 1.0f;

private static SensorManager sensorMgr = null;
private static List<Sensor> sensors = null;
private static Sensor sensorGrav = null;
private static Sensor sensorMag = null;

private static float prevGrav = 0.0f;
private static float prevMag = 0.0f;

/**
* {@inheritDoc}
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

/**
* {@inheritDoc}
*/
@Override
public void onDestroy() {
super.onDestroy();
}

/**
* {@inheritDoc}
*/
@Override
public void onStart() {
super.onStart();

try {
sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);

sensors = sensorMgr.getSensorList(Sensor.TYPE_ACCELEROMETER);
if (sensors.size() > 0) sensorGrav = sensors.get(0);

sensors = sensorMgr.getSensorList(Sensor.TYPE_MAGNETIC_FIELD);
if (sensors.size() > 0) sensorMag = sensors.get(0);

sensorMgr.registerListener(this, sensorGrav, SensorManager.SENSOR_DELAY_NORMAL);
sensorMgr.registerListener(this, sensorMag, SensorManager.SENSOR_DELAY_NORMAL);
} catch (Exception ex1) {
try {
if (sensorMgr != null) {
sensorMgr.unregisterListener(this, sensorGrav);
sensorMgr.unregisterListener(this, sensorMag);
sensorMgr = null;
}
} catch (Exception ex2) {
ex2.printStackTrace();
}
}
}

/**
* {@inheritDoc}
*/
@Override
public void onStop() {
super.onStop();

try {
try {
sensorMgr.unregisterListener(this, sensorGrav);
} catch (Exception ex) {
ex.printStackTrace();
}
try {
sensorMgr.unregisterListener(this, sensorMag);
} catch (Exception ex) {
ex.printStackTrace();
}
sensorMgr = null;
} catch (Exception ex) {
ex.printStackTrace();
}
}

/**
* {@inheritDoc}
*/
@Override
public void onSensorChanged(SensorEvent evt) {
if (!computing.compareAndSet(false, true)) return;

if (evt.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
grav[0] = evt.values[0];
grav[1] = evt.values[1];
grav[2] = evt.values[2];
} else if (evt.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
mag[0] = evt.values[0];
mag[1] = evt.values[1];
mag[2] = evt.values[2];
}

float gravity = grav[0]+grav[1]+grav[2];
float magnetic = mag[0]+mag[1]+mag[2];

float gravDiff = Math.abs(gravity-prevGrav);
float magDiff = Math.abs(magnetic-prevMag);
//Log.i(TAG, "gravDiff="+gravDiff+" magDiff="+magDiff);

if ( (Float.compare(prevGrav,0.0f)!=0 && Float.compare(prevMag,0.0f)!=0) &&
(gravDiff>gravThreshold || magDiff>magThreshold) )
{
GlobalData.setPhoneInMotion(true);
} else {
GlobalData.setPhoneInMotion(false);
}

prevGrav = gravity;
prevMag = magnetic;

computing.set(false);
}

/**
* {@inheritDoc}
*/
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
if (sensor==null) throw new NullPointerException();

if(sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD && accuracy==SensorManager.SENSOR_STATUS_UNRELIABLE) {
Log.e(TAG, "Compass data unreliable");
}
}
}
21 changes: 21 additions & 0 deletions src/com/jwetherell/motion_detection/data/GlobalData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.jwetherell.motion_detection.data;

import java.util.concurrent.atomic.AtomicBoolean;

/**
* This class is used to store global data.
*
* @author Justin Wetherell <[email protected]>
*/
public class GlobalData {
private GlobalData() { };

private static final AtomicBoolean phoneInMotion = new AtomicBoolean(false);

public static boolean isPhoneInMotion() {
return phoneInMotion.get();
}
public static void setPhoneInMotion(boolean bool) {
phoneInMotion.set(bool);
}
}

0 comments on commit 90922f5

Please sign in to comment.