如何使用 .Net 将以字节为单位转换的图像传递给 detectdocumenttextresult 和 detectdocumenttextresponse 中的请求?

How to pass the image converted in bytes to request in detectdocumenttextresult and detectdocumenttextresponse using .Net?

无法将转换为字节的图像传递给请求。因此我无法使用 detectdocumenttextresult 和 detectdocumenttextresponse

这是我尝试用 c#

转换的 java 代码
string document = "input.png";

ByteBuffer imageBytes;
using (Stream inputStream = new FileStream(document, FileMode.Open, FileAccess.Read)) {
    imageBytes = ByteBuffer.wrap(IOUtils.toByteArray(inputStream));
}
AmazonTextract client = AmazonTextractClientBuilder.defaultClient();

DetectDocumentTextRequest request = (new DetectDocumentTextRequest()).withDocument(new Document()
                    .withBytes(imageBytes));

DetectDocumentTextResult result = client.detectDocumentText(request);

/* 这是我无法将数据传递给请求的 c# 代码*/

AmazonTextractClient Atc = new AmazonTextractClient(credentials, config);
Image img = Image.FromFile("D:\Images\1.Jpeg");
byte[] ImageBytes = (byte[])(new ImageConverter()).ConvertTo(img, typeof(byte[]));
DetectDocumentTextRequest request = new DetectDocumentTextRequest();
request.Document.Bytes.Read(ImageBytes, 0 , ImageBytes.Length);
DetectDocumentTextResponse res = Atc.DetectDocumentText(request);

即使 属性 表示字节,它也需要原始内存流。照片是图像的文件位置。 Client 是您的 AmazonTextractClient 客户端,但是您想要实例化它。

var client = new AmazonTextractClient("[KEY ID]", "[ACCESS KEY]", Amazon.RegionEndpoint.USEast1); 

Document MyDocument;
using (Image image = Image.FromFile(photo))
{
    using (MemoryStream m = new MemoryStream())
    {
        image.Save(m, image.RawFormat);
        MyDocument = new Document()
        {
            Bytes = m
        };
    }
}

然后为 DetectDocumentTextRequest()

var request = new DetectDocumentTextRequest()
{
    Document = MyDocument
};

var response = client.DetectDocumentText(request);

A​​nalyzeDocumentRequest() 也有效

var DocRequest = new AnalyzeDocumentRequest()
{
    Document = MyDocument,
    FeatureTypes = new List<string> { FeatureType.FORMS, FeatureType.TABLES }
};

var response =  client.AnalyzeDocument(DocRequest);