You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET MAUI中Camera.MAUI拍摄照片存入SQL Server问题求助

MAUI使用Camera.MAUI拍摄照片并存储到SQL Server数据库

你的核心问题是未将拍摄的照片转换为字节数组并传递给存储API,当前代码仅将图片显示在界面上,没有保留可用于存储的原始数据。以下是具体解决方案:

关键修改步骤

1. 修正XAML中的错误绑定

CameraView的Cameras是只读属性,不能绑定Command,移除该错误绑定:

<VerticalStackLayout
 Spacing="25"
 Padding="30,0"
 VerticalOptions="Fill"
 HorizontalOptions="Fill"
>
   <cv:CameraView x:Name="cameraView" WidthRequest="300" HeightRequest="300" />
    <Button Text="Smile!" Command="{Binding CapturePhotoCommand}"/>
    <Image Source="{Binding ImgSrc}" WidthRequest="200" HeightRequest="200" />
    <Button Margin="20" Text="Save Photo" Command="{Binding SavePhotoCommand}"/>
</VerticalStackLayout>

2. 调整ViewModel,保留图片字节数组

修改CapturePhoto方法,同时获取图片字节数组和显示用的ImageSource,并将字节数组存入ViewModel属性:

[ObservableProperty] public int _imgId;
[ObservableProperty] public string _imgName;
[ObservableProperty] public byte[] _imgData; // 改为byte[]类型匹配API需求
[ObservableProperty] public string _loc;
[ObservableProperty] public string _owner;
[ObservableProperty] public string _imgType;

[ObservableProperty] public CameraView _cameraView;
[ObservableProperty] ImageSource _imgSrc;

IPhotoDataService photoDataService;
public HomeViewModel(IPhotoDataService photoDataService)
{
    this.photoDataService = photoDataService;
}

// ... 其他原有方法保持不变 ...

[RelayCommand]
async Task CapturePhoto()
{
    // 获取PNG格式的图片字节数组(核心:保留可存储的原始数据)
    var imageBytes = CameraView.GetSnapShot(Camera.MAUI.ImageFormat.PNG);
    // 将字节数组转为ImageSource用于界面显示
    ImgSrc = ImageSource.FromStream(() => new MemoryStream(imageBytes));
    // 存储字节数组到ViewModel属性,供后续保存使用
    ImgData = imageBytes;
    // 明确图片类型
    ImgType = "image/png";
}

[RelayCommand]
async Task SavePhoto()
{
    try
    {
        // 空值检查:避免未拍摄照片就执行保存
        if (ImgData == null)
        {
            await Shell.Current.DisplayAlert("提示", "请先拍摄照片", "确定");
            return;
        }

        var addPhoto = new PhotoModel
        {
            ImageId = ImgId,
            ImageName = ImgName ?? "未命名照片", // 给默认值避免空引用
            ImageData = ImgData, // 直接传递字节数组到API模型
            Location = Loc,
            User = Owner,
            ImageType = ImgType,
        };

        Debug.WriteLine("---&gt; 添加新照片");
        await photoDataService.AddPhotoAsync(addPhoto);
        await Shell.Current.DisplayAlert("信息", "照片保存成功", "确定");
    }
    catch (Exception ex)
    {
        await Shell.Current.DisplayAlert("错误", ex.Message, "确定");
    }
}

[RelayCommand]
async Task ViewCamera()
{
    if (CameraView.Cameras.Any())
    {
        CameraView.Camera = CameraView.Cameras.First();
        await CameraView.StopCameraAsync();
        await CameraView.StartCameraAsync();
    }
}

3. 适配API要求(可选)

如果你的API要求接收Base64字符串而非原始字节数组,只需在CapturePhoto中转换:

// 替换CapturePhoto中的ImgData赋值
ImgData = Convert.ToBase64String(imageBytes);
// 同时将ViewModel的ImgData属性改回string类型
[ObservableProperty] public string _imgData;

核心逻辑说明

  • CameraView的GetSnapShot方法直接返回图片的字节数组,这是存储到SQL Server的核心数据
  • 先将字节数组转为ImageSource用于界面显示,再将原始字节数组存入ViewModel属性
  • 保存时直接将字节数组(或Base64字符串)传递给API模型,完成数据库存储

内容的提问来源于stack exchange,提问作者Nikka Aquino

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 17:24:51