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
fixes for back and front camera overlay rotation
n8fr8decc74a1c497
diff --git a/src/main/java/org/havenapp/main/sensors/motion/MotionDetector.java b/src/main/java/org/havenapp/main/sensors/motion/MotionDetector.javaindex fb3a902..068b211 100644--- a/src/main/java/org/havenapp/main/sensors/motion/MotionDetector.java+++ b/src/main/java/org/havenapp/main/sensors/motion/MotionDetector.java@@ -18,6 +18,8 @@ import android.os.Handler;import android.support.v8.renderscript.RenderScript;import android.util.Log;+import com.google.android.cameraview.CameraView;+import org.havenapp.main.sensors.media.ImageCodec;import org.havenapp.main.sensors.motion.IMotionDetector;import org.havenapp.main.sensors.motion.LuminanceMotionDetector;@@ -46,7 +48,8 @@ public class MotionDetector {private int height;private Handler handler;private int motionSensitivity;-+ private int rotationDegrees;+ private int cameraFacing;// Output dataprivate boolean hasChanged;@@ -73,6 +76,8 @@ public class MotionDetector {byte[] rawNewPic,int width,int height,+ int rotationDegrees,+ int cameraFacing,Handler updateHandler,int motionSensitivity) {this.renderScript = renderScript;@@ -80,6 +85,8 @@ public class MotionDetector {this.rawNewPic = rawNewPic;this.width = width;this.height = height;+ this.rotationDegrees = rotationDegrees;+ this.cameraFacing = cameraFacing;this.handler = updateHandler;this.motionSensitivity = motionSensitivity;detector = new LuminanceMotionDetector();@@ -124,15 +131,20 @@ public class MotionDetector {Matrix mtx = new Matrix();- mtx.postRotate(-90);- mtx.postScale(-1, 1, width/2,height/2);++ if (cameraFacing == CameraView.FACING_FRONT) {+ mtx.postRotate(-90);+ mtx.postScale(-1, 1, width / 2, height / 2);+ }+ else+ mtx.postRotate(90);+Bitmap newBitmap= Bitmap.createBitmap(Bitmap.createBitmap(newPic, width, height, Bitmap.Config.ARGB_4444), 0, 0, width, height, mtx, true);Bitmap rawBitmap = Bitmap.createBitmap(Nv21Image.nv21ToBitmap(renderScript, rawNewPic, width, height),0,0,width,height,mtx,true);- Log.i("MotionDetector", "Finished processing, sending results");handler.post(() -> {for (MotionListener listener : listeners) {Log.i("MotionDetector", "Updating back view");diff --git a/src/main/java/org/havenapp/main/sensors/motion/Preview.java b/src/main/java/org/havenapp/main/sensors/motion/Preview.javaindex e0282c8..689d93d 100644--- a/src/main/java/org/havenapp/main/sensors/motion/Preview.java+++ b/src/main/java/org/havenapp/main/sensors/motion/Preview.java@@ -45,7 +45,6 @@ public class Preview {* Object to retrieve and set shared preferences*/private PreferenceManager prefs;- private int cameraFacing = 0;private final static int PREVIEW_INTERVAL = 500;@@ -118,6 +117,7 @@ public class Preview {motionSensitivity = prefs.getCameraSensitivity();initCamera();+/** We bind to the alert service*/@@ -146,45 +146,20 @@ public class Preview {public void initCamera() {- //if (cameraView != null)- // stopCamera();-- cameraView.start();-- /*- * The Surface has been created, acquire the camera and tell it where- * to draw.- * If the selected camera is the front one we open it- */switch (prefs.getCamera()) {case PreferenceManager.FRONT:- Camera.CameraInfo cameraInfo = new Camera.CameraInfo();- int cameraCount = Camera.getNumberOfCameras();- for (int camIdx = 0; camIdx < cameraCount; camIdx++) {- Camera.getCameraInfo(camIdx, cameraInfo);- if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {- try {- //camera = Camera.open(camIdx);- cameraFacing = Camera.CameraInfo.CAMERA_FACING_FRONT;- cameraView.setFacing(CameraView.FACING_FRONT);-- } catch (RuntimeException e) {- Log.e("Preview", "Camera failed to open: " + e.getLocalizedMessage());- }- }- }- break;+ cameraView.setFacing(CameraView.FACING_FRONT);+ break;case PreferenceManager.BACK:-- //camera = Camera.open();cameraView.setFacing(CameraView.FACING_BACK);- cameraFacing = Camera.CameraInfo.CAMERA_FACING_BACK;break;default:// camera = null;break;}+ cameraView.start();+cameraView.setOnFrameListener((data, width, height, rotationDegrees) -> {// processNewFrame(data, width, height);@@ -195,7 +170,7 @@ public class Preview {Log.i("Preview", "Processing new image");Preview.this.lastTimestamp = now;- mDecodeThreadPool.execute(() -> processNewFrame(data, width, height));+ mDecodeThreadPool.execute(() -> processNewFrame(data, width, height, rotationDegrees));});}@@ -212,7 +187,7 @@ public class Preview {mDecodeWorkQueue);- private void processNewFrame (byte[] data, int width, int height)+ private void processNewFrame (byte[] data, int width, int height, int rotationDegrees){@@ -222,6 +197,8 @@ public class Preview {data,width,height,+ rotationDegrees,+ cameraView.getFacing(),updateHandler,motionSensitivity);diff --git a/src/main/java/org/havenapp/main/ui/CameraFragment.java b/src/main/java/org/havenapp/main/ui/CameraFragment.javaindex e037c2a..1868d24 100644--- a/src/main/java/org/havenapp/main/ui/CameraFragment.java+++ b/src/main/java/org/havenapp/main/ui/CameraFragment.java@@ -86,30 +86,9 @@ public final class CameraFragment extends Fragment {CameraView cameraView = getActivity().findViewById(R.id.camera_view);cameraViewHolder = new Preview(getActivity(),cameraView);- // ((FrameLayout) getActivity().findViewById(R.id.cameraViewHolder)).addView(cameraViewHolder);-- // oldImage = (ImageView) getActivity().findViewById(R.id.old_image);newImage = getActivity().findViewById(R.id.new_image);cameraViewHolder.addListener((oldBitmap, newBitmap, rawBitmap, motionDetected) -> {-- /**- int rotation = 0;- boolean reflex = false;-- if (cameraViewHolder == null)- return;-- if (cameraViewHolder.getCameraFacing() == Camera.CameraInfo.CAMERA_FACING_BACK) {- rotation = 90;- } else {- rotation = 270;- reflex = true;- }-- // oldImage.setImageBitmap(ImageCodec.rotate(oldBitmap, rotation, reflex));- newImage.setImageBitmap(ImageCodec.rotate(newBitmap, rotation, reflex));- **/if (motionDetected)newImage.setImageBitmap(newBitmap);});