0

无法将转换为字节的图像传递给请求。因此,我无法使用 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);
4

1 回答 1

0

即使该属性表示字节,它也需要原始内存流。照片是图像的文件位置。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);

AnalyzeDocumentRequest() 也有效

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

var response =  client.AnalyzeDocument(DocRequest);
于 2019-05-21T15:53:05.040 回答