二维码生成、二维码识别、生成对应小鱼模型、摄像图截图保存本地、读取本地图片替换模型贴图、裁剪

Plugins文件夹里放这三个
二维码生成、二维码识别、生成对应小鱼模型、摄像图截图保存本地、读取本地图片替换模型贴图、裁剪_第1张图片

using System.IO;
/// 
/// 保存替换贴图
/// 
public class CameraTextureSave : MonoBehaviour
{
   public static void SaveOne(WebCamTexture t,int frame,int w,int h)
    {
        Texture2D t2d= new Texture2D(w, h, TextureFormat.ARGB32, true);
        Vector2 offset = new Vector2((t.width - w) / 2, (t.height - h) / 2);
        t2d.SetPixels(t.GetPixels((int)offset.x, (int)offset.y, w, h));
        t2d.Apply();
        //编码
        byte[] imageTytes=t2d.EncodeToJPG();
        //存储
        File.WriteAllBytes(@"Assets\Resources\myTexture1.jpg", imageTytes);
    }
    public static void SaveTwo(WebCamTexture t,int frame,int w=850,int h=550)
    {
        Texture2D texture=new Texture2D(w,h, TextureFormat.ARGB32,true);
        Vector2 offset=new Vector2((t.width-w)/2,(t.height-h)/2);        
        texture.SetPixels(t.GetPixels((int)offset.x,(int)offset.y,w,h));
        texture.Apply();
        byte[] imageTytes = texture.EncodeToJPG();
        File.WriteAllBytes(@"Assets\Resources\myTexture2.jpg", imageTytes);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using ZXing;
using System.IO;

/// 
/// 二维码扫描识别功能
/// 
public class TestQRCodeScanning : MonoBehaviour
{
    [Header("摄像机检测界面")]
    public RawImage cameraTexture; //摄像机映射显示区域

    private WebCamTexture webCamTexture;//摄像机映射纹理

    //public Text text;   //用来显示扫描的信息
    public int framecount = 0;
    public int framecount1 = 0;
    BarcodeReader barcodeReader;    //库文件的对象(二维码信息保存的地方)

    public int CameraRequestedWidth = 1024;
    public int CameraRequestedHeight = 1024;

    /// 
    /// 开启摄像机和准备工作
    /// 
    void  DeviceInit()
    {
        //yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
        //if (Application.HasUserAuthorization(UserAuthorization.WebCam))
        //{

            WebCamDevice[] devices = WebCamTexture.devices;//1.获取所有摄像机硬件
            string deviceName = devices[0].name;    //2.获取第一个摄像机硬件的名称
            webCamTexture = new WebCamTexture(deviceName, 1024, 1024); //3.创建实例化一个摄像机显示区域
            cameraTexture.texture = webCamTexture;  //4.显示图片信息
            //webCamTexture.width = 1024;
            //webCamTexture.height = 1024;
            CameraRequestedWidth = webCamTexture.requestedWidth;
            CameraRequestedHeight = webCamTexture.requestedHeight;
            
            webCamTexture.Play();   //5.打开摄像机运行识别

            barcodeReader = new BarcodeReader(); //6.实例化识别二维码信息
        //}


    }


    Color32[] data; //二维码图片信息以像素点颜色信息数组存放
    /// 
    /// 识别摄像机图片中的二维码信息
    /// 
    void ScanQRCode()
    {
        data = webCamTexture.GetPixels32(); //7.获取摄像机画面像素颜色的数组信息
        Result result=barcodeReader.Decode(data,webCamTexture.width,webCamTexture.height);//8.获取图片中的二维码信息
        //如果获取到二维码信息了,打印出来
        if (result!=null)
        {
            Debug.Log(result.Text);//从二维码识别出来的信息
            if (result.Text== "二维码")
            {
                CameraTextureSave.SaveOne(webCamTexture, framecount,850,550);
                FishOneCreate();
                framecount++;
            }
            else if (result.Text == "第一个")
            {
                CameraTextureSave.SaveTwo(webCamTexture, framecount1);
                FishTwoCreate();
                framecount1++;
            }
            //扫描成功后的处理:停止识别关闭相机
            //IsScanning = false;
            //webCamTexture.Stop();
        }
    }
 
    /// 
    /// Start初始化函数
    /// 
    private void Start()
    {
        //scanningButton.onClick.AddListener(ScanningButtonClick);
        DeviceInit();
        //DeviceInit();
        IsScanning = true;
    }

    bool IsScanning = false;
    float interval = 5; //扫描识别时间间隔
    [SerializeField]
    Button scanningButton;
    void ScanningButtonClick()
    {
        //DeviceInit();
        //IsScanning = true;
    }
    private void Update()
    {
        if (IsScanning)
        {
            //每隔一段时间进行一次识别二维码信息
            interval += Time.deltaTime;
            if (interval>=5)
            {
                interval = 0;
                ScanQRCode();//开始扫描
            }
        }
    }
    public void FishOneCreate()
    {
        float x = Random.Range(-3.0f, 0);
        float y = Random.Range(-3.0f, 0);
        float z = Random.Range(-3.0f, 0);
        Vector3 pos = new Vector3(x, y, z);
        GameObject obj = Resources.Load<GameObject>("yu_1");
        //读取写入本地的图片并指定
        //Texture mTexture = Resources.Load("myTexture", typeof(Texture)) as Texture;
        byte[] imgData = File.ReadAllBytes(@"Assets\Resources\myTexture1.jpg");
        Texture mTexture = ByteToTex2d(imgData);
        //mTexture.LoadImage(imgData);
        GameObject a = Instantiate(obj, pos, Quaternion.identity);
        a.transform.Find("yu_1").GetComponent<Renderer>().material.mainTexture = mTexture;
    }
    public void FishTwoCreate()
    {
        float x = Random.Range(-3.0f, 0);
        float y = Random.Range(-3.0f, 0);
        float z = Random.Range(-3.0f, 0);
        Vector3 pos = new Vector3(x, y, z);
        GameObject obj = Resources.Load<GameObject>("yu_2");
        //读取写入本地的图片并指定
        //Texture mTexture = Resources.Load("myTexture", typeof(Texture)) as Texture;
        byte[] imgData = File.ReadAllBytes(@"Assets\Resources\myTexture2.jpg");
        Texture mTexture = ByteToTex2d(imgData);
        //mTexture.LoadImage(imgData);
        GameObject a = Instantiate(obj, pos, Quaternion.identity);
        a.transform.Find("yu_2").GetComponent<Renderer>().material.mainTexture = mTexture;
    }
    public static Texture2D ByteToTex2d(byte[] bytes, int w = 1024, int h = 1024)
    {
        Texture2D tex = new Texture2D(w, h);
        tex.LoadImage(bytes);
        return tex;
    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using ZXing;
using ZXing.QrCode;

//二维码的生成

public class TestQRCodeDraw : MonoBehaviour
{
    [Header("绘制好的二维码显示界面")]
    public RawImage QRCode;
    //二维码绘制类
    BarcodeWriter barcodeWriter;
    [SerializeField]
    Button drawbutton;//先生成俩二维码测试用,截图保存下来


    /// 
    /// 将制定字符串信息转换成二维码图片信息
    /// 
    /// 要生产二维码的字符串信息
    /// 二维码的宽度
    /// 二维码的高度
    /// 返回二维码图片的颜色数组信息
    Color32[] GeneQRCode(string formatStr, int width, int height)
    {
        QrCodeEncodingOptions options = new QrCodeEncodingOptions();//绘制二维码之前进行一些设置
        options.CharacterSet = "UTF-8"; //设置字符串转换格式,确保字符串信息保持正确
        options.Width= width;   //设置绘制区域的宽度和高度的像素值
        options.Height= height;
        options.Margin = 1; //设置二维码边缘留白宽度(值越大留白宽度越大,二维码就越小)

        barcodeWriter=new BarcodeWriter { Format = BarcodeFormat.QR_CODE,Options = options};//实例化字符串绘制二维码工具
        return barcodeWriter.Write(formatStr);//进行二维码绘制并进行返回图片的颜色数组信息
    }


    /// 
    /// 根据二维码图片信息绘制指定字符串信息的二维码到指定区域
    /// 
    /// 要产生二维码字符串信息
    /// 二维码的宽度
    /// 二维码的高度
    /// 返回绘制好的图片
    Texture2D showQRCode(string str,int width,int height)
    {
        Texture2D t=new Texture2D(width,height);//实例化一个图片类
        Color32[] col32=GeneQRCode(str,width,height);//获取二维码图片颜色数组信息
        t.SetPixels32(col32);//为图片设置绘制像素颜色信息
        t.Apply();//设置信息更新应用下
        return t;//将整理好的图片信息指定到指定区域中
    }

    /// 
    /// 开始绘制指定信息的二维码
    /// 
    /// 
    void DrawQRCode(string formatStr)
    {
        Texture2D t=showQRCode(formatStr,256,256);//注意:这个宽高度大小256不要变。
        QRCode .texture = t;//显示到UI界面的图片上

    }
    public string QRCodeText = "二维码";
    void DrawButtonClick()
    {
        DrawQRCode(QRCodeText);
    }
    private void Start()
    {
        drawbutton.onClick.AddListener(DrawButtonClick);
    }

}

二维码生成、二维码识别、生成对应小鱼模型、摄像图截图保存本地、读取本地图片替换模型贴图、裁剪_第2张图片

你可能感兴趣的:(unity引擎,unity)