[英]android - How to take a photo and crop using ImageCropper.Forms?
我會在這里尋求幫助。我正在使用 xamarin 並希望:
我正在關注教程 -參考: https://github.com/stormlion227/ImageCropper.Forms
目前,當我點擊按鈕時,相機會打開,但不會裁剪或保存照片。 由於相機打開,這意味着我的設置和權限是正確的。
為什么此代碼不能從 github 工作,我該如何裁剪照片? 請讓我知道我在這里缺少什么。
進行一些調試后,以下代碼未運行。 我這個問題與屬性有關
Device.BeginInvokeOnMainThread(() =>
{
ImageURL.Source = ImageSource.FromFile(imageFile);
});
查看后端
public partial class AddCardPage : ContentPage
{
public AddCardPage()
{
InitializeComponent();
CrossMedia.Current.Initialize();
}
protected async void TapGestureRecognizerCommand(object sender, EventArgs e)
{
try
{
await CrossMedia.Current.Initialize();
await new ImageCropper()
{
PageTitle = "Test Title",
AspectRatioX = 1,
AspectRatioY = 1,
CropShape = ImageCropper.CropShapeType.Rectangle, //Cropt shape
SelectSourceTitle = "Select source",
TakePhotoTitle = "Take Photo",
PhotoLibraryTitle = "Photo Library",
Success = (imageFile) =>
{
Device.BeginInvokeOnMainThread(() =>
{
ImageURL.Source = ImageSource.FromFile(imageFile);
});
}
}.Show(this);
}
catch (Exception ex)
{
//System.Diagnostics.Debug.WriteLine("CameraException:>" + ex);
}
}//end of method
}//end of class
}
其他信息:我下載了 2 個 nuget: ImageCropper.Forms.Fix.v7和Xam.Media.Plugin
您使用的插件太舊了。 您可以改用ImageCropper.Forms.Fix.v2
。
在您的 MainActivity 中添加以下代碼:
Stormlion.ImageCropper.Droid.Platform.Init();
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
Stormlion.ImageCropper.Droid.Platform.OnActivityResult(requestCode, resultCode, data);
}
在AndroidManifest.xaml標簽中添加代碼:
<activity android:name="com.theartofdev.edmodo.cropper.CropImageActivity" android:theme="@style/Base.Theme.AppCompat" />
后面的代碼:
new ImageCropper()
{
// PageTitle = "Test Title",
// AspectRatioX = 1,
// AspectRatioY = 1,
Success = (imageFile) =>
{
Device.BeginInvokeOnMainThread(() =>
{
image.Source = ImageSource.FromFile(imageFile);
});
}
}.Show(this);
當你點擊按鈕時,它會彈出一個 window。 如果要拍照,請選擇Take Photo
。 請注意,您需要添加CAMERA
權限。
<uses-permission android:name="android.permission.CAMERA" />
或者您可以 select 從設備中裁剪圖像。
OutPut:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.