This project has moved and is read-only. For the latest updates, please go here.

QRCode detection on Kinect

May 20, 2013 at 2:31 AM
Hello,
I've searched some examples here of usage of the Zxing.net on Kinect to detect QRCodes, but I still have trouble to implement this on my project.
private WriteableBitmap colorBitmap;
private Reader decoder = new QRCodeReader();

private void SensorColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
        {
            using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
            {
                if (colorFrame != null)
                {
                    // Copy the pixel data from the image to a temporary array
                    colorFrame.CopyPixelDataTo(this.colorPixels);

                    // Write the pixel data into our bitmap
                    this.colorBitmap.WritePixels(
                        new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight),
                        this.colorPixels,
                        this.colorBitmap.PixelWidth * sizeof(int),
                        0);

                    Bitmap image = BitmapFromSource((BitmapSource)Image.Source);
                    BitmapLuminanceSource sr = new BitmapLuminanceSource(image);
                    HybridBinarizer hybrid = new HybridBinarizer(sr);
                    BinaryBitmap bb = new BinaryBitmap(hybrid);
                    
                    decode.Text = CovertBitmapToQRcode(bb);

                }
            }
        }

        private Bitmap BitmapFromSource(BitmapSource bitmapsource)
        {
            Bitmap bitmap;
            
            using (MemoryStream outStream = new MemoryStream())
            {
                BitmapEncoder enc = new BmpBitmapEncoder();
                enc.Frames.Add(BitmapFrame.Create(bitmapsource));
                enc.Save(outStream);
                bitmap = new Bitmap(outStream);
            }

            return bitmap;
        }

        private string CovertBitmapToQRcode(BinaryBitmap barcodeBitmap)
        {
            if (decoder.decode(barcodeBitmap) != null)
            {
                return "Resultado: " + decoder.decode(barcodeBitmap);
            }

            return "À procura de QRCode...";
        }
Like this, my application runs the video stream really slowly. I know the stream is running 30fps but or that or I use an higger resolution with 12fps and still not work. Isnt there any othr way to to this instead of turning the Bitmap into BitmapLuminanceSource, then into HybridBinarizer, and only after into BinaryBitmap?

Thank you for your time
May 20, 2013 at 8:13 PM
There is another way. I described it in the following issue:
https://zxingnet.codeplex.com/workitem/12902
I added some classes and a project file which you can use for kinect.
With that you can write the code like the following:
ZXing.Kinect.BarcodeReader reader = new ZXing.Kinect.BarcodeReader();

void SensorColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
        {
            using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
            {
                if (colorFrame != null)
                {
                    //Decode the colorFrame
                    var result = reader.Decode(colorFrame);

                    //Show the result
                    if (result != null)
                    {
                        decode.Text = result.Text;
                    }
                }
            }
        }
Hopefully the next release of ZXing.Net will support kinect out of the box.
May 21, 2013 at 1:14 AM
Edited May 21, 2013 at 6:56 PM
Hello,
Sorry but I am pretty new on VS2012 and C#, I don't understand how and what to apply to my project so the line "var result = reader.Decode(colorFrame);" doesnt give any error.
I saw this - https://zxingnet.codeplex.com/SourceControl/changeset/86129#file_diff_1700482 - and downloaded, but I can't see any .dll files to use as references to my project, instead I see a lot of folders and I don't know what to do with those.
Can you help me out please?

Thanks in advance!

EDIT:

Ok I managed to copy both classes from the folder "kinect" to my project, imported them to my project and I can now use the Zxing.Kinec and I don't have any build error now :)
I don't know why, but using that code you gave me and deleting the other 2 methods I had below, the Kinect color stream shows black when I run it so I can't see anything :\
Maybe because the .Decause method on the reader object only works when I add the dlls zxing and zxing.presentation to the references, and those dlls I get from the zxing.net 0.11.0.0 ?
May 21, 2013 at 9:28 PM
Perhaps the following code snippet will fix your issue.
I can't test it because I don't have a kinect.
private WriteableBitmap colorBitmap;
private ZXing.Kinect.BarcodeReader reader = new ZXing.Kinect.BarcodeReader { PossibleFormats = new List<BarcodeFormat> { BarcodeFormat.QR_CODE } };

void SensorColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
        {
            using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
            {
                if (colorFrame != null)
                {
                    // Copy the pixel data from the image to a temporary array
                    colorFrame.CopyPixelDataTo(this.colorPixels);

                    // Write the pixel data into our bitmap
                    this.colorBitmap.WritePixels(
                        new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight),
                        this.colorPixels,
                        this.colorBitmap.PixelWidth * sizeof(int),
                        0);

                    //Decode the colorFrame
                    var result = reader.Decode(colorFrame);

                    //Show the result
                    if (result != null)
                    {
                        decode.Text = result.Text;
                    }
                }
            }
        }
May 21, 2013 at 9:29 PM
Worked!
Thank you very much :)
May 21, 2013 at 9:40 PM
Worked? What in detail? The black screen is gone, I think!?
Does the QR code scanning work, too?
Is it faster than before? (referencing to your initial post)
May 21, 2013 at 9:42 PM
Well it doesnt recognize the code that fine, it takes a bit, but its smoother than before!
Btw, isn't there a method to cancel the reader? Like, when it finally finds the result, can I stop the reader so it doesn't overload the sensor?
Nov 25, 2015 at 12:02 AM
Hi guys,

It seems that zxing.kinect only works for the earlier version of kinect but not kinect v2. Is there a way to implement zxing on the kinect v2?

Thanks!