Skip to content

Commit

Permalink
fixes for back and front camera overlay rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
n8fr8 committed Jul 3, 2018
1 parent 7bf626c commit decc74a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 57 deletions.
20 changes: 16 additions & 4 deletions src/main/java/org/havenapp/main/sensors/motion/MotionDetector.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
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;
Expand Down Expand Up @@ -46,7 +48,8 @@ public class MotionDetector {
private int height;
private Handler handler;
private int motionSensitivity;

private int rotationDegrees;
private int cameraFacing;
// Output data

private boolean hasChanged;
Expand All @@ -73,13 +76,17 @@ public MotionDetector(
byte[] rawNewPic,
int width,
int height,
int rotationDegrees,
int cameraFacing,
Handler updateHandler,
int motionSensitivity) {
this.renderScript = renderScript;
this.rawOldPic = rawOldPic;
this.rawNewPic = rawNewPic;
this.width = width;
this.height = height;
this.rotationDegrees = rotationDegrees;
this.cameraFacing = cameraFacing;
this.handler = updateHandler;
this.motionSensitivity = motionSensitivity;
detector = new LuminanceMotionDetector();
Expand Down Expand Up @@ -124,15 +131,20 @@ public void detect() {


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");
Expand Down
41 changes: 9 additions & 32 deletions src/main/java/org/havenapp/main/sensors/motion/Preview.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -118,6 +117,7 @@ public Preview (Context context, CameraView cameraView) {
motionSensitivity = prefs.getCameraSensitivity();

initCamera();

/*
* We bind to the alert service
*/
Expand Down Expand Up @@ -146,45 +146,20 @@ public void addListener(MotionDetector.MotionListener listener) {
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);
Expand All @@ -195,7 +170,7 @@ public void initCamera() {
Log.i("Preview", "Processing new image");
Preview.this.lastTimestamp = now;

mDecodeThreadPool.execute(() -> processNewFrame(data, width, height));
mDecodeThreadPool.execute(() -> processNewFrame(data, width, height, rotationDegrees));
});

}
Expand All @@ -212,7 +187,7 @@ public void initCamera() {
mDecodeWorkQueue);


private void processNewFrame (byte[] data, int width, int height)
private void processNewFrame (byte[] data, int width, int height, int rotationDegrees)
{


Expand All @@ -222,6 +197,8 @@ private void processNewFrame (byte[] data, int width, int height)
data,
width,
height,
rotationDegrees,
cameraView.getFacing(),
updateHandler,
motionSensitivity);

Expand Down
21 changes: 0 additions & 21 deletions src/main/java/org/havenapp/main/ui/CameraFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,30 +86,9 @@ private void initCamera ()
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);
});
Expand Down

0 comments on commit decc74a

Please sign in to comment.