Is there any property to capture image from Camera in the surface view?

I have the camera in the surface view (please refer the .axml file below) . I want to know if there is a way to click the image from the camera in the surface view and store it in another image view. Currently I am able to only stream/Display the camera preview. I want to capture the image may be on button click. Thanks in Advance

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="16dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
  <SurfaceView
      android:id="@+id/surface_view"
      android:layout_width="match_parent"
      android:layout_height="200sp" />

  <Button
      android:id="@+id/btnProcess"
      android:layout_below="@id/surface_view"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
     android:layout_marginTop = "15dp"
      android:text="Click and Capture !!" />
  <TextView
      android:id="@+id/txtview"
      android:text="No Text"
      android:textSize="20sp"
      android:textColor="#FFFF0000"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true" />



</RelativeLayout>




//MainActivity.cs
    namespace TextRecognitionbyCamera
    {
        [Activity(Label = "TextRecognitionbyCamera", MainLauncher = true, Theme ="@style/Theme.AppCompat.Light.NoActionBar")]
        public class MainActivity : AppCompatActivity, ISurfaceHolderCallback, IProcessor
        {
            private SurfaceView cameraView;
            private TextView txtView;
            private CameraSource cameraSource;
            private Button clickAndCaptureButton;
            private const int RequestCameraPermissionID = 1001;
            public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
            {
                switch (requestCode)
                {
                    case RequestCameraPermissionID:
                        {
                            if (grantResults[0] == Permission.Granted)
                            {
                                cameraSource.Start(cameraView.Holder);
                            }
                        }
                        break;
                }
            }
            protected override void OnCreate(Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);

                // Set our view from the "main" layout resource
                SetContentView(Resource.Layout.Main);
                cameraView = FindViewById<SurfaceView>(Resource.Id.surface_view);
                txtView = FindViewById<TextView>(Resource.Id.txtview);
          //      clickAndCaptureButton = FindViewById<Button>(Resource.Id.btnProcess);

                TextRecognizer txtRecognizer = new TextRecognizer.Builder(ApplicationContext).Build();
                if (!txtRecognizer.IsOperational)
                {
                    Log.Error("Main Activity", "Detector dependencies are not yet available");
                }
                else
                {
                    cameraSource = new CameraSource.Builder(ApplicationContext, txtRecognizer)
                        .SetFacing(CameraFacing.Back)
                        .SetRequestedPreviewSize(1280, 1024)
                        .SetRequestedFps(2.0f)
                        .SetAutoFocusEnabled(true)
                        .Build();

                    cameraView.Holder.AddCallback(this);

                    txtRecognizer.SetProcessor(this);
                }   
            }



            public void SurfaceChanged(ISurfaceHolder holder, [GeneratedEnum] Format format, int width, int height)
            {
                //do nothing
            }

            public void SurfaceCreated(ISurfaceHolder holder)
            {

                if (ActivityCompat.CheckSelfPermission(ApplicationContext, Manifest.Permission.Camera) != Android.Content.PM.Permission.Granted)
                {
                    //Request permission
                    ActivityCompat.RequestPermissions(this, new string[] {
                        Android.Manifest.Permission.Camera
                    }, RequestCameraPermissionID);
                    return;
                }

                cameraSource.Start(cameraView.Holder);
            }

            public void SurfaceDestroyed(ISurfaceHolder holder)
            {
                cameraSource.Stop();
            }

            public void Release()
            {
                throw new System.NotImplementedException();
            }
        }
    }

Best Answers

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    Are there any updates for this issue? If the reply is helpful, please try to mark it as an answer, it will help others who have similar issue.

  • PoorneshV15PoorneshV15 Member ✭✭

    Thank you but probably not the right solution. Trying the above solution I get an error that IPictureCallback is obsolete. Even I am finding the fix for this problem. I will keep you updated.

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    @PoorneshV15
    Are there any updates for this issue? If the reply is helpful, please try to mark it as an answer, it will help others who have similar issue.

Sign In or Register to comment.