unity实现照相功能并保存图片到本地

unity实现照相功能并保存图片到本地

功能描述

在Unity中实现照相机功能。通过调整视角拍照,获取指定相机看到的画面。显示出来并且可以选择保存或者删除。

项目结构

UI组件

请添加图片描述请添加图片描述

  • PhotoSlider: 左侧滑动条,控制相机视角远近。
  • TakePhotoBtn:点击照相按钮。
  • ShowImage: 显示照相结果的UI界面,包括删除和保存图片逻辑。

脚本文件

  • TakePhoto: 控制所有功能的脚本,挂载在场景TakePhoto物体上
    请添加图片描述
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
/**********************************
 *作者:一条闲の鱼
 *时间:2023-03-30 15:25:25
 *邮箱:1536604034@qq.com
 *版本:1.0
 *Unity版本:2019.4.30f1c3
 **********************************/
/// <summary>
/// 描述:拍照功能,拍照显示拍照图片选择删除或者保存
/// </summary>
public class TakePhoto : MonoBehaviour
{
    private string folderPath = Application.streamingAssetsPath + "/Photos";
    [Header("相机")]
    public Camera _camera;
    [Header("相机缩放")]
    public Slider photoSlider;
    [Header("拍摄按钮")]
    public Button takePhotoBtn;
    [Header("显示图片界面")]
    public Transform showImagePanel;
    [Header("图片")]
    public Image photoImage;
    [Header("删除按钮")]
    public Button deletBtn;
    [Header("保存按钮")]
    public Button saveBtn;
    byte[] imageByte;//图片字节
    private void Start()
    {
        takePhotoBtn.onClick.AddListener(ScreenShotClick);
        deletBtn.onClick.AddListener(() => { 
showImagePanel.gameObject.SetActive(false); });
        saveBtn.onClick.AddListener(SaveImage);
        photoSlider.onValueChanged.AddListener(CameraViewChange);
    }
    /// <summary>
    /// 拍照点击事件
    /// </summary>
    private void ScreenShotClick()
    {
       StartCoroutine(ScreenCapture(new Rect(0, 0, Screen.width, Screen.height)));
    }
    /// <summary>
    /// 截屏
    /// </summary>
    /// <param name="rect"></param>
    /// <returns></returns>
    IEnumerator ScreenCapture(Rect rect)
    {
        yield return null;
        #region 不带UI截屏
        // 创建一个RenderTexture对象          
        RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
        Debug.Log(rt.depth);
        // 临时设置相关相机的targetTexture为rt, 并手动渲染相关相机  
        _camera.targetTexture = rt;
        //camera.RenderDontRestore(); //编辑器模式下可用,打包后运行报错
        _camera.Render();
        // 激活这个rt, 并从中中读取像素。  
        RenderTexture.active = rt;// 注:这个时候,它是从RenderTexture.active中读取像素
        #endregion
        //先创建一个空纹理,大小可根据实现需求来设置
        Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, 
TextureFormat.RGB24, false);
        //yield return new WaitForEndOfFrame(); //不能停止一帧运行,否则会截取UI内容
        //读取屏幕像素信息并储存为纹理数据
        screenShot.ReadPixels(rect, 0, 0);
        screenShot.Apply();
        // 重置相关参数,以使用camera继续在屏幕上显示  
        _camera.targetTexture = null;
        RenderTexture.active = null;
        GameObject.Destroy(rt);
        //最后将这些纹理数据,成一个png图片文件
        imageByte = screenShot.EncodeToJPG();
        showImagePanel.gameObject.SetActive(true);
        photoImage.sprite = Sprite.Create(screenShot, new Rect(0, 0, 
screenShot.width, screenShot.height), new Vector2(0.5f, 0.5f));
    }
    private void SaveImage()
    {
        showImagePanel.gameObject.SetActive(false);
        if (!Directory.Exists(folderPath))
        {
            Directory.CreateDirectory(folderPath);
        }
        //保存图片到本地
        string filename = folderPath + "/01.jpg";
        File.WriteAllBytes(filename, imageByte);
        Debug.Log("保存成功:" + filename);
        imageByte = null;
    }
    private void CameraViewChange(float value)
    {
        _camera.fieldOfView = 60 * value;
    }
}

实现效果

请添加图片描述
请添加图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一条闲の鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值