Forum Xamarin.Android

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

OnImageAvailable seems to stop from being called after 100 frames in imageReader

Hi,I'm trying to develop a piece of code that reads the frames from the camera of the device and then process them.

Initially everything seems to work properly, after every frame the ImageReader.SetOnImageAvailableListener is called, but after 100 frames for some reason it stops calling up.

I can't understand why it happens. Can someone suggest me where the problem is? what am I doing wrong?
Below my code ...

First I create the manager and the imageReader in this way:

CameraManager manager = (CameraManager)Activity.GetSystemService (Context.CameraService);
try {
if (!cameraOpenCloseLock.TryAcquire(2500, TimeUnit.Milliseconds))
throw new RuntimeException("Time out waiting to lock camera opening.");

            string cameraId = manager.GetCameraIdList()[0];
            CameraCharacteristics characteristics = manager.GetCameraCharacteristics(cameraId);
            StreamConfigurationMap map = (StreamConfigurationMap)characteristics.Get(CameraCharacteristics.ScalerStreamConfigurationMap);
            videoSize = ChooseVideoSize(map.GetOutputSizes(Class.FromType(typeof(MediaRecorder))));
            previewSize = ChooseOptimalSize(map.GetOutputSizes(Class.FromType(typeof(MediaRecorder))),width,height,videoSize);

    mImageReader = ImageReader.NewInstance(videoSize.Width, videoSize.Height, ImageFormatType.Yuv420888, 2);
    mImageReader.SetOnImageAvailableListener(new ImageAvailableListener(), null);

            mediaRecorder = new MediaRecorder();
            manager.OpenCamera(cameraId,stateListener,null);

        } catch (CameraAccessException) {
            Toast.MakeText (Activity, "Cannot access the camera.", ToastLength.Short).Show ();
            Activity.Finish ();
        } catch (NullPointerException) {
            var dialog = new ErrorDialog ();
            dialog.Show (FragmentManager, "dialog");
        } catch (InterruptedException) {
            throw new RuntimeException ("Interrupted while trying to lock camera opening.");
        }
    }

Then I set up my mediaRecorder with this parameter:

        mediaRecorder.SetVideoSource (VideoSource.Surface);
        mediaRecorder.SetOutputFormat (OutputFormat.Mpeg4);
        mediaRecorder.SetOutputFile (GetVideoFile(Activity).AbsolutePath);
        mediaRecorder.SetVideoEncodingBitRate (256000);
        mediaRecorder.SetVideoFrameRate (30);
        mediaRecorder.SetVideoSize (videoSize.Width,videoSize.Height);
        mediaRecorder.SetVideoEncoder (VideoEncoder.Default);
        mediaRecorder.Prepare ();

Finally this is my class for the ImageAvailableListener :

public class ImageAvailableListener : Java.Lang.Object, ImageReader.IOnImageAvailableListener
{
private int cont = 0;

public void OnImageAvailable(ImageReader reader)
{
  Image image = null;
  try
  {

    Log.Info("Frame", "Frame Captured! " + cont++ );

  }
  catch (FileNotFoundException e)
  {
    Console.WriteLine(e.Message);
  }
  catch (IOException e)
  {
    Console.WriteLine(e.Message);
  }
  finally
  {
    if (image != null)
    {
      image.Close();
    }
  }
}

what am I doing wrong? Someone can Help me?

Answers

  • PoornaChandraPoornaChandra Member ✭✭

    Hi Daniel

    I am also working on something similar and even facing same issue. Currently in the OnImageAvailabelListener is getting called for around 50 frames, after when no frames are received. I have observed that the onError CameraDevice.StateCallback() is getting called.

    Can you please let me know did you find the route cause of this issue?

    Thanks in Advance.

Sign In or Register to comment.