Haven is for people who need a way to protect their personal spaces and possessions without compromising their own privacy, through an Android app and on-device sensors
Reorganized ImageCodec. Moved computational work from Preview to MotionAsyncTask.
Luca Bonato8cac88d59ad8
diff --git a/.gitignore b/.gitignoreindex 64d1a8e..370d518 100644--- a/.gitignore+++ b/.gitignore@@ -21,6 +21,7 @@ local.properties# Eclipse project files.classpath.project+lint.xml# Proguard folder generated by Eclipseproguard/diff --git a/src/me/ziccard/secureit/Preview.java b/src/me/ziccard/secureit/Preview.javaindex 89ae96f..95d616c 100644--- a/src/me/ziccard/secureit/Preview.java+++ b/src/me/ziccard/secureit/Preview.java@@ -9,7 +9,6 @@ import java.util.List;import me.ziccard.secureit.async.MotionAsyncTask;import me.ziccard.secureit.async.MotionAsyncTask.MotionListener;-import me.ziccard.secureit.codec.ImageCodec;import me.ziccard.secureit.motiondetection.LuminanceMotionDetector;import me.ziccard.secureit.service.BluetoothService;import android.content.ComponentName;@@ -53,7 +52,7 @@ public class Preview extends SurfaceView implements SurfaceHolder.Callback {/*** Last picture processed*/- private int[] lastPic;+ private byte[] lastPic;/*** True IFF there's an async task processing images*/@@ -213,7 +212,7 @@ public class Preview extends SurfaceView implements SurfaceHolder.Callback {YuvImage image = new YuvImage(data, parameters.getPreviewFormat(),size.width, size.height, null);- imageCount = (imageCount + 1)%(prefs.getMaxImages()+1);+ imageCount = (imageCount + 1)%(prefs.getMaxImages());File file = new File(Environment.getExternalStorageDirectory().getPath() +@@ -268,10 +267,7 @@ public class Preview extends SurfaceView implements SurfaceHolder.Callback {}});task.start();- lastPic = ImageCodec.N21toLuma(- data,- size.width,- size.height);+ lastPic = data;}}});diff --git a/src/me/ziccard/secureit/async/MotionAsyncTask.java b/src/me/ziccard/secureit/async/MotionAsyncTask.javaindex 9d0fe9a..b50d822 100644--- a/src/me/ziccard/secureit/async/MotionAsyncTask.java+++ b/src/me/ziccard/secureit/async/MotionAsyncTask.java@@ -24,7 +24,7 @@ public class MotionAsyncTask extends Thread {// Input dataprivate List<MotionListener> listeners = new ArrayList<MotionListener>();- private int[] lastPic;+ private byte[] rawOldPic;private byte[] rawNewPic;private int width;private int height;@@ -48,13 +48,13 @@ public class MotionAsyncTask extends Thread {}public MotionAsyncTask(- int[] lastPic,+ byte[] rawOldPic,byte[] rawNewPic,int width,int height,Handler updateHandler,int motionSensitivity) {- this.lastPic = lastPic;+ this.rawOldPic = rawOldPic;this.rawNewPic = rawNewPic;this.width = width;this.height = height;@@ -65,20 +65,19 @@ public class MotionAsyncTask extends Thread {@Overridepublic void run() {- int[] newPic = ImageCodec.N21toLuma(- rawNewPic,- width,- height);- if (lastPic == null) {- newBitmap = ImageCodec.lumaToGreyscale(newPic, width, height);+ int[] newPicLuma = ImageCodec.N21toLuma(rawNewPic, width, height);+ if (rawOldPic == null) {+ newBitmap = ImageCodec.lumaToBitmapGreyscale(newPicLuma, width, height);lastBitmap = newBitmap;} else {+ int[] oldPicLuma = ImageCodec.N21toLuma(rawOldPic, width, height);IMotionDetector detector = new LuminanceMotionDetector();detector.setThreshold(motionSensitivity);List<Integer> changedPixels =- detector.detectMotion(lastPic, newPic, width, height);+ detector.detectMotion(oldPicLuma, newPicLuma, width, height);hasChanged = false;+ int[] newPic = ImageCodec.lumaToGreyscale(newPicLuma, width, height);if (changedPixels != null) {hasChanged = true;for (int changedPixel : changedPixels) {@@ -86,8 +85,8 @@ public class MotionAsyncTask extends Thread {}}- lastBitmap = ImageCodec.lumaToGreyscale(lastPic, width, height);- newBitmap = ImageCodec.lumaToGreyscale(newPic, width, height);+ lastBitmap = ImageCodec.lumaToBitmapGreyscale(oldPicLuma, width, height);+ newBitmap = Bitmap.createBitmap(newPic, width, height, Bitmap.Config.RGB_565);}Log.i("MotionAsyncTask", "Finished processing, sending results");diff --git a/src/me/ziccard/secureit/codec/ImageCodec.java b/src/me/ziccard/secureit/codec/ImageCodec.javaindex 9b6f482..8c514d2 100644--- a/src/me/ziccard/secureit/codec/ImageCodec.java+++ b/src/me/ziccard/secureit/codec/ImageCodec.java@@ -3,7 +3,6 @@ package me.ziccard.secureit.codec;import java.io.ByteArrayOutputStream;import android.graphics.Bitmap;-import android.graphics.Color;import android.graphics.Matrix;public class ImageCodec {@@ -27,23 +26,27 @@ public class ImageCodec {}/**- * Converts a luminance matrix to a grayscale bitmap+ * Converts a luminance matrix to a RGB grayscale bitmap* @param lum* @param width* @param height* @return*/- public static Bitmap lumaToGreyscale(int[] lum, int width, int height) {+ public static int[] lumaToGreyscale(int[] lum, int width, int height) {if (lum==null) throw new NullPointerException();- Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);- for(int y=0, xy=0; y<bitmap.getHeight(); y++) {- for(int x=0; x<bitmap.getWidth(); x++, xy++) {- int luma = lum[xy];- bitmap.setPixel(x,y,Color.rgb(luma,luma,luma));- }+ int[] greyscale = new int[height*width];+ for (int ij=0; ij<greyscale.length; ij++) {+ // create the RGB-grey color corresponding to the specified luma component+ greyscale[ij] = ((((lum[ij]<<8)|lum[ij])<<8)|lum[ij])&0x00FFFFFF;}- return bitmap;+ return greyscale;+ }++ public static Bitmap lumaToBitmapGreyscale(int[] lum, int width, int height) {+ if (lum == null) throw new NullPointerException();++ return Bitmap.createBitmap(ImageCodec.lumaToGreyscale(lum, width, height), width, height, Bitmap.Config.RGB_565);}/**