简体   繁体   English

如何使用片段中的相机在Xamarin Android中捕获图像

[英]How to capture an image in xamarin android using the camera in fragment

I am a beginner in xamarin android.I want to take photo using my device camera,I am using the following code to to capture the image but, I got an exception when running the code 我是xamarin android的初学者。我想使用设备相机拍照,我正在使用以下代码来捕获图像,但是,运行代码时出现异常

Here is my Fragment.CS 这是我的Fragment.CS

  public class ScanFragments : Fragment
{
    ImageView imageView;

    public override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
    }

    public static ScanFragments NewInstance()
    {
        var scan = new ScanFragments { Arguments = new Bundle() };
        return scan;
    }


    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        var ignored = base.OnCreateView(inflater, container, savedInstanceState);

        imageView =View.FindViewById<ImageView>(Resource.Id.Iv_ScanImg);
        var btnCamera =View. FindViewById<Button>(Resource.Id.btnCamera);

        btnCamera.Click += BtnCamera_Click;


        return inflater.Inflate(Resource.Layout.ScanLayout, null);
    }

    private void BtnCamera_Click(object sender, EventArgs e)
    {
        Intent intent = new Intent(MediaStore.ActionImageCapture);
        StartActivityForResult(intent, 0);
    }
}

Well now I see your mistake its actually very simple you get the null reference because of the following reasons 好吧,现在我看到您的错误实际上非常简单,由于以下原因,您获得了空引用

1.OnCreateView does not contain a definition of View basically what I mean here is View is a class which you are using as an object of that class. 1.OnCreateView基本上不包含View的定义,我的意思是View是一个用作该类对象的类。

2.The actual way I recommend using fragments is first you define it an XML or an AXML as its foreground view in on OnCreateView as follows : 2.我建议使用片段的实际方法是,首先在OnCreateView上将其定义为XML或AXML作为其前景视图,如下所示:

 return inflater.Inflate(Resource.Layout.ScanLayout, null);

3.Override the OnViewCreated method and use its View to do all your work as follows : 3.重写OnViewCreated方法并使用其View来完成所有工作,如下所示:

 public override void OnViewCreated(View view, Bundle savedInstanceState)
{
    base.OnViewCreated(view, savedInstanceState);
    imageView =view.FindViewById<ImageView>(Resource.Id.Iv_ScanImg);
    var btnCamera =view. FindViewById<Button>(Resource.Id.btnCamera);

    btnCamera.Click += BtnCamera_Click;

}

4.For details related to the Android Activity and Fragment lifecycle check here 4,有关Android活动和片段生命周期的详细信息,请点击此处

Goodluck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用相机“圆圈”覆盖捕获图像并将其保存在 Xamarin.forms (Android) 中的路径上 - How Capture image using camera 'Circle' overlay and Save it on path in Xamarin.forms (Android) 如何使用Camera Xamarin Forms Android捕获多张照片 - How to Capture multiple photos with Camera xamarin Forms android 如何使用 xamarin android 打开前置摄像头 - How can I open front camera using xamarin android 如何在 android 项目中使用 xamarin 显示图像 - How to display image using xamarin in android project 如何使用Xamarin Android压缩图像但不调整大小? - How to Compress Image but not resize it Using Xamarin Android? 如何使用iOS相机拍照并使用Xamarin将图像保存到相机胶卷 - How to take a picture with iOS camera and save the image to camera roll using Xamarin 使用Xamarin Android在OCR上进行OCR矩形覆盖 - rectangle overlay on Camera for OCR using Xamarin Android 如何在 Android 或 IOS Unity 中设置从 Image Capture by Camera 读取的纹理? - How to set Texture readable from Image Capture by Camera in Android or IOS Unity? 使用Xamarin Android在片段中显示timerpickerdialog - Display timerpickerdialog within a fragment using Xamarin Android 如何不使用DirectShow从相机捕获数据? - How to capture data from camera NOT using DirectShow?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM