赞
踩
近来给了个超坑的项目,自己翻阅文档包括测试搞了如下的几点关于U3D获取屏幕截屏
和分割图片的功能, 效果如下
原图大小是1920*1080的我这边分别切出了1024*1024 和300*300的
话不多说代码如下
/// <summary>
/// 通过摄像头捕获窗口
/// </summary>
public IEnumerator start()
{
yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
if (Application.HasUserAuthorization(UserAuthorization.WebCam))
{
WebCamDevice[] devices = WebCamTexture.devices;
deviceName = devices[0].name;//这里是调用设备上第几个摄像头
tex = new WebCamTexture(deviceName, (int)resSize.x, (int)resSize.y, 30);
tex.Play();
}
}
//获取图片
public IEnumerator getTexture()
{
Ecode = Application.dataPath + "/Photoes/BigPhoto/" + Time.time + "Big"+".jpg";//切屏存储
QECode = Application.dataPath + "/Photoes/QECode/" + Time.time +"E" +".jpg";//1024
PMainEcode=Application.dataPath+ "/Photoes/PMainEcode/" + Time.time +"Main" +".jpg";//300
//1.截图
yield return new WaitForEndOfFrame();
Texture2D tex = new Texture2D(1080, 1920, TextureFormat.RGB24, false);
tex.ReadPixels(new UnityEngine.Rect(0, 0, 1080, 1920), 0, 0);
tex.Apply();
byte[] byt = texture.EncodeToPNG();
//存储大图
File.WriteAllBytes(Ecode, byt);
//读取本地大图切出1024尺寸
Bitmap bmp = new Bitmap(Ecode);
Bitmap bmptex = new Bitmap(bmp,1920,1080);
System.Drawing.Graphics n = System.Drawing.Graphics.FromImage(bmptex);
Rectangle MainRec = new Rectangle(0, 0, 1024, 1024);
Bitmap btps = bmptex.Clone(MainRec, bmptex.PixelFormat);
btps.Save(PMainEcode, ImageFormat.Jpeg);
//裁取300尺寸大小的图片
Bitmap bmps = new Bitmap(Ecode);
Bitmap sbpm = new Bitmap(bmps, 1920, 1080);
System.Drawing.Graphics k = System.Drawing.Graphics.FromImage(sbpm);
//新裁图
System.Drawing.Rectangle rClipRect = new System.Drawing.Rectangle(0, 0, 300, 300);
Bitmap btp = sbpm.Clone(rClipRect, sbpm.PixelFormat);
btp.Save(QECode, ImageFormat.Jpeg);
}
代码如上,总之使用bitmap还是比较方便的而且效率也是比较高的 同时也可以使用bitmap进行尺寸等比压缩
这些操作
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。