在识别和生成二维码这个ZXing的插件很好用
首先是识别图片中的二维码,也可以打开摄像头来扫描二维码,我想这里就写识别图片中的二维码
//把加载到的图片传进来
public string GetFishID(Texture2D tex)
{
string sr = Decode(tex.GetPixels32(), tex.width, tex.height);
return sr;
}
private BarcodeReader mReader= new BarcodeReader();
//识别二维码中的信息
public string Decode(Color32[] colors, int width, int height)
{
var result = mReader.Decode(colors, width, height);
if (result != null)
{
return result.Text;
}
else
{
Decode(tex.GetPixels32(), tex.width, tex.height);
}
return null;
}
下面写一下如何生成二维码
public void ShowQRCode(string s_str, int s_width, int s_height)
{
//定义Texture2D并且填充
Texture2D tTexture = new Texture2D(s_width, s_height);
//绘制相对应的贴图纹理
tTexture.SetPixels32(GeneQRCode(s_str, s_width, s_height));
tTexture.Apply();
//赋值贴图
m_QRCode.texture = tTexture;
}
/// <summary>
/// 返回对应颜色数组
/// </summary>
/// <param name="s_formatStr">扫码信息</param>
/// <param name="s_width">码宽</param>
/// <param name="s_height">码高</param>
Color32[] GeneQRCode(string s_formatStr, int s_width, int s_height)
{
//设置中文编码格式,否则中文不支持
QrCodeEncodingOptions tOptions = new QrCodeEncodingOptions();
tOptions.CharacterSet = "UTF-8";
//设置宽高
tOptions.Width = s_width;
tOptions.Height = s_height;
//设置二维码距离边缘的空白距离
tOptions.Margin = 3;
//重置申请写二维码变量类 (参数为:码格式(二维码、条形码...) 编码格式(支持的编码格式) )
m_barcodeWriter = new BarcodeWriter { Format = BarcodeFormat.QR_CODE, Options = tOptions };
//将咱们需要隐藏在码后面的信息赋值上
return m_barcodeWriter.Write(s_formatStr);
}
Zxing 插件的链接 https://download.csdn.net/download/baozaodedianxiaoer/12689401