1515import android .content .Intent ;
1616import android .content .ServiceConnection ;
1717import android .graphics .Bitmap ;
18+ import android .graphics .Matrix ;
1819import android .hardware .Camera ;
1920import android .os .Environment ;
2021import 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}
0 commit comments