对着月亮发誓,不如顶着太阳做事!
认真的人改变自己,执着的人改变命运!
  1. using System.Collections;
  2.  
    using System.Collections.Generic;
  3.  
    using UnityEngine;
  4.  
    using ZXing;
  5.  
    using UnityEngine.UI;
  6.  
    /// <summary>
  7.  
    /// 二维码扫描识别功能
  8.  
    /// </summary>
  9.  
    public class TestQRCodeScanning : MonoBehaviour {
  10.  
     
  11.  
    [Header("摄像机检测界面")]
  12.  
    public RawImage cameraTexture;//摄像机映射显示区域
  13.  
     
  14.  
    private WebCamTexture webCamTexture;//摄像机映射纹理
  15.  
    public Text text;//用来显示扫描信息
  16.  
    //二维码识别类
  17.  
    BarcodeReader barcodeReader;//库文件的对象(二维码信息保存的地方)
  18.  
     
  19.  
    /// <summary>
  20.  
    /// 开启摄像机和准备工作
  21.  
    /// </summary>
  22.  
    void DeviceInit()
  23.  
    {
  24.  
     
  25.  
     
  26.  
    //1、获取所有摄像机硬件
  27.  
    WebCamDevice[] devices = WebCamTexture.devices;
  28.  
    //2、获取第一个摄像机硬件的名称
  29.  
    string deviceName = devices[0].name;//手机后置摄像机
  30.  
    //3、创建实例化一个摄像机显示区域
  31.  
    webCamTexture = new WebCamTexture(deviceName, 400, 300);
  32.  
    //4、显示的图片信息
  33.  
    cameraTexture.texture = webCamTexture;
  34.  
    //5、打开摄像机运行识别
  35.  
    webCamTexture.Play();
  36.  
     
  37.  
    //6、实例化识别二维码信息存储对象
  38.  
    barcodeReader = new BarcodeReader();
  39.  
    }
  40.  
     
  41.  
    Color32[] data;//二维码图片信息以像素点颜色信息数组存放
  42.  
     
  43.  
    /// <summary>
  44.  
    /// 识别摄像机图片中的二维码信息
  45.  
    /// 打印二维码识别到的信息
  46.  
    /// </summary>
  47.  
    void ScanQRCode()
  48.  
    {
  49.  
    //7、获取摄像机画面的像素颜色数组信息
  50.  
    data = webCamTexture.GetPixels32();
  51.  
    //8、获取图片中的二维码信息
  52.  
    Result result = barcodeReader.Decode(data,webCamTexture.width,webCamTexture.height);
  53.  
    //如果获取到二维码信息了,打印出来
  54.  
    if (result!=null)
  55.  
    {
  56.  
    Debug.Log(result.Text);//===》==》===》 这是从二维码识别出来的信息
  57.  
    text.text = result.Text;//显示扫描信息
  58.  
     
  59.  
    //扫描成功之后的处理
  60.  
    IsScanning = false;
  61.  
    webCamTexture.Stop();
  62.  
    }
  63.  
    }
  64.  
     
  65.  
     
  66.  
    /// <summary>
  67.  
    /// Start 初始化函数
  68.  
    /// </summary>
  69.  
    private void Start()
  70.  
    {
  71.  
    scanningButton.onClick.AddListener(ScanningButtonClick);
  72.  
    }
  73.  
     
  74.  
     
  75.  
    bool IsScanning = false;
  76.  
    float interval = 3;//扫描识别时间间隔
  77.  
    [SerializeField] Button scanningButton;
  78.  
    void ScanningButtonClick()
  79.  
    {
  80.  
    DeviceInit();
  81.  
    IsScanning = true;
  82.  
    }
  83.  
     
  84.  
    private void Update()
  85.  
    {
  86.  
    if (IsScanning)
  87.  
    {
  88.  
    //每隔一段时间进行一次识别二维码信息
  89.  
    interval += Time.deltaTime;
  90.  
    if (interval>=3)
  91.  
    {
  92.  
    interval = 0;
  93.  
    ScanQRCode();//开始扫描
  94.  
    }
  95.  
    }
  96.  
    }
  97.  
    }

ZXing:https://archive.codeplex.com/?p=zxingnet

下载之后把zxing.unity.dll拷贝到Unity的Plugins文件夹下,

zxing.unity.dll下载之后的位置找到UnityDemo/Assets下

  1.  
    using System.Collections;
  2.  
    using System.Collections.Generic;
  3.  
    using UnityEngine;
  4.  
    using UnityEngine.UI;
  5.  
    using ZXing;
  6.  
    //二维码的生成
  7.  
    public class TestQRCodeDraw : MonoBehaviour {
  8.  
     
  9.  
    [Header("绘制好的二维码显示界面")]
  10.  
    public RawImage QRCode;
  11.  
    //二维码绘制类
  12.  
    BarcodeWriter barcodeWriter;
  13.  
    [SerializeField] Button drawbutton;
  14.  
    /// <summary>
  15.  
    /// 将制定字符串信息转换成二维码图片信息
  16.  
    /// </summary>
  17.  
    /// <param name="formatStr">要生产二维码的字符串信息</param>
  18.  
    /// <param name="width">二维码的宽度</param>
  19.  
    /// <param name="height">二维码的高度</param>
  20.  
    /// <returns>返回二维码图片的颜色数组信息</returns>
  21.  
    Color32[] GeneQRCode(string formatStr,int width,int height)
  22.  
    {
  23.  
    //绘制二维码前进行一些设置
  24.  
    ZXing.QrCode.QrCodeEncodingOptions options =
  25.  
    new ZXing.QrCode.QrCodeEncodingOptions();
  26.  
    //设置字符串转换格式,确保字符串信息保持正确
  27.  
    options.CharacterSet = "UTF-8";
  28.  
    //设置绘制区域的宽度和高度的像素值
  29.  
    options.Width = width;
  30.  
    options.Height = height;
  31.  
    //设置二维码边缘留白宽度(值越大留白宽度大,二维码就减小)
  32.  
    options.Margin = 1;
  33.  
     
  34.  
    //实例化字符串绘制二维码工具
  35.  
    barcodeWriter = new BarcodeWriter {Format=BarcodeFormat.QR_CODE,Options=options };
  36.  
    //进行二维码绘制并进行返回图片的颜色数组信息
  37.  
    return barcodeWriter.Write(formatStr);https://www.cnblogs.com/p199543H/articles/12977982.html
    https://www.cnblogs.com/webdesigner/articles/12977983.html
    https://www.cnblogs.com/annie_229/articles/12977984.html
    https://www.cnblogs.com/zzll123/articles/12977985.html
    https://www.cnblogs.com/yilx/articles/12977986.html
    https://www.cnblogs.com/yilx/articles/12977988.html
    https://www.cnblogs.com/webdesigner/articles/12977991.html
    https://www.cnblogs.com/yilx/articles/12977992.html
    https://www.cnblogs.com/zzll123/articles/12977993.html
    https://www.cnblogs.com/annie_229/articles/12977995.html
    https://www.cnblogs.com/yilx/articles/12977996.html
    https://www.cnblogs.com/webdesigner/articles/12977997.html
    https://www.cnblogs.com/webdesigner/articles/12978002.html
    https://www.cnblogs.com/webdesigner/articles/12978006.html
    https://www.cnblogs.com/webdesigner/articles/12978010.html
    https://www.cnblogs.com/webdesigner/articles/12978016.html
    https://www.cnblogs.com/webdesigner/articles/12978018.html
    https://www.cnblogs.com/webdesigner/articles/12978022.html
    https://www.cnblogs.com/zzll123/articles/12978023.html
    https://www.cnblogs.com/annie_229/articles/12978024.html
    https://www.cnblogs.com/annie_229/articles/12978030.html
    https://www.cnblogs.com/annie_229/articles/12978035.html
    https://www.cnblogs.com/annie_229/articles/12978040.html
    https://www.cnblogs.com/annie_229/articles/12978044.html
    https://www.cnblogs.com/annie_229/articles/12978048.html
    https://www.cnblogs.com/annie_229/articles/12978053.html
    https://www.cnblogs.com/annie_229/articles/12978056.html
    www.cnblogs.com/annie_229/articles/12977868.html
    www.cnblogs.com/annie_229/articles/12977863.html
    www.cnblogs.com/annie_229/articles/12977858.html
    www.cnblogs.com/annie_229/articles/12977853.html
    www.cnblogs.com/annie_229/articles/12977846.html
    www.cnblogs.com/annie_229/articles/12977840.html
    www.cnblogs.com/annie_229/articles/12977837.html
    www.cnblogs.com/annie_229/articles/12977921.html
    www.cnblogs.com/annie_229/articles/12977915.html
    www.cnblogs.com/annie_229/articles/12977910.html
    www.cnblogs.com/annie_229/articles/12977905.html
    www.cnblogs.com/annie_229/articles/12977899.html
    www.cnblogs.com/annie_229/articles/12977895.html
    www.cnblogs.com/annie_229/articles/12977888.html
    www.cnblogs.com/annie_229/articles/12977883.html
    www.cnblogs.com/annie_229/articles/12977878.html
    www.cnblogs.com/annie_229/articles/12977874.html
    www.cnblogs.com/zzll123/articles/12977929.html
    www.cnblogs.com/zzll123/articles/12977923.html
    www.cnblogs.com/zzll123/articles/12977916.html
    www.cnblogs.com/zzll123/articles/12977911.html
    www.cnblogs.com/zzll123/articles/12977908.html
    www.cnblogs.com/zzll123/articles/12977902.html
    www.cnblogs.com/zzll123/articles/12977893.html
    www.cnblogs.com/zzll123/articles/12977886.html
    www.cnblogs.com/zzll123/articles/12977881.html
    www.cnblogs.com/zzll123/articles/12977876.html
    www.cnblogs.com/zzll123/articles/12977873.html
    www.cnblogs.com/zzll123/articles/12977869.html
    www.cnblogs.com/zzll123/articles/12977864.html
    www.cnblogs.com/zzll123/articles/12977859.html
    www.cnblogs.com/zzll123/articles/12977854.html
    www.cnblogs.com/zzll123/articles/12977848.html
    www.cnblogs.com/zzll123/articles/12977844.html
    www.cnblogs.com/yilx/articles/12977871.html
    www.cnblogs.com/yilx/articles/12977867.html
    www.cnblogs.com/yilx/articles/12977861.html
    www.cnblogs.com/yilx/articles/12977856.html
    www.cnblogs.com/yilx/articles/12977851.html
    www.cnblogs.com/yilx/articles/12977845.html
    www.cnblogs.com/yilx/articles/12977841.html
    www.cnblogs.com/yilx/articles/12977931.html
    www.cnblogs.com/yilx/articles/12977924.html
    www.cnblogs.com/yilx/articles/12977918.html
    www.cnblogs.com/yilx/articles/12977912.html
    www.cnblogs.com/yilx/articles/12977906.html
    www.cnblogs.com/yilx/articles/12977901.html
    www.cnblogs.com/yilx/articles/12977896.html
    www.cnblogs.com/yilx/articles/12977887.html
    www.cnblogs.com/yilx/articles/12977882.html
    www.cnblogs.com/yilx/articles/12977877.html
    www.cnblogs.com/p199543H/articles/12977935.html
    www.cnblogs.com/p199543H/articles/12977930.html
    www.cnblogs.com/p199543H/articles/12977925.html
    www.cnblogs.com/p199543H/articles/12977917.html
    www.cnblogs.com/p199543H/articles/12977913.html
    www.cnblogs.com/p199543H/articles/12977909.html
    www.cnblogs.com/p199543H/articles/12977898.html
    www.cnblogs.com/p199543H/articles/12977894.html
    www.cnblogs.com/p199543H/articles/12977891.html
    www.cnblogs.com/p199543H/articles/12977884.html
    www.cnblogs.com/p199543H/articles/12977880.html
    www.cnblogs.com/p199543H/articles/12977875.html
    www.cnblogs.com/p199543H/articles/12977866.html
    www.cnblogs.com/p199543H/articles/12977862.html
    www.cnblogs.com/p199543H/articles/12977857.html
    www.cnblogs.com/p199543H/articles/12977852.html
    www.cnblogs.com/p199543H/articles/12977847.html
    www.cnblogs.com/p199543H/articles/12977842.html
    www.cnblogs.com/p199543H/articles/12977838.html
    www.cnblogs.com/p199543H/articles/12977835.html
    www.cnblogs.com/p199543H/articles/12977834.html
    www.cnblogs.com/p199543H/articles/12977833.html
    www.cnblogs.com/webdesigner/articles/12977932.html
    www.cnblogs.com/webdesigner/articles/12977927.html
    www.cnblogs.com/webdesigner/articles/12977920.html
    www.cnblogs.com/webdesigner/articles/12977914.html
    www.cnblogs.com/webdesigner/articles/12977907.html
    www.cnblogs.com/webdesigner/articles/12977900.html
    www.cnblogs.com/webdesigner/articles/12977892.html
    www.cnblogs.com/webdesigner/articles/12977885.html
    www.cnblogs.com/webdesigner/articles/12977879.html
    www.cnblogs.com/webdesigner/articles/12977872.html
    www.cnblogs.com/webdesigner/articles/12977860.html
    www.cnblogs.com/webdesigner/articles/12977855.html
    www.cnblogs.com/webdesigner/articles/12977850.html
    www.cnblogs.com/webdesigner/articles/12977843.html
    www.cnblogs.com/webdesigner/articles/12977839.html
    www.cnblogs.com/webdesigner/articles/12977836.html
    https://www.cnblogs.com/b197594K/articles/12977957.html
    https://www.cnblogs.com/b197594K/articles/12977958.html
    https://www.cnblogs.com/b197594K/articles/12977959.html
    https://www.cnblogs.com/b197594K/articles/12977960.html
    https://www.cnblogs.com/b197594K/articles/12977961.html
    https://www.cnblogs.com/b197594K/articles/12977962.html
    https://www.cnblogs.com/b197594K/articles/12977963.html
    https://www.cnblogs.com/b197594K/articles/12977964.html
    https://www.cnblogs.com/b197594K/articles/12977966.html
    https://www.cnblogs.com/b197594K/articles/12977968.html
    https://www.cnblogs.com/b197594K/articles/12977974.html
    https://www.cnblogs.com/b197594K/articles/12977976.html
    https://www.cnblogs.com/b197594K/articles/12977977.html
    https://www.cnblogs.com/b197594K/articles/12977999.html
    https://www.cnblogs.com/b197594K/articles/12978004.html
    https://www.cnblogs.com/b197594K/articles/12978009.html
    https://www.cnblogs.com/b197594K/articles/12978014.html
    https://www.cnblogs.com/b197594K/articles/12978020.html
    https://www.cnblogs.com/b197594K/articles/12978027.html
    https://www.cnblogs.com/b197594K/articles/12978031.html
    https://www.cnblogs.com/b197594K/articles/12978037.html
    https://www.cnblogs.com/b197594K/articles/12978042.html
    https://www.cnblogs.com/b197594K/articles/12978046.html
    https://www.cnblogs.com/b197594K/articles/12978050.html
    https://www.cnblogs.com/b197594K/articles/12978054.html
    https://www.cnblogs.com/b197594K/articles/12978057.html
    https://www.cnblogs.com/b197594K/articles/12978059.html
    https://www.cnblogs.com/b197594K/articles/12978060.html
    https://www.cnblogs.com/b197594K/articles/12978061.html
    https://www.cnblogs.com/b197594K/articles/12978063.html
    https://www.cnblogs.com/b197594K/articles/12978064.html
  38.  
posted on 2020-05-28 07:05  抽象的继承  阅读(242)  评论(0编辑  收藏  举报