游戏蛮牛学习群(纯技术交流,不闲聊):539178957
游戏蛮牛 手机端
开启辅助访问
 找回密码
 注册帐号

扫一扫,访问微社区

首页   >   博客   >   ID丶sunshine

关于拍照和拍照的图片显示在Image上

个人分类: 通用 | 2018-5-18 18:02
0 个评论 | 阅读 23 | 收藏 | 举报
脚本如下:


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System;
public class GameCon : MonoBehaviour {
    public Camera  Maincamera;
    // Use this for initialization
    void Start ()
    {
      
 }
 
 // Update is called once per frame
 void Update ()
    {
  
 }
    //
    void CaptureCamera(Camera camera, Rect rect)
    {
        string nowTime = DateTime.Now.ToString();
        nowTime = nowTime.Trim();
        nowTime = nowTime.Replace("/","-");
        string filename = "time" + nowTime + ".jpg";
        RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
        camera.targetTexture = rt;
        camera.Render();
        RenderTexture.active = rt;
        Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
        screenShot.ReadPixels(rect, 0, 0);
        screenShot.Apply();
        camera.targetTexture = null;
        RenderTexture.active = null;
        GameObject.Destroy(rt);
        byte[] bytes = screenShot.EncodeToJPG();
        string mobblePath = "/sdcard/DCIM/ARphoto"; //存储路径
      
        if (!Directory.Exists(mobblePath))
        {
            Directory.CreateDirectory(mobblePath);  //创建文件夹OK,将图片保存有误
            print("文件夹不存在,创建");
        }
        // string filename = Application.dataPath + "/Screenshot.png";
        string Path_save = mobblePath + "/"+filename;  //路径保存
        File.WriteAllBytes(Path_save, bytes);
    }
    //private void CreateDirectory(string path)
    //{
    //    throw new NotImplementedException();
    //}
    //private bool Exists(string path)
    //{
    //    throw new NotImplementedException();
    //}
    public void Capture2()  //拍照
    {
        CaptureCamera(Maincamera, new Rect(Screen.width * 0f, Screen.height * 0f, Screen.width * 1f, Screen.height * 1f));
    }
    IEnumerator  LoadImage(string path)  //拍照的图片展示在Image上
    {
        yield return new WaitForSeconds(0.1f);
        FileStream fileStream = new FileStream(path,FileMode.Open,FileAccess.Read);
        fileStream.Seek(0,SeekOrigin.Begin);
        //创建文件长度缓冲区
        byte[] bytes = new byte[fileStream.Length];
        //读取文件
        fileStream.Read(bytes,0,(int)fileStream.Length);
        //释放文件读取流
        fileStream.Close();
        fileStream.Dispose();
        fileStream = null;
        //创建Textures
        int width = 1280;
        int height = 800;
        Texture2D texture = new Texture2D(width,height);
        texture.LoadImage(bytes);
        Sprite sprites = Sprite.Create(texture,new Rect(0,0,texture.width,texture.height),new Vector2(0.5f,0.5f));
        // XX.image.sprite = sprites;
    }
}
0 0

作者的其他最新博客

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册帐号

个人分类

标签

阅读排行

评论排行

推荐博客

最新博客

关闭

站长推荐 上一条 /1 下一条

返回顶部