Skip to content

Commit b937479

Browse files
committed
more improvements to video recording
1 parent 72011b8 commit b937479

File tree

3 files changed

+78
-19
lines changed

3 files changed

+78
-19
lines changed

src/main/java/org/havenapp/main/sensors/motion/CameraViewHolder.java

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import android.content.Intent;
1616
import android.content.ServiceConnection;
1717
import android.graphics.Bitmap;
18+
import android.graphics.Matrix;
1819
import android.hardware.Camera;
1920
import android.os.Environment;
2021
import android.os.Handler;
@@ -260,36 +261,48 @@ public void startCamera() {
260261
mEncodeVideoWorkQueue);
261262

262263

264+
private Matrix mtxVideoRotate;
265+
263266
private void recordNewFrame (byte[] data, int width, int height, int rotationDegrees)
264267
{
265268

266269
Bitmap bitmap = Nv21Image.nv21ToBitmap(renderScript, data, width, height);
270+
271+
bitmap = Bitmap.createBitmap(bitmap,0,0,width,height,mtxVideoRotate,true);
272+
267273
try {
268-
encoder.encodeImage(bitmap);
274+
if (encoder != null)
275+
encoder.encodeImage(bitmap);
276+
269277
bitmap.recycle();
270278

271279
} catch (Exception e) {
272280
e.printStackTrace();
273281
}
274282

275-
if (mEncodeVideoWorkQueue.isEmpty() && (!doingVideoProcessing)) {
276-
try {
277-
encoder.finish();
278283

279-
if (serviceMessenger != null) {
280-
Message message = new Message();
281-
message.what = EventTrigger.CAMERA_VIDEO;
282-
message.getData().putString(MonitorService.KEY_PATH, videoFile);
283-
try {
284-
serviceMessenger.send(message);
285-
} catch (RemoteException e) {
286-
e.printStackTrace();
287-
}
284+
285+
}
286+
287+
private void finishVideoEncoding ()
288+
{
289+
try {
290+
encoder.finish();
291+
292+
if (serviceMessenger != null) {
293+
Message message = new Message();
294+
message.what = EventTrigger.CAMERA_VIDEO;
295+
message.getData().putString(MonitorService.KEY_PATH, videoFile);
296+
try {
297+
serviceMessenger.send(message);
298+
} catch (RemoteException e) {
299+
e.printStackTrace();
288300
}
289-
} catch (IOException e) {
290-
e.printStackTrace();
291301
}
302+
} catch (IOException e) {
303+
e.printStackTrace();
292304
}
305+
293306
}
294307

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

312325
if (doingVideoProcessing)
313326
return false;
314-
315327
String ts1 = String.valueOf(new Date().getTime());
316328
videoFile = Environment.getExternalStorageDirectory() + File.separator + prefs.getImagePath() + File.separator + ts1 + ".mp4";
317329
try {
@@ -321,12 +333,26 @@ private synchronized boolean recordVideo() {
321333
e.printStackTrace();
322334
}
323335

324-
int seconds = prefs.getMonitoringTime() * 1000;
336+
mtxVideoRotate = new Matrix();
337+
338+
if (cameraView.getFacing() == CameraView.FACING_FRONT) {
339+
mtxVideoRotate.postRotate(-cameraView.getDefaultOrientation());
340+
mtxVideoRotate.postScale(-1, 1, cameraView.getWidth() / 2, cameraView.getHeight() / 2);
341+
}
342+
else
343+
mtxVideoRotate.postRotate(cameraView.getDefaultOrientation());
344+
325345
doingVideoProcessing = true;
346+
347+
int seconds = prefs.getMonitoringTime() * 1000;
326348
updateHandler.postDelayed(() -> {
327349
doingVideoProcessing = false;
350+
finishVideoEncoding();
328351
}, seconds);
329352

353+
for (MotionDetector.MotionListener listener : listeners)
354+
listener.onProcess(null, null, null, false);
355+
330356
return true;
331357
}
332358

@@ -386,4 +412,9 @@ public int getCorrectCameraOrientation(int facing, int orientation) {
386412
return result;
387413
}
388414

415+
public boolean doingVideoProcessing ()
416+
{
417+
return doingVideoProcessing;
418+
}
419+
389420
}

src/main/java/org/havenapp/main/ui/CameraFragment.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import android.view.View;
1717
import android.view.ViewGroup;
1818
import android.widget.ImageView;
19+
import android.widget.TextView;
1920

2021
import com.google.android.cameraview.CameraView;
2122

@@ -28,12 +29,18 @@ public final class CameraFragment extends Fragment {
2829
private CameraViewHolder cameraViewHolder;
2930
private ImageView newImage;
3031
private PreferenceManager prefs;
32+
private TextView txtCameraStatus;
3133

3234
@Override
3335
public View onCreateView(LayoutInflater inflater, ViewGroup container,
3436
Bundle savedInstanceState) {
3537

36-
return inflater.inflate(R.layout.camera_fragment, container, false);
38+
View view = inflater.inflate(R.layout.camera_fragment, container, false);
39+
40+
newImage = view.findViewById(R.id.new_image);
41+
txtCameraStatus = view.findViewById(R.id.camera_status_display);
42+
43+
return view;
3744

3845
}
3946

@@ -80,7 +87,6 @@ public void resetCamera ()
8087
private void initCamera ()
8188
{
8289

83-
newImage = getActivity().findViewById(R.id.new_image);
8490

8591
PreferenceManager prefs = new PreferenceManager(getActivity());
8692

@@ -97,6 +103,15 @@ private void initCamera ()
97103
newImage.setImageBitmap(newBitmap);
98104
else
99105
newImage.setImageResource(R.drawable.blankimage);
106+
107+
if (txtCameraStatus != null) {
108+
if (cameraViewHolder.doingVideoProcessing()) {
109+
txtCameraStatus.setText("Recording...");
110+
} else {
111+
txtCameraStatus.setText("");
112+
}
113+
}
114+
100115
});
101116
}
102117

src/main/res/layout/camera_fragment.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,17 @@
2828
android:background="@color/transparent"
2929
android:scaleType="fitXY"
3030
/>
31+
32+
<TextView
33+
android:id="@+id/camera_status_display"
34+
android:layout_width="wrap_content"
35+
android:layout_height="wrap_content"
36+
android:gravity="center"
37+
android:text=""
38+
android:textColor="@color/DarkRed"
39+
android:textSize="28sp"
40+
android:textStyle="bold"
41+
android:layout_gravity="center_horizontal|center_vertical"
42+
43+
/>
3144
</FrameLayout>

0 commit comments

Comments
 (0)