Skip to content

Commit

Permalink
more improvements to video recording
Browse files Browse the repository at this point in the history
  • Loading branch information
n8fr8 committed Jul 6, 2018
1 parent 72011b8 commit b937479
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.hardware.Camera;
import android.os.Environment;
import android.os.Handler;
Expand Down Expand Up @@ -260,36 +261,48 @@ public void startCamera() {
mEncodeVideoWorkQueue);


private Matrix mtxVideoRotate;

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

Bitmap bitmap = Nv21Image.nv21ToBitmap(renderScript, data, width, height);

bitmap = Bitmap.createBitmap(bitmap,0,0,width,height,mtxVideoRotate,true);

try {
encoder.encodeImage(bitmap);
if (encoder != null)
encoder.encodeImage(bitmap);

bitmap.recycle();

} catch (Exception e) {
e.printStackTrace();
}

if (mEncodeVideoWorkQueue.isEmpty() && (!doingVideoProcessing)) {
try {
encoder.finish();

if (serviceMessenger != null) {
Message message = new Message();
message.what = EventTrigger.CAMERA_VIDEO;
message.getData().putString(MonitorService.KEY_PATH, videoFile);
try {
serviceMessenger.send(message);
} catch (RemoteException e) {
e.printStackTrace();
}

}

private void finishVideoEncoding ()
{
try {
encoder.finish();

if (serviceMessenger != null) {
Message message = new Message();
message.what = EventTrigger.CAMERA_VIDEO;
message.getData().putString(MonitorService.KEY_PATH, videoFile);
try {
serviceMessenger.send(message);
} catch (RemoteException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}

}

private synchronized void processNewFrame (byte[] data, int width, int height, int rotationDegrees)
Expand All @@ -311,7 +324,6 @@ private synchronized boolean recordVideo() {

if (doingVideoProcessing)
return false;

String ts1 = String.valueOf(new Date().getTime());
videoFile = Environment.getExternalStorageDirectory() + File.separator + prefs.getImagePath() + File.separator + ts1 + ".mp4";
try {
Expand All @@ -321,12 +333,26 @@ private synchronized boolean recordVideo() {
e.printStackTrace();
}

int seconds = prefs.getMonitoringTime() * 1000;
mtxVideoRotate = new Matrix();

if (cameraView.getFacing() == CameraView.FACING_FRONT) {
mtxVideoRotate.postRotate(-cameraView.getDefaultOrientation());
mtxVideoRotate.postScale(-1, 1, cameraView.getWidth() / 2, cameraView.getHeight() / 2);
}
else
mtxVideoRotate.postRotate(cameraView.getDefaultOrientation());

doingVideoProcessing = true;

int seconds = prefs.getMonitoringTime() * 1000;
updateHandler.postDelayed(() -> {
doingVideoProcessing = false;
finishVideoEncoding();
}, seconds);

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

return true;
}

Expand Down Expand Up @@ -386,4 +412,9 @@ public int getCorrectCameraOrientation(int facing, int orientation) {
return result;
}

public boolean doingVideoProcessing ()
{
return doingVideoProcessing;
}

}
19 changes: 17 additions & 2 deletions src/main/java/org/havenapp/main/ui/CameraFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.cameraview.CameraView;

Expand All @@ -28,12 +29,18 @@ public final class CameraFragment extends Fragment {
private CameraViewHolder cameraViewHolder;
private ImageView newImage;
private PreferenceManager prefs;
private TextView txtCameraStatus;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

return inflater.inflate(R.layout.camera_fragment, container, false);
View view = inflater.inflate(R.layout.camera_fragment, container, false);

newImage = view.findViewById(R.id.new_image);
txtCameraStatus = view.findViewById(R.id.camera_status_display);

return view;

}

Expand Down Expand Up @@ -80,7 +87,6 @@ public void resetCamera ()
private void initCamera ()
{

newImage = getActivity().findViewById(R.id.new_image);

PreferenceManager prefs = new PreferenceManager(getActivity());

Expand All @@ -97,6 +103,15 @@ private void initCamera ()
newImage.setImageBitmap(newBitmap);
else
newImage.setImageResource(R.drawable.blankimage);

if (txtCameraStatus != null) {
if (cameraViewHolder.doingVideoProcessing()) {
txtCameraStatus.setText("Recording...");
} else {
txtCameraStatus.setText("");
}
}

});
}

Expand Down
13 changes: 13 additions & 0 deletions src/main/res/layout/camera_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,17 @@
android:background="@color/transparent"
android:scaleType="fitXY"
/>

<TextView
android:id="@+id/camera_status_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text=""
android:textColor="@color/DarkRed"
android:textSize="28sp"
android:textStyle="bold"
android:layout_gravity="center_horizontal|center_vertical"

/>
</FrameLayout>

0 comments on commit b937479

Please sign in to comment.