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

使用Spire.Barcode在C# MAUI Android(.NET8)解码Code128条码遇未知错误

C# MAUI Android中Spire.Barcode解码Code128条码触发未指定错误

问题详情

在.NET 8.0的C# MAUI Android项目中,使用Spire.Barcode库解码拍摄的Code128条码照片时触发未指定错误,错误发生在调用Spire.Barcode.BarcodeScanner.ScanOne(tempFilePath)这一行。流程为用户拍摄照片后保存至应用私有存储,随后执行解码操作。

现有代码

private async void OnDecodeBarcodeClicked(object sender, EventArgs e)
{
     try
     {
         var photo = await MediaPicker.Default.CapturePhotoAsync();
         if (photo == null)
         {
            await DisplayAlert("Error", "No photo captured.", "OK");
            return;
         }

         var tempFilePath = Path.Combine(FileSystem.AppDataDirectory, "photo.jpg");
         using Stream sourceStream = await photo.OpenReadAsync();
         using FileStream fileStream = File.OpenWrite(tempFilePath);
         await sourceStream.CopyToAsync(fileStream);
            
         if (File.Exists(tempFilePath))
             await DisplayAlert("file exist", $"File exist", "OK");
            
         if (photo != null)
         {
             string data = Spire.Barcode.BarcodeScanner.ScanOne(tempFilePath);
             if (data.Length > 0)
             {
                await DisplayAlert("Success", $"Decoded Barcode: {data[0]}", "OK");
             }
             else
             {
                await DisplayAlert("Error", "No barcode found in the photo.","OK");
             }
        }
        if (File.Exists(tempFilePath))
        {
            File.Delete(tempFilePath);
        }
    }
    catch (Spire.Barcode.BarCodeFormatException ex)
    {
        Console.WriteLine($"Invalid Operation: {ex.Message}");
        await DisplayAlert("Error", $"{ex.Message}", "OK");
    }
    catch (InvalidOperationException ex)
    {
         Console.WriteLine($"Invalid Operation: {ex.Message}");
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Exception: {ex.Message}");
        await DisplayAlert("Error", $"An error occurred: {ex.Message}", "OK");
    }
}

错误触发行

string data = Spire.Barcode.BarcodeScanner.ScanOne(tempFilePath);

排查与解决建议

  • 确保文件写入完成:虽然用了using包裹流,但复制后手动调用FlushAsync()能保证文件完全写入磁盘,避免解码时文件处于未就绪状态:

    using Stream sourceStream = await photo.OpenReadAsync();
    using FileStream fileStream = File.OpenWrite(tempFilePath);
    await sourceStream.CopyToAsync(fileStream);
    await fileStream.FlushAsync(); // 强制写入所有缓存数据
    
  • 指定条码格式:默认ScanOne会尝试所有条码类型,明确指定Code128可减少识别误差,提高成功率:

    string data = Spire.Barcode.BarcodeScanner.ScanOne(tempFilePath, Spire.Barcode.BarCodeType.Code128);
    
  • 更新库版本:确认使用的Spire.Barcode版本适配.NET MAUI(.NET 8),旧版本可能存在平台兼容问题,尝试升级到最新稳定版。

  • 获取完整错误信息:修改异常捕获逻辑,打印堆栈跟踪,定位具体错误根源:

    catch (Exception ex)
    {
        var errorDetails = $"Message: {ex.Message}\nStack Trace: {ex.StackTrace}";
        Console.WriteLine(errorDetails);
        await DisplayAlert("Error", $"An error occurred:\n{errorDetails}", "OK");
    }
    
  • 测试基准图片:用一张清晰的、已知有效的Code128条码本地图片替换拍摄的照片,验证解码功能是否正常,排除拍摄照片质量问题。

内容的提问来源于stack exchange,提问作者David Frucht

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 12:15:15