.NET 8 LTS-ben MediaPicker-rel kép készítése
This commit is contained in:
@@ -1,40 +1,55 @@
|
||||
using CommunityToolkit.Maui.Views;
|
||||
using Microsoft.Maui.Controls;
|
||||
using System;
|
||||
using Microsoft.Maui.Storage;
|
||||
|
||||
namespace WorkFlowCheck.MAUI.Pages.Media;
|
||||
using Microsoft.Maui.Media;
|
||||
|
||||
|
||||
public partial class CaptureImagePage : ContentPage
|
||||
{
|
||||
public CaptureImagePage()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
public string PhotoPath { get; private set; }
|
||||
|
||||
private async Task TakePhotoAsync()
|
||||
public CaptureImagePage()
|
||||
{
|
||||
if (MediaPicker.Default.IsCaptureSupported)
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
|
||||
|
||||
private async void OnCaptureClicked(object sender, EventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
FileResult photo = await MediaPicker.Default.CapturePhotoAsync();
|
||||
|
||||
if (photo != null)
|
||||
// Ellenőrizzük, hogy a MediaPicker elérhető-e az eszközön
|
||||
if (MediaPicker.IsCaptureSupported)
|
||||
{
|
||||
// A kép elérési útja
|
||||
string localFilePath = Path.Combine(FileSystem.CacheDirectory, photo.FileName);
|
||||
// Kép készítése
|
||||
var photo = await MediaPicker.CapturePhotoAsync();
|
||||
|
||||
// A kép mentése a helyi fájlrendszerbe
|
||||
using Stream sourceStream = await photo.OpenReadAsync();
|
||||
using FileStream localFileStream = File.OpenWrite(localFilePath);
|
||||
await sourceStream.CopyToAsync(localFileStream);
|
||||
// A kép mentése a fájlrendszerbe
|
||||
var filePath = Path.Combine(FileSystem.CacheDirectory, "captured_photo.jpg");
|
||||
|
||||
// Esetleg megjelenítheted az UI-ban
|
||||
PhotoPath = localFilePath;
|
||||
// A kép átmásolása a kívánt helyre
|
||||
using (var stream = await photo.OpenReadAsync())
|
||||
using (var fileStream = File.OpenWrite(filePath))
|
||||
{
|
||||
await stream.CopyToAsync(fileStream);
|
||||
}
|
||||
|
||||
// A kép megjelenítése
|
||||
CapturedImage.Source = ImageSource.FromFile(filePath);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Ha a MediaPicker nem elérhető, hibaüzenet
|
||||
Console.WriteLine("A fényképezés nem támogatott ezen az eszközön.");
|
||||
}
|
||||
}
|
||||
else
|
||||
catch (Exception ex)
|
||||
{
|
||||
await Application.Current.MainPage.DisplayAlert("Hiba", "A kamerához való hozzáférés nem támogatott.", "OK");
|
||||
// Hiba kezelése
|
||||
Console.WriteLine($"Hiba történt a fényképezés közben: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user