如何在Camera2Basic API中获取图像的文件路径?

时间:2019-01-22 22:37:03

标签: xamarin xamarin.android camera

这肯定是一个愚蠢的问题,但请帮助我。我们如何在camera2basic api中获取图像文件路径,并在其他活动的imageview上显示图像?我一直在尝试获取项目中mFile的“绝对路径”,但未得到任何结果。 由于Camera2 api对我来说比较复杂。请帮我。

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);

    }

当我们按下按钮时,它会调用takepicture();里面有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();
        }
    }

我有showCapturePhoto,它正在从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));


}

0 个答案:

没有答案