简体   繁体   中英

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

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

<?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. 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.

If you run your code in the physical android device, you can get the view that physical camera get.

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)

  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. I take a picture, then find it in the temp folder(click the File app, select Image , choose the temp folder ).

在此处输入图像描述

eg. trying to scan something

If you want to scan something, such as QR code, barcode, please use Android device to make a test.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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