Closed dazinator closed 9 years ago
I've asked this on stackoverflow as well: http://stackoverflow.com/questions/30920675/camera-preview-and-ocr?noredirect=1#comment49878574_30920675
There is an answer there. Being honest I didn't play much with byte array format as I use XLabs MediaService to get images from camera and it returns stream or file name. Will try using camera directly today.
So apparently the byte[] data coming from the camera preview is YuvImage format. So I am now doing this to convert it to JPEG:
public byte[] TranscodeImageBytesToJpeg(byte[] imageBytes, Android.Hardware.Camera camera)
{
var parameters = camera.GetParameters();
int width = parameters.PreviewSize.Width;
int height = parameters.PreviewSize.Height;
Rect rect = new Rect(0, 0, width, height);
YuvImage yuvimage = new YuvImage(imageBytes, ImageFormatType.Nv21, width, height, null);
using (MemoryStream memStream = new MemoryStream())
{
if (yuvimage.CompressToJpeg(rect, 100, memStream))
{
return memStream.ToArray();
}
throw new InvalidOperationException("Could not compress camera to Jpeg");
}
}
Now interestingly, If i pass this new byte[] directly to SetImage() - it still hangs. However, if i write the same data to a file on disk, and then call the SetImage() with the filepath overload - it runs ok. It gives me back gibberish text but guessing that's just the quality of my image that I'm passing in (not very good). This does seem to suggest there may be something wrong with the byte array overload
Could you please give me an example of your image? I'll have a look. I'll check what's wrong with SetImage(byte[]).
Thank you!
Oh, this text is definitely not recognisable :)
haha yeah - definately not!
Did you get the image file ok?
Yes.
Cool beans :)
So my big idea is to try and capture frames from camera preview, and do OCR on the fly!
However when I pass in the byte array for the current frame:
It essentially hangs and doesn't return.
Does this Byte array need to be encoded before I pass it in? Any ideas on why it might hang?
My code is doing this:
Any help or advice is much appreciated.