Skip to content

Commit

Permalink
for #365 reduce camera resolution and remove one unneeded bitmap
Browse files Browse the repository at this point in the history
  • Loading branch information
n8fr8 committed Feb 7, 2019
1 parent 272b341 commit 06c4a8c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 31 deletions.
31 changes: 11 additions & 20 deletions src/main/java/org/havenapp/main/sensors/motion/MotionDetector.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,14 @@ public class MotionDetector {
private int motionSensitivity;
// Output data

private boolean hasChanged;

private IMotionDetector detector;

//private RenderScript renderScript;

private int detectColor = Color.YELLOW;

public interface MotionListener {
public void onProcess(Bitmap oldBitmap,
public void onProcess(
Bitmap newBitmap,
Bitmap rawBitmap,
boolean motionDetected);
Expand Down Expand Up @@ -91,24 +89,18 @@ public void detect(byte[] rawOldPic,
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;
Expand All @@ -118,6 +110,7 @@ public void detect(byte[] rawOldPic,
}



Matrix mtx = new Matrix();

if (facingFront) {
Expand All @@ -127,20 +120,19 @@ public void detect(byte[] rawOldPic,
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);
}

});
Expand All @@ -153,8 +145,7 @@ public void detect(byte[] rawOldPic,
listener.onProcess(
null,
null,
null,
hasChanged);
false);
}

});
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/org/havenapp/main/ui/CameraFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@

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;

Expand Down Expand Up @@ -99,7 +97,7 @@ public void initCamera ()
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,10 +30,13 @@
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;

Expand All @@ -52,16 +55,15 @@

import androidx.annotation.NonNull;

import io.github.silvaren.easyrs.tools.Nv21Image;

public class CameraViewHolder {

/**
* Object to retrieve and set shared preferences
*/
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<>();

Expand Down Expand Up @@ -135,10 +137,10 @@ public CameraViewHolder(Activity context, CameraView cameraView) {
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) {

Expand Down Expand Up @@ -210,14 +212,30 @@ public void startCamera() {

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() {
@Override
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;
Expand Down Expand Up @@ -376,7 +394,7 @@ private synchronized boolean recordVideo() {
}, seconds);

for (MotionDetector.MotionListener listener : listeners)
listener.onProcess(null, null, null, false);
listener.onProcess(null, null, false);

return true;
}
Expand Down

0 comments on commit 06c4a8c

Please sign in to comment.