1.场景
承接上篇,如何对摄像头得到的图片或者本地图片进行二维码识别
2.程序
程序使用类库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);
}
结合上篇,定时器捕捉到画面并能够识别出二维码的值是,停止定时器,图片定格,得到值。
大功告成!交差!