简体   繁体   English

图像作为字节流流

[英]Image as byte array to Stream

I am taking pictures with my camera and when OnPictureTaken is called i get a byte[ ] data. 我正在用相机拍照,当调用OnPictureTaken时,我得到了byte []数据。 I am trying to convert the byte array to a System.IO.Stream. 我试图将字节数组转换为System.IO.Stream。 This helps me to analise the image in the Project Oxford API. 这有助于我分析Project Oxford API中的图像。 I've tried in countless ways to do this but it seems I can't find a solution. 我已经尝试了无数种方法来执行此操作,但似乎找不到解决方案。 I would be very grateful if you help me. 如果您能帮助我,我将不胜感激。

您可以使用MemoryStream类,该类创建由字节数组支持的流: MSDN链接

When using the Client SDK , wich you can get via NuGet , you need to provide a Stream to methods like VisionServiceClient.AnalyzeImageAsync() . 使用Client SDK时 (可以通过NuGet获得),您需要向VisionServiceClient.AnalyzeImageAsync()类的方法提供Stream

You can create a Stream out of an byte[] and provide it to the SDK like this: 您可以使用byte[]创建一个Stream然后将其提供给SDK:

using (var stream = new MemoryStream(yourByteArray))
{
    var visionServiceClient = new VisionServiceClient("YOUR_API_KEY");
    var visualFeatures = new VisualFeature[] { VisualFeature.Adult, VisualFeature.Categories, VisualFeature.Color, VisualFeature.Description, VisualFeature.Faces, VisualFeature.ImageType, VisualFeature.Tags };

    var result = await visionServiceClient.AnalyzeImageAsync(stream, visualFeatures);
}

Hint: In Android you often end up with a Android.Graphics.Bitmap object when working with images. 提示:在Android中,处理图像时通常会得到一个Android.Graphics.Bitmap对象。 Especially when taking pictures with the camera. 尤其是在用相机拍照时。 You can also convert them to streams with imageBitmap.Compress(Bitmap.CompressFormat.Jpeg, 0, stream); 您还可以使用imageBitmap.Compress(Bitmap.CompressFormat.Jpeg, 0, stream);将它们转换为流imageBitmap.Compress(Bitmap.CompressFormat.Jpeg, 0, stream); but don't forget to "rewind" the stream with stream.Seek(0, SeekOrigin.Begin); 但不要忘记使用stream.Seek(0, SeekOrigin.Begin); “倒回”该流stream.Seek(0, SeekOrigin.Begin); , otherwise the SDK will throw an exception. ,否则SDK会引发异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM