简体   繁体   English

使用插件媒体捕获照片将屏幕显示为彩色矩形

[英]Capture photo using plug in media shows screen as colored retcangles

i am trying to capture the photo via camera using Media plugin.我正在尝试使用媒体插件通过相机拍摄照片。 i have written the code below.我已经写了下面的代码。 But the image captured shows colored rectangles as attached image.但是捕获的图像显示彩色矩形作为附加图像。 I have tried almost everything.我几乎尝试了一切。 but all the time it show the same thing as shown in the attached image.但它始终显示与附图所示相同的内容。

The button click for the photo capture is用于拍摄照片的按钮是

if (brepeat == false)
{
await CrossMedia.Current.Initialize();

            if (!CrossMedia.Current.IsTakePhotoSupported && !CrossMedia.Current.IsPickPhotoSupported)
            {
                await DisplayAlert("Error", "Photo Capture and pick not supported", "OK");
                return;
            }

            var varFilePhoto = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions()
            {
                                    Name = "Img" + DateTime.Now.ToString() + ".jpg"
            }); ;


            if (varFilePhoto == null)
                return;

            
            imgCapture.Source = ImageSource.FromStream(() => {
                return varFilePhoto.GetStream();
            });
        }

        brepeat = true;

MainActivity.cs is MainActivity.cs 是

protected override async void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(savedInstanceState);
        await CrossMedia.Current.Initialize();

        int requestPermissions = 1;
        string cameraPermission = Android.Manifest.Permission.Camera;

        if (!(ContextCompat.CheckSelfPermission(this, cameraPermission) == (int)Permission.Granted))
        {
            ActivityCompat.RequestPermissions(this, new String[] { cameraPermission, }, requestPermissions);
        }

        CrossCurrentActivity.Current.Init(this, savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
        LoadApplication(new App());
    }
    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
    {
        PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

        base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }

Android Manifest is Android 清单是

<?xml version="1.0" encoding="utf-8"?>




        <meta-data android:name="android.support.FILE_PROVIDER_PATHS"
               android:resource="@xml/file_paths"></meta-data>
    </provider>
</application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_INPUT_STATE" />

enter image description here在此处输入图像描述

If you can get the colored rectangles as you attached image, Your code is running normally.如果您可以在附加图像时获得彩色矩形,则您的代码运行正常。

You test your code in an Android emulator.您在 Android 仿真器中测试您的代码。 Because the emulator do not have physical camera, Android emulator provide this colored rectangles to simulating what you get when you turn on the physical camera.因为模拟器没有物理相机,Android 模拟器提供了这个彩色矩形来模拟你打开物理相机时得到的东西。

If you run your code in the physical android device, you can get the view that physical camera get.如果您在物理 android 设备中运行您的代码,您可以获得物理相机所获得的视图。

But is there any way to test it, where i can see what i am taking picture?但是有什么方法可以测试它,我可以在哪里看到我正在拍摄的照片?

If you want to see the taking picture, please add SaveToAlbum = true, in your take picture code like following code.(Note: If you want to get my simulate view of the camera, First of all, please update Android emulator in Android SDKs and Tools and use Android 9.0 or above emulator)如果您想看拍照,请在您的拍照代码中添加SaveToAlbum = true,如下代码。(注意:如果您想获得我的相机模拟视图,请首先更新Android SDK中的Android模拟器和工具和使用Android 9.0或以上模拟器)

  var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
            {
                Name = "Img" + DateTime.Now.ToString() + ".jpg",
                //Sets the properties of the photo file 
                SaveToAlbum = true,
               
            });

Here is running GIF.这里正在运行 GIF。 I take a picture, then find it in the temp folder(click the File app, select Image , choose the temp folder ).我拍了张照片,然后在临时文件夹中找到它(单击文件应用程序,select Image ,选择temp文件夹)。

在此处输入图像描述

eg.例如。 trying to scan something试图扫描一些东西

If you want to scan something, such as QR code, barcode, please use Android device to make a test.如果要扫描二维码、条码等,请使用Android设备进行测试。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM