2525import android .util .Log ;
2626import android .view .Surface ;
2727
28- import com .google .android .cameraview .CameraView ;
28+ import com .otaliastudios .cameraview .CameraView ;
29+ import com .otaliastudios .cameraview .Facing ;
30+ import com .otaliastudios .cameraview .Frame ;
31+ import com .otaliastudios .cameraview .FrameProcessor ;
32+ import com .otaliastudios .cameraview .Size ;
2933
3034import org .havenapp .main .PreferenceManager ;
3135import org .havenapp .main .Utils ;
4650import java .util .concurrent .ThreadPoolExecutor ;
4751import java .util .concurrent .TimeUnit ;
4852
49- import androidx .renderscript . RenderScript ;
53+ import androidx .annotation . NonNull ;
5054
5155import io .github .silvaren .easyrs .tools .Nv21Image ;
5256
@@ -102,7 +106,7 @@ public class CameraViewHolder {
102106 private File videoFile ;
103107
104108 //for managing bitmap processing
105- private RenderScript renderScript ;
109+ // private RenderScript renderScript;
106110
107111 private ServiceConnection mConnection = new ServiceConnection () {
108112
@@ -123,12 +127,11 @@ public CameraViewHolder(Activity context, CameraView cameraView) {
123127 //super(context);
124128 this .context = context ;
125129 this .cameraView = cameraView ;
126- this .renderScript = RenderScript .create (context ); // where context can be your activity, application, etc.
130+ // this.renderScript = RenderScript.create(context); // where context can be your activity, application, etc.
127131
128132 prefs = new PreferenceManager (context );
129133
130134 task = new MotionDetector (
131- renderScript ,
132135 updateHandler ,
133136 motionSensitivity );
134137
@@ -202,28 +205,37 @@ public void addListener(MotionDetector.MotionListener listener) {
202205 * (preferred is 640x480)
203206 * in order to minimize CPU usage
204207 */
205- public synchronized void startCamera () {
208+ public void startCamera () {
206209
207210
208211 updateCamera ();
209212
210- cameraView .start ();
213+ cameraView .open ();
211214
212- cameraView .setOnFrameListener ((data , width , height , rotationDegrees ) -> {
215+ cameraView .addFrameProcessor (new FrameProcessor () {
216+ @ Override
217+ public void process (@ NonNull Frame frame ) {
213218
214- long now = System .currentTimeMillis ();
215- if (now < CameraViewHolder .this .lastTimestamp + PREVIEW_INTERVAL )
216- return ;
219+ long now = System .currentTimeMillis ();
220+ if (now < CameraViewHolder .this .lastTimestamp + PREVIEW_INTERVAL )
221+ return ;
217222
218- CameraViewHolder .this .lastTimestamp = now ;
223+ CameraViewHolder .this .lastTimestamp = now ;
219224
220- if (! doingVideoProcessing ) {
225+ if (frame . getData () != null && frame . getSize () != null ) {
221226
222- Log .i ("CameraViewHolder" , "Processing new image" );
227+ byte [] data = frame .getData ();
228+ Size size = frame .getSize ();
229+ int width = size .getWidth ();
230+ int height = size .getHeight ();
231+ int rot = getCorrectCameraOrientation (cameraView .getFacing (),frame .getRotation ());
223232
224- mDecodeThreadPool .execute (() -> processNewFrame (data , width , height , rotationDegrees ));
225- } else {
226- mEncodeVideoThreadPool .execute (() -> recordNewFrame (data , width , height , rotationDegrees ));
233+ if (!doingVideoProcessing ) {
234+ mDecodeThreadPool .execute (() -> processNewFrame (data , width , height , rot ));
235+ } else {
236+ mEncodeVideoThreadPool .execute (() -> recordNewFrame (data , width , height , rot ));
237+ }
238+ }
227239 }
228240 });
229241
@@ -234,12 +246,10 @@ public void updateCamera ()
234246 {
235247 switch (prefs .getCamera ()) {
236248 case PreferenceManager .FRONT :
237- if (cameraView .getFacing () != CameraView .FACING_FRONT )
238- cameraView .setFacing (CameraView .FACING_FRONT );
249+ cameraView .setFacing (Facing .FRONT );
239250 break ;
240251 case PreferenceManager .BACK :
241- if (cameraView .getFacing () != CameraView .FACING_BACK )
242- cameraView .setFacing (CameraView .FACING_BACK );
252+ cameraView .setFacing (Facing .BACK );
243253 break ;
244254 default :
245255 // camera = null;
@@ -275,7 +285,8 @@ public void updateCamera ()
275285 private void recordNewFrame (byte [] data , int width , int height , int rotationDegrees )
276286 {
277287
278- Bitmap bitmap = Nv21Image .nv21ToBitmap (renderScript , data , width , height );
288+ Bitmap bitmap = MotionDetector .convertImage (data , width , height );
289+ //Nv21Image.nv21ToBitmap(renderScript, data, width, height);
279290
280291 bitmap = Bitmap .createBitmap (bitmap ,0 ,0 ,width ,height ,mtxVideoRotate ,true );
281292
@@ -321,8 +332,8 @@ private synchronized void processNewFrame (byte[] data, int width, int height, i
321332 data ,
322333 width ,
323334 height ,
324- cameraView . getDefaultOrientation () ,
325- cameraView .getFacing ());
335+ rotationDegrees ,
336+ cameraView .getFacing ()== Facing . FRONT );
326337
327338 lastPic = data ;
328339
@@ -349,12 +360,12 @@ private synchronized boolean recordVideo() {
349360
350361 mtxVideoRotate = new Matrix ();
351362
352- if (cameraView .getFacing () == CameraView . FACING_FRONT ) {
353- mtxVideoRotate .postRotate (-cameraView .getDefaultOrientation ());
363+ if (cameraView .getFacing () == Facing . FRONT ) {
364+ mtxVideoRotate .postRotate (-cameraView .getRotation ());
354365 mtxVideoRotate .postScale (-1 , 1 , cameraView .getWidth () / 2 , cameraView .getHeight () / 2 );
355366 }
356367 else
357- mtxVideoRotate .postRotate (cameraView .getDefaultOrientation ());
368+ mtxVideoRotate .postRotate (cameraView .getRotation ());
358369
359370 doingVideoProcessing = true ;
360371
@@ -374,13 +385,10 @@ private synchronized boolean recordVideo() {
374385 public synchronized void stopCamera ()
375386 {
376387 if (cameraView != null ) {
377- cameraView .stop ();
388+ cameraView .close ();
378389 }
379390 }
380391
381- public int getCameraFacing () {
382- return cameraView .getFacing ();
383- }
384392
385393 public void destroy ()
386394 {
@@ -391,7 +399,7 @@ public void destroy ()
391399 stopCamera ();
392400 }
393401
394- public int getCorrectCameraOrientation (int facing , int orientation ) {
402+ public int getCorrectCameraOrientation (Facing facing , int orientation ) {
395403
396404 int rotation = context .getWindowManager ().getDefaultDisplay ().getRotation ();
397405 int degrees = 0 ;
@@ -416,7 +424,7 @@ public int getCorrectCameraOrientation(int facing, int orientation) {
416424 }
417425
418426 int result ;
419- if (facing == CameraView . FACING_FRONT ){
427+ if (facing == Facing . FRONT ){
420428 result = (orientation + degrees ) % 360 ;
421429 result = (360 - result ) % 360 ;
422430 }else {
0 commit comments