使用媒体插件拍摄照片显示屏幕为彩色矩形
Capture photo using plug in media shows screen as colored retcangles
我正在尝试使用媒体插件通过相机拍摄照片。我已经写了下面的代码。但是捕获的图像显示彩色矩形作为附加图像。我几乎尝试了一切。但它始终显示与附图中相同的内容。
点击拍照的按钮是
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 是
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 清单是
<?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
如果您可以在附加图像时得到彩色矩形,则您的代码正常 运行ning。
您在 Android 模拟器中测试您的代码。由于模拟器没有物理摄像头,Android 模拟器提供了这个彩色矩形来模拟打开物理摄像头时的效果。
如果您 运行 在物理 android 设备中编写代码,您可以获得物理相机获得的视图。
But is there any way to test it, where i can see what i am taking picture?
如果你想看到拍照,请在你的拍照代码中添加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,
});
这是运行宁 GIF。我拍照,然后在临时文件夹中找到它(单击文件应用程序,select Image
,选择 temp
文件夹)。
eg. trying to scan something
如果要扫描二维码、条码等,请使用Android设备进行测试。
我正在尝试使用媒体插件通过相机拍摄照片。我已经写了下面的代码。但是捕获的图像显示彩色矩形作为附加图像。我几乎尝试了一切。但它始终显示与附图中相同的内容。
点击拍照的按钮是
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 是
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 清单是
<?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
如果您可以在附加图像时得到彩色矩形,则您的代码正常 运行ning。
您在 Android 模拟器中测试您的代码。由于模拟器没有物理摄像头,Android 模拟器提供了这个彩色矩形来模拟打开物理摄像头时的效果。
如果您 运行 在物理 android 设备中编写代码,您可以获得物理相机获得的视图。
But is there any way to test it, where i can see what i am taking picture?
如果你想看到拍照,请在你的拍照代码中添加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,
});
这是运行宁 GIF。我拍照,然后在临时文件夹中找到它(单击文件应用程序,select Image
,选择 temp
文件夹)。
eg. trying to scan something
如果要扫描二维码、条码等,请使用Android设备进行测试。