关于我们

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻公共列表

.net_C#图片二维码识别

发布时间:2019-11-29 16:33:55

如何对摄像头得到的二维码图片和本地上的图片二维码进行识别:

程序使用类库zxing.dll(用来识别二维码),根据图片识别二维码方法是通用的

(1)识别二维码并得到二维码信息的方法。(传入参数为BitMap对象)代码如下:

        public string RecognizePic(Bitmap image)

        {

            string str = null;

            try

            {

                if (image != null)

                {

                    //Bitmap img = new Bitmap(@"D:\069936cb-b9a7-4fed-a7de-b9cd99f487ad.png");

                    byte[] bt = getImgByte(image);

                    LuminanceSource source = new RGBLuminanceSource(bt, image.Width, image.Height);

                    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

                    Result result = new MultiFormatReader().decode(bitmap);

                    if (result != null)

                    {

                        str = result.Text;

                    }

                }

            }

            catch (Exception re)

            {

                throw re;

            }

            return str;

        }

(2)图片转换为byte[]。(Image为Bitmap的基类型),代码如下:

        public byte[] getImgByte(Image image)

        {

            MemoryStream ms = new MemoryStream();

            try

            {

                image.Save(ms, ImageFormat.Bmp);

                byte[] bt = ms.GetBuffer();

                return bt;

            }

            catch (Exception ex)

            {

                throw ex;

            }

            finally

            {

                ms.Close();

            }

        }

(3)识别本机图片二维码,代码如下:

        public string RecognizePic(string path)

        {

            Bitmap bitmap = new Bitmap(@path);

            return RecognizePic(bitmap);

        }



/template/Home/Zkeys/PC/Static