我正在尝试使用Azure API Vision读取图像上的文本,并且可以正常运行。但是在添加了一些代码来检测我的图像是否为黑色(无文本)之后,我从Azure API中收到了一个错误:“ Bad Request”。当我不想使用我的小功能检测黑色时,它将再次起作用。
我想那是因为我的流已在另一个对象中转换,但我不知道是什么对象...
这是我的代码:
var streamImage = this.printScreenService.CaptureRegionToStream(screenRegion);
var isBlack = await this.IsBlack(streamImage);
if (isBlack)
{
return new OcrModel
{
IsBlack = true,
Texts = new[] { "" }
};
}
var localFileOcrResult = await this.client.RecognizePrintedTextInStreamAsync(true, streamImage, OcrLanguages.Fr);
-----
private bool IsBlack(Stream stream)
{
var image = new Bitmap(Image.FromStream(stream));
for (var h = 0; h < image.Height; h++)
{
for (var w = 0; w < image.Width; w++)
{
var color = image.GetPixel(w, h);
if ((color.R != color.G || color.G != color.B || color.R != color.B) && color.A != 0)
{
return false;
}
}
}
return true;
}
我尝试使用CopyToAsync流,但不起作用。
感谢您的帮助
调用IsBlack方法后,应将流返回到位置0:
var streamImage = this.printScreenService.CaptureRegionToStream(screenRegion);
var isBlack = await this.IsBlack(streamImage);
if (isBlack)
{
return new OcrModel
{
IsBlack = true,
Texts = new[] { "" }
};
}
streamImage.Position = 0; //this line
var localFileOcrResult = await this.client.RecognizePrintedTextInStreamAsync(true, streamImage, OcrLanguages.Fr);