0

I am taking pictures with my camera and when OnPictureTaken is called i get a byte[ ] data. I am trying to convert the byte array to a System.IO.Stream. This helps me to analise the image in the 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.

Robin-Manuel Thiel
  • 2,206
  • 19
  • 26
  • 1
    You might *write* the byte array to the stream, but not convert it to a stream. Post just one of those "countless ways" you've tried. And also read [Ask] and take the [Tour] – Ňɏssa Pøngjǣrdenlarp Jun 22 '16 at 18:28
  • @Plutonix System.IO.Stream stream = new MemoryStream(); stream.Write(data, 0, data.Length); – Teodor Ciripescu Jun 22 '16 at 18:32
  • 1
    Possible duplicate of [How do I convert byte\[\] to stream in C#?](http://stackoverflow.com/questions/4736155/how-do-i-convert-byte-to-stream-in-c) – Jason Jun 22 '16 at 18:36

2 Answers2

2

You can use the MemoryStream class, which creates a stream backed by a byte array: MSDN link

tsandy
  • 911
  • 4
  • 10
0

When using the Client SDK, wich you can get via NuGet, you need to provide a Stream to methods like VisionServiceClient.AnalyzeImageAsync().

You can create a Stream out of an byte[] and provide it to the SDK like this:

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. Especially when taking pictures with the camera. You can also convert them to streams with imageBitmap.Compress(Bitmap.CompressFormat.Jpeg, 0, stream); but don't forget to "rewind" the stream with stream.Seek(0, SeekOrigin.Begin);, otherwise the SDK will throw an exception.

Robin-Manuel Thiel
  • 2,206
  • 19
  • 26