Bakudo
Bakudo

Reputation: 1

Grab Camera data and convert it to WritableBitmap for BarCode scaning


I am using windows phone 7

I want to do following on every tick of Timer object:

  1. Grab camera data from PhotoCamera object
  2. convert its' data to WritableBitmap.

I'am stuck on system exception Error.

Here is my code

        if (camReady == true)
        {

            try
            {
                var image = new Image();
                byte[] ba = new byte[camBufferSize];
                cam.GetPreviewBufferY(ba);
                var mem = new MemoryStream(ba);
                bitmap.SetSource(mem);

                var result = reader.Decode(bitmap);

                if (result == null)
                {
                    txtDebug.Text = "Tick\t" + savedCounter + "\n" + (result == null ? "Result jest nullem" : result.Text) + "\tsize " + buffer.Length
                        + "\nPierwszy elem" + buffer[0];
                    //+ "\nByteArray Len "+byteArray.Length
                    //+ "\nFirst Elem of ByteArray "+byteArray[0];
                }
                else
                {
                    txtDebug.Text = "HURRAAAAAAAA!!!!"+
                    "\nresult.Text\t" + result.Text;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("{0} Exception caught.", ex);
                 txtDebug.Text = "{0} Exception caught.\t"+ ex;
            }
            Console.WriteLine("Buffer", buffer);
            if (savedCounter % 6 == 0) cam.Focus();
            //var result = reader.Decode(bitmap);        

        }

"bitmap" object and camBufferSize object are created in other part of code

 bitmap = new WriteableBitmap((int)cam.Resolution.Width, (int)cam.Resolution.Height);
 camBufferSize = (int)cam.Resolution.Width * (int)cam.Resolution.Height;

I have Error when performing creation of WritableBitmap

bitmap.SetSource(mem);

I have checked previous lines in debugger and none of them are null etc.

What is my purpose in creating WritableBitmap this way ?

I'm building barcode scanner and i need WritableBitmap data as input for ZXing library to decode barcode on image when using camera.

I'm new in C# stuff but thank's in advance for your time helping me resolve this matter :)

Upvotes: 0

Views: 250

Answers (1)

Michael
Michael

Reputation: 2506

If you don't need the WriteableBitmap for other purposes you should not transform the luminance data from the camera to a bitmap object. It is an unnecessary conversion. Use the luminance values directly with ZXing. It's much faster. The ZXing.Net project provides some windows phone samples which show how it works.

If you really need the WriteableBitmap object then you have to convert the Y data from the camera to an ARGB32 representation. Or you use the method GetPreviewBufferArgb and then use the resulting int array as the image source.

Upvotes: 1

Related Questions