0

this must be a stupid question but please help me. How can we get the image file path in camera2basic api and display the image on the other activity's imageview? I have been trying to get the "Absolutepath" of the mFile in the project but not getting anything. As Camera2 api is relatively complex to understand for me. Please help me.

public override void OnActivityCreated(Bundle savedInstanceState)
    {
        base.OnActivityCreated(savedInstanceState);
        mFile = new Java.IO.File(Activity.GetExternalFilesDir(null), "pic.jpg");
        mCaptureCallback = new CameraCaptureListener(this);
        mOnImageAvailableListener = new ImageAvailableListener(this, mFile);

    }

when we press the button, it evokes takepicture(); which has lockfocus();

 private void LockFocus()
    {
        try
        {
            // This is how to tell the camera to lock focus.

            mPreviewRequestBuilder.Set(CaptureRequest.ControlAfTrigger, (int)ControlAFTrigger.Start);
            // Tell #mCaptureCallback to wait for the lock.
            mState = STATE_WAITING_LOCK;
            mCaptureSession.Capture(mPreviewRequestBuilder.Build(), mCaptureCallback,
                    mBackgroundHandler);

        }
        catch (CameraAccessException e)
        {
            e.PrintStackTrace();
        }
    }

I have showCapturePhoto which is getting the image from ImageAvailableListener

using Android.Media;
using Java.IO;
using Java.Lang;
using Java.Nio;
using Android.Util;

namespace Camera2Basic.Listeners
{
    public class ImageAvailableListener : Java.Lang.Object, 
ImageReader.IOnImageAvailableListener
{
    private readonly File file;
    private  Camera2BasicFragment owner;


    public ImageAvailableListener(Camera2BasicFragment fragment, File 
file)
    {
        if (fragment == null)
            throw new System.ArgumentNullException("fragment");
        if (file == null)
            throw new System.ArgumentNullException("file");

        owner = fragment;
        this.file = file;
    }


    //public File File { get; private set; }
    //public Camera2BasicFragment Owner { get; private set; }

    public void OnImageAvailable(ImageReader reader)
    {
        owner.mBackgroundHandler.Post(new ImageSaver(reader.AcquireNextImage(), file));

    }

    // Saves a JPEG {@link Image} into the specified {@link File}.
    private class ImageSaver : Java.Lang.Object, IRunnable
    {
        // The JPEG image
        private Image mImage;

        // The file we save the image into.
        private File mFile;

        public ImageSaver(Image image, File file)
        {
            if (image == null)
                throw new System.ArgumentNullException("image");
            if (file == null)
                throw new System.ArgumentNullException("file");

            mImage = image;
            mFile = file;
        }

        public void Run()
        {

            ByteBuffer buffer = mImage.GetPlanes()[0].Buffer;
            byte[] bytes = new byte[buffer.Remaining()];
            buffer.Get(bytes);
            using (var output = new FileOutputStream(mFile))
            {
                try
                {
                    output.Write(bytes);
                }
                catch (IOException e)
                {
                    e.PrintStackTrace();
                }
                finally
                {
                    mImage.Close();
                }
            }
            Camera2BasicFragment.showCapturedPhoto(mImage);

        }

    }


}

} ShowcapturePhoto

 public static void showCapturedPhoto(Image img)
    {
        ByteBuffer buffer;
        byte[] bytes;

        MemoryStream memStream = new MemoryStream();

            buffer = img.GetPlanes()[0].Buffer;
            bytes = new byte[buffer.Capacity()];
            buffer.Get(bytes);


        Activity activity= new Activity();

     Intent showPhoto = new Intent(activity, typeof(RetryOK));

    showPhoto.PutExtra("savedImg", bytes);
    showPhoto.PutExtra("zoomAmount", 1.7f / 1.4f);
    showPhoto.PutExtra("focusDistance", -1.0f);


       activity.StartActivity(typeof(RetryOK));


}
  • Have you checked the mFile property? Is it being correctly filled? – Bruno Caceiro Jan 22 '19 at 23:05
  • @BrunoCaceiro the image is being saved to the phone directory but when I try to display it on the other activity through PutExtra it shows empty string. https://github.com/xamarin/monodroid-samples/tree/master/android5.0/Camera2Basic I have added another method in the cameracapturestillpicturesession which is listeners folder but still no success. Code is so huge, I cant even post it here. –  Jan 22 '19 at 23:13
  • Check this answer https://stackoverflow.com/questions/45343217/xamarin-camera2basic-sample-throws-exception-after-unlockfocus-call – Bruno Caceiro Jan 22 '19 at 23:15
  • Add some code that would help me – FreakyAli Jan 23 '19 at 02:20
  • @G.hakim please have a look. –  Jan 23 '19 at 02:28
  • @BrunoCaceiro the above link did not help me. Have a look at this code. –  Jan 23 '19 at 02:28
  • you could look at this ,maybe help you :https://stackoverflow.com/questions/28003186/capture-picture-without-preview-using-camera2-api – Leo Zhu Jan 23 '19 at 04:56
  • @PrashantBhandari Can you explain in brief what are you trying to achieve so maybe I can answer this quickly? – FreakyAli Jan 23 '19 at 05:35
  • I am actually trying to get the image to display on the other activity's ImageView. For that I need to get the path of the mFile I suppose. –  Jan 23 '19 at 06:29
  • mFile from the `ImageSaver` class? Also are you calling it from your Activity? – FreakyAli Jan 23 '19 at 06:42
  • I solved it just now. I made few changes in the code but was finally able to call mFile. Thanks alot guys –  Jan 23 '19 at 08:32

0 Answers0