zoukankan      html  css  js  c++  java
  • Unity之读取本地图片

    1.下载Opencv for unity.

    2.把OpenCVForUnity下的StreamingAssets拖到Assets下。

    3.点击Tools->opencv for unity->set plugin import settings.

    4.

    using UnityEngine;
    using System.Collections;
    using OpenCVForUnity;
    using System;
    using System.Collections.Generic; 

    public class imageTest : MonoBehaviour {
        public Texture2D img; 

        // Use this for initialization
        void Start () {
            
        }
        
        // Update is called once per frame
        void Update () {
            if (Input.GetKeyDown(KeyCode.A)){
                int height = 500;
                string imgpath = "/Users/shiyuhuang/Downloads/ATOCAR/ATOCAR_UNITY_2/ATOCAR_UNITY/13.jpg";
                Mat imgMat2 = Imgcodecs.imread(imgpath);
                Mat imgMat = new Mat ();
                int new_w, new_h=500;
                double scale  = new_h/Convert.ToDouble(imgMat2.rows ());
                new_w = Convert.ToInt32 (imgMat2.cols () * scale);

                Imgproc.resize (imgMat2, imgMat,new Size( Convert.ToDouble(new_w), Convert.ToDouble(new_h)) );
                List<Mat> channels = new List<Mat>();
                OpenCVForUnity.Core.split (imgMat,channels);
                Mat a = new Mat ();
                a = channels [0];
                channels [0] = channels [2];
                channels [2] = a;
                OpenCVForUnity.Core.merge (channels,imgMat);

                Debug.Log("您按下了A键"+imgMat.size());
                SpriteRenderer spr = gameObject.GetComponent<SpriteRenderer>();

                Texture2D texture2d = new Texture2D (new_w, new_h); 
                Utils.matToTexture2D (imgMat, texture2d);
                Sprite sp = Sprite.Create(texture2d,new UnityEngine.Rect(0, 0, new_w, new_h), new Vector2(0.5f,0.5f) );//注意居中显示采用0.5f值  
                spr.sprite = sp;  

    //            gameObject.GetComponent<Renderer> ().material.mainTexture = texture2d;

    //            Mat imgMat2 = new Mat (texture2d.height, texture2d.width, CvType.CV_8UC4);

    //            Utils.texture2DToMat (texture2d, imgMat2);
    //            Imgcodecs.imwrite ("/Users/shiyuhuang/Downloads/ATOCAR/ATOCAR_UNITY_2/ATOCAR_UNITY/test2.jpg",imgMat2);
            }
        }
    }

  • 相关阅读:
    Laya中使用Protobuf
    Laya中第三方库的使用
    Laya的骨骼换装
    Laya的粒子效果
    Laya的预设Prefab (预制件)
    Egret EUI Tab + ViewStack
    Egret 划线手势动画 (切水果)
    Egret3.2.6老项目转成5.2.22微信小游戏,遇到exml加载不了问题
    AndroidUI设计 之 图片浏览器
    Android应用的自动更新模块
  • 原文地址:https://www.cnblogs.com/huangshiyu13/p/5985248.html
Copyright © 2011-2022 走看看