Skip to content

armgit / haven

Public Imported from GitHub
0

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

for #365 reduce camera resolution and remove one unneeded bitmap

n8fr806c4a8cdd780
diff --git a/src/main/java/org/havenapp/main/sensors/motion/MotionDetector.java b/src/main/java/org/havenapp/main/sensors/motion/MotionDetector.java
index e3daa13..04be1a3 100644
--- a/src/main/java/org/havenapp/main/sensors/motion/MotionDetector.java
+++ b/src/main/java/org/havenapp/main/sensors/motion/MotionDetector.java
@@ -40,8 +40,6 @@ public class MotionDetector {
private int motionSensitivity;
// Output data
- private boolean hasChanged;
-
private IMotionDetector detector;
//private RenderScript renderScript;
@@ -49,7 +47,7 @@ public class MotionDetector {
private int detectColor = Color.YELLOW;
public interface MotionListener {
- public void onProcess(Bitmap oldBitmap,
+ public void onProcess(
Bitmap newBitmap,
Bitmap rawBitmap,
boolean motionDetected);
@@ -91,24 +89,18 @@ public class MotionDetector {
int[] newPicLuma = ImageCodec.N21toLuma(rawNewPic, width, height);
if (rawOldPic != null) {
- int[] oldPicLuma = ImageCodec.N21toLuma(rawOldPic, width, height);
+
detector.setThreshold(motionSensitivity);
List<Integer> changedPixels =
- detector.detectMotion(oldPicLuma, newPicLuma, width, height);
- hasChanged = false;
-
- int[] newPic = ImageCodec.lumaToGreyscale(newPicLuma, width, height);
+ detector.detectMotion(ImageCodec.N21toLuma(rawOldPic, width, height), newPicLuma, width, height);
if (changedPixels != null) {
- hasChanged = true;
-
- }
+ int[] newPic = ImageCodec.lumaToGreyscale(newPicLuma, width, height);
+ newPicLuma = null;
- if (hasChanged) {
-
- Bitmap lastBitmap = ImageCodec.lumaToBitmapGreyscale(oldPicLuma, width, height);
+ System.gc();
for (int i = 0; i < newPic.length; i++)
newPic[i] = Color.TRANSPARENT;
@@ -118,6 +110,7 @@ public class MotionDetector {
}
+
Matrix mtx = new Matrix();
if (facingFront) {
@@ -127,20 +120,19 @@ public class MotionDetector {
else
mtx.postRotate(rotationDegrees);
-
Bitmap newBitmap
= Bitmap.createBitmap(Bitmap.createBitmap(newPic, width, height, Bitmap.Config.ARGB_4444), 0, 0, width, height, mtx, true);
+ newPic = null;
+
Bitmap rawBitmap = convertImage(rawNewPic,width,height);
- //Bitmap.createBitmap(Nv21Image.nv21ToBitmap(renderScript, rawNewPic, width, height),0,0,width,height,mtx,true);
handler.post(() -> {
for (MotionListener listener : listeners) {
listener.onProcess(
- lastBitmap,
newBitmap,
rawBitmap,
- hasChanged);
+ true);
}
});
@@ -153,8 +145,7 @@ public class MotionDetector {
listener.onProcess(
null,
null,
- null,
- hasChanged);
+ false);
}
});
diff --git a/src/main/java/org/havenapp/main/ui/CameraFragment.java b/src/main/java/org/havenapp/main/ui/CameraFragment.java
index 49ee4ec..cbfa444 100644
--- a/src/main/java/org/havenapp/main/ui/CameraFragment.java
+++ b/src/main/java/org/havenapp/main/ui/CameraFragment.java
@@ -18,11 +18,9 @@ import android.widget.TextView;
import com.otaliastudios.cameraview.Audio;
import com.otaliastudios.cameraview.CameraView;
-import com.otaliastudios.cameraview.SizeSelector;
import org.havenapp.main.PreferenceManager;
import org.havenapp.main.R;
-import org.havenapp.main.sensors.motion.CameraViewHolder;
import androidx.fragment.app.Fragment;
@@ -99,7 +97,7 @@ public final class CameraFragment extends Fragment {
if (cameraViewHolder == null) {
cameraViewHolder = new CameraViewHolder(getActivity(), cameraView);
- cameraViewHolder.addListener((oldBitmap, newBitmap, rawBitmap, motionDetected) -> {
+ cameraViewHolder.addListener((newBitmap, rawBitmap, motionDetected) -> {
if (motionDetected)
newImage.setImageBitmap(newBitmap);
else
diff --git a/src/main/java/org/havenapp/main/sensors/motion/CameraViewHolder.java b/src/main/java/org/havenapp/main/ui/CameraViewHolder.java
similarity index 92%
rename from src/main/java/org/havenapp/main/sensors/motion/CameraViewHolder.java
rename to src/main/java/org/havenapp/main/ui/CameraViewHolder.java
index fb87d1c..eb7ac3e 100644
--- a/src/main/java/org/havenapp/main/sensors/motion/CameraViewHolder.java
+++ b/src/main/java/org/havenapp/main/ui/CameraViewHolder.java
@@ -7,7 +7,7 @@
* Licensed under the MIT license.
*/
-package org.havenapp.main.sensors.motion;
+package org.havenapp.main.ui;
import android.app.Activity;
import android.content.ComponentName;
@@ -30,10 +30,13 @@ import com.otaliastudios.cameraview.Facing;
import com.otaliastudios.cameraview.Frame;
import com.otaliastudios.cameraview.FrameProcessor;
import com.otaliastudios.cameraview.Size;
+import com.otaliastudios.cameraview.SizeSelector;
import org.havenapp.main.PreferenceManager;
import org.havenapp.main.Utils;
import org.havenapp.main.model.EventTrigger;
+import org.havenapp.main.sensors.motion.LuminanceMotionDetector;
+import org.havenapp.main.sensors.motion.MotionDetector;
import org.havenapp.main.service.MonitorService;
import org.jcodec.api.android.AndroidSequenceEncoder;
@@ -52,8 +55,6 @@ import java.util.concurrent.TimeUnit;
import androidx.annotation.NonNull;
-import io.github.silvaren.easyrs.tools.Nv21Image;
-
public class CameraViewHolder {
/**
@@ -61,7 +62,8 @@ public class CameraViewHolder {
*/
private PreferenceManager prefs;
- private final static int PREVIEW_INTERVAL = 200;
+ private final static int DETECTION_INTERVAL_MS = 300;
+ private final static int MAX_CAMERA_WIDTH = 800;
private List<MotionDetector.MotionListener> listeners = new ArrayList<>();
@@ -135,10 +137,10 @@ public class CameraViewHolder {
updateHandler,
motionSensitivity);
- task.addListener((sourceImage, detectedImage, rawBitmap, motionDetected) -> {
+ task.addListener((detectedImage, rawBitmap, motionDetected) -> {
for (MotionDetector.MotionListener listener : listeners)
- listener.onProcess(sourceImage,detectedImage,rawBitmap,motionDetected);
+ listener.onProcess(detectedImage,rawBitmap,motionDetected);
if (motionDetected) {
@@ -210,6 +212,22 @@ public class CameraViewHolder {
updateCamera();
+ cameraView.setPlaySounds(false);
+ cameraView.setPreviewSize(new SizeSelector() {
+ @NonNull
+ @Override
+ public List<Size> select(@NonNull List<Size> source) {
+ ArrayList<Size> result = new ArrayList<>();
+
+ for (Size size : source)
+ {
+ if (size.getWidth()<MAX_CAMERA_WIDTH)
+ result.add(size);
+ }
+
+ return result;
+ }
+ });
cameraView.open();
cameraView.addFrameProcessor(new FrameProcessor() {
@@ -217,7 +235,7 @@ public class CameraViewHolder {
public void process(@NonNull Frame frame) {
long now = System.currentTimeMillis();
- if (now < CameraViewHolder.this.lastTimestamp + PREVIEW_INTERVAL)
+ if (now < CameraViewHolder.this.lastTimestamp + DETECTION_INTERVAL_MS)
return;
CameraViewHolder.this.lastTimestamp = now;
@@ -376,7 +394,7 @@ public class CameraViewHolder {
}, seconds);
for (MotionDetector.MotionListener listener : listeners)
- listener.onProcess(null, null, null, false);
+ listener.onProcess(null, null, false);
return true;
}