void SaveJPG(string filename, int width, int height, Color[] pixels) { FileStream fs = new FileStream(filename, FileMode.Create); if (fs != null) { //Debug.Log("File created \"" + filename + "\""); BinaryWriter bw = new BinaryWriter(fs); if (bw != null) { //Debug.Log("Creating JPG " + width + " x " + height); Quality = Mathf.Clamp(Quality, 0.0f, 100.0f); JPGEncoder NewEncoder = new JPGEncoder(pixels, width, height, Quality); NewEncoder.doEncoding(); //Debug.Log("jpg encoded size=" + NewEncoder.GetBytes().Length + " bytes"); byte[] TexData = NewEncoder.GetBytes(); bw.Write(TexData); bw.Close(); } fs.Close(); //Debug.Log("JPG saved"); } }
public void SendTexture() { if (sendTexture == null) { sendTexture = new Texture2D(sendRenderTexture.width, sendRenderTexture.height, TextureFormat.RGB24, false); } RenderTexture.active = sendRenderTexture; sendTexture.ReadPixels(new Rect(0, 0, sendRenderTexture.width, sendRenderTexture.height), 0, 0, false); //sendTexture.Apply(false); byte[] packet; if (useJpgEncodeSend) { JPGEncoder encoder = new JPGEncoder(sendTexture, jpgQuality); encoder.doEncoding(); packet = encoder.GetBytes(); } else { packet = sendTexture.EncodeToPNG(); } if (packet.Length > 0) { udp.SendPacket(packet); } }
private IEnumerator ScreenshotEncode() { // yield return new WaitForEndOfFrame(); // // // create a texture to pass to encoding // texture = new Texture2D (Screen.width, Screen.height, TextureFormat.RGB24, false); // // // put buffer into texture // texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0); // texture.Apply(false, false); string fullPath = Application.dataPath + "/../testscreen-" + count + ".jpg"; JPGEncoder encoder = new JPGEncoder(texture, 75, fullPath ); //How to encode without save to disk // JPGEncoder encoder = new JPGEncoder(texture, 75); //encoder is threaded; wait for it to finish while(!encoder.isDone) yield return null; Debug.Log("Screendump saved at : " + fullPath); Debug.Log("Done encoding and bytes ready for use. e.g. send over network, write to disk"); Debug.Log("Size: " + encoder.GetBytes().Length + " bytes"); count++; }
private IEnumerator ScreenshotEncode() { // yield return new WaitForEndOfFrame(); // // // create a texture to pass to encoding // texture = new Texture2D (Screen.width, Screen.height, TextureFormat.RGB24, false); // // // put buffer into texture // texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0); // texture.Apply(false, false); string fullPath = Application.dataPath + "/../testscreen-" + count + ".jpg"; JPGEncoder encoder = new JPGEncoder(texture, 75, fullPath); //How to encode without save to disk // JPGEncoder encoder = new JPGEncoder(texture, 75); //encoder is threaded; wait for it to finish while (!encoder.isDone) { yield return(null); } Debug.Log("Screendump saved at : " + fullPath); Debug.Log("Done encoding and bytes ready for use. e.g. send over network, write to disk"); Debug.Log("Size: " + encoder.GetBytes().Length + " bytes"); count++; }
void SaveJPG(string filename, int width, int height, Color[] pixels) { using (FileStream fs = new FileStream(filename, FileMode.Create)) { try { BinaryWriter bw = new BinaryWriter(fs); if (bw != null) { Quality = Mathf.Clamp(Quality, 0.0f, 100.0f); JPGEncoder NewEncoder = new JPGEncoder(pixels, width, height, Quality); NewEncoder.doEncoding(); byte[] TexData = NewEncoder.GetBytes(); bw.Write(TexData); bw.Close(); } } catch (System.Exception ex) { Debug.Log("Error Writing File! " + ex); } finally { fs.Close(); } } }
IEnumerator CaptureScreenshotJpg(Rect rect, Action <string> onFinish) { yield return(new WaitForEndOfFrame()); // 先创建一个的空纹理,大小可根据实现需要来设置 var screenShot = new Texture2D((int)(rect.width), (int)(rect.height), TextureFormat.RGB24, false); // 读取屏幕像素信息并存储为纹理数据, screenShot.ReadPixels(rect, 0, 0); screenShot.Apply(); var encoder = new JPGEncoder(screenShot, 20); //质量1~100 encoder.doEncoding(); while (!encoder.isDone) { yield return(null); } File.WriteAllBytes(_sShotImgpath, encoder.GetBytes()); while (!File.Exists(_sShotImgpath)) { Debug.LogError("等待截图完成"); yield return(new WaitForEndOfFrame()); } onFinish(_sShotImgpath); }
IEnumerator CreateScreenshotCR(string path) { yield return(new WaitForEndOfFrame()); Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false); #if UNITY_EDITOR var offset = GetWindowOffset() - GetMainGameView().position.position; offset.x += 2; offset.y -= 16; texture.ReadPixels(new Rect(offset.x, offset.y, Screen.width, Screen.height), 0, 0); #else texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0); #endif texture.Apply(); yield return(null); JPGEncoder encoder = new JPGEncoder(texture, 95.0f); while (!encoder.isDone) { yield return(null); } System.IO.File.WriteAllBytes(path, encoder.GetBytes()); #endif }
public void Save(string path, float jpegQuality, float lightmapMult, float lightmapPower) { // http://answers.unity3d.com/questions/1114251/lightmappingcompleted-callback-occurs-before-light.html string curScene = UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene().path; string[] parts = curScene.Split('/', '\\'); string sceneName = parts[parts.Length - 1].Split('.')[0]; string lightmapPath = Path.GetDirectoryName(curScene) + "/" + sceneName + "/"; string filepath = lightmapPath + "Lightmap-" + "0" + "_comp_light.exr"; //TODO 어디에서 파일명을 얻을 것인가? //string filepath = "Assets/Scenes/simple/Lightmap-0_comp_light.exr"; // readable 설정 켜주기 TextureImporter texImporter = (TextureImporter)AssetImporter.GetAtPath(filepath); if (!texImporter.isReadable) { texImporter.isReadable = true; texImporter.SaveAndReimport(); } Texture2D ti = LightmapSettings.lightmaps[Index].lightmapFar; Texture2D tf = new Texture2D(ti.width, ti.height, TextureFormat.ARGB32, false); Color32[] c = ti.GetPixels32(); for (int j = 0; j < c.Length; j++) { float af = c[j].a / 255f; float rf = c[j].r / 255f; float gf = c[j].g / 255f; float bf = c[j].b / 255f; float ur = Mathf.Pow(rf * af, lightmapPower) * 255f * lightmapMult; float ug = Mathf.Pow(gf * af, lightmapPower) * 255f * lightmapMult; float ub = Mathf.Pow(bf * af, lightmapPower) * 255f * lightmapMult; ur = Mathf.Clamp(ur, 0, 255); ug = Mathf.Clamp(ug, 0, 255); ub = Mathf.Clamp(ub, 0, 255); c[j].r = Convert.ToByte(ur); c[j].g = Convert.ToByte(ug); c[j].b = Convert.ToByte(ub); c[j].a = 255; } tf.SetPixels32(c); JPGEncoder je = new JPGEncoder(tf, jpegQuality, "", true); byte[] bytes = je.GetBytes(); File.WriteAllBytes(path + FileName, bytes); Texture2D.DestroyImmediate(tf); }
private byte[] getBytes(float quality) { if(Format == TextureImporterFormat.ARGB32) { return t2d.EncodeToPNG (); } else { JPGEncoder j = new JPGEncoder(t2d, quality); return j.GetBytes(); } }
void UploadJPG(string filename, int width, int height, Color[] pixels) { Quality = Mathf.Clamp(Quality, 0.0f, 100.0f); JPGEncoder NewEncoder = new JPGEncoder(pixels, width, height, Quality); NewEncoder.doEncoding(); byte[] TexData = NewEncoder.GetBytes(); UploadFile(TexData, m_URL, filename); }
private byte[] getBytes(float quality) { if (Format == TextureImporterFormat.ARGB32) { return(t2d.EncodeToPNG()); } else { JPGEncoder j = new JPGEncoder(t2d, quality); return(j.GetBytes()); } }
public void CaptureJPG(string filePath, int superSize) { int width = Screen.width; int height = Screen.height; Texture2D texture = new Texture2D(width, height, TextureFormat.ARGB32, false); texture.ReadPixels (new Rect(0, 0, width, height), 0, 0); texture.Apply(); JPGEncoder enc = new JPGEncoder(texture, 100, filePath); GameObject.Destroy(texture); Debug.Log(filePath+" : saved !"); }
void SaveJPG(string filename, int width, int height, Color[] pixels) { FileStream fs = new FileStream(filename, FileMode.Create); if (fs != null) { BinaryWriter bw = new BinaryWriter(fs); Quality = Mathf.Clamp(Quality, 0.0f, 100.0f); JPGEncoder NewEncoder = new JPGEncoder(pixels, width, height, Quality); NewEncoder.doEncoding(); byte[] TexData = NewEncoder.GetBytes(); bw.Write(TexData); } }
public void Save(string path, float jpegQuality, float lightmapMult, float lightmapPower) { Texture2D ti = LightmapSettings.lightmaps[index].lightmapFar; Texture2D tf = new Texture2D(ti.width, ti.height, TextureFormat.ARGB32, false); Color32[] c = ti.GetPixels32(); for (int j = 0; j < c.Length; j++) { float af = c[j].a / 255f; float rf = c[j].r / 255f; float gf = c[j].g / 255f; float bf = c[j].b / 255f; float ur = Mathf.Pow(rf * af, lightmapPower) * 255f * lightmapMult; float ug = Mathf.Pow(gf * af, lightmapPower) * 255f * lightmapMult; float ub = Mathf.Pow(bf * af, lightmapPower) * 255f * lightmapMult; ur = Mathf.Clamp(ur, 0, 255); ug = Mathf.Clamp(ug, 0, 255); ub = Mathf.Clamp(ub, 0, 255); c[j].r = Convert.ToByte(ur); c[j].g = Convert.ToByte(ug); c[j].b = Convert.ToByte(ub); c[j].a = 255; } tf.SetPixels32(c); JPGEncoder je = new JPGEncoder(tf, jpegQuality); byte[] bytes = je.GetBytes(); File.WriteAllBytes(path + fileName, bytes); Texture2D.DestroyImmediate(tf); }
static byte [] EncodeTexture(Texture2D texture, ImageType type) { byte [] encodedBytes = null; switch (type) { case ImageType.PNG: encodedBytes = texture.EncodeToPNG(); break; case ImageType.JPG: JPGEncoder encoder = new JPGEncoder(texture, m_JPEGQuality); encoder.doEncoding(); if (encoder.isDone) { encodedBytes = encoder.GetBytes(); } break; } return(encodedBytes); }
/// <summary> /// 範囲指定をしてPNGで保存 /// </summary> public static IEnumerator CaptureRangeJPG(string dirPath, string fileName, Rect range) { yield return(new WaitForEndOfFrame()); //範囲でキャプチャをする Texture2D screenShot = new Texture2D((int)range.width, (int)range.height); screenShot.ReadPixels(range, 0, 0); //保存先の確認 if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } //拡張子の確認 if (Path.GetExtension(fileName).ToUpper() != ".JPG") { fileName = Path.GetFileNameWithoutExtension(fileName) + ".jpg"; } //JPEGエンコード JPGEncoder encoder = new JPGEncoder(screenShot, 100.0f); encoder.doEncoding(); while (!encoder.isDone) { yield return(0); } //保存 File.WriteAllBytes(Path.Combine(dirPath, fileName), encoder.GetBytes()); encoder = null; Texture2D.Destroy(screenShot); }
public void Save (string path, float jpegQuality, float lightmapMult, float lightmapPower) { Texture2D ti = LightmapSettings.lightmaps[index].lightmapFar; Texture2D tf = new Texture2D(ti.width, ti.height, TextureFormat.ARGB32, false); Color32[] c = ti.GetPixels32(); for(int j = 0; j < c.Length; j++) { float af = c[j].a / 255f; float rf = c[j].r / 255f; float gf = c[j].g / 255f; float bf = c[j].b / 255f; float ur = Mathf.Pow(rf * af, lightmapPower) * 255f * lightmapMult; float ug = Mathf.Pow(gf * af, lightmapPower) * 255f * lightmapMult; float ub = Mathf.Pow(bf * af, lightmapPower) * 255f * lightmapMult; ur = Mathf.Clamp(ur, 0, 255); ug = Mathf.Clamp(ug, 0, 255); ub = Mathf.Clamp(ub, 0, 255); c[j].r = Convert.ToByte(ur); c[j].g = Convert.ToByte(ug); c[j].b = Convert.ToByte(ub); c[j].a = 255; } tf.SetPixels32(c); JPGEncoder je = new JPGEncoder(tf, jpegQuality); byte[] bytes = je.GetBytes(); File.WriteAllBytes (path + fileName, bytes); Texture2D.DestroyImmediate(tf); }
void SaveJPG(string filename, int width, int height, Color[] pixels) { FileStream fs = new FileStream(filename, FileMode.Create); if ( fs != null ) { //Debug.Log("File created \"" + filename + "\""); BinaryWriter bw = new BinaryWriter(fs); if ( bw != null ) { //Debug.Log("Creating JPG " + width + " x " + height); Quality = Mathf.Clamp(Quality, 0.0f, 100.0f); JPGEncoder NewEncoder = new JPGEncoder(pixels, width, height, Quality); NewEncoder.doEncoding(); //Debug.Log("jpg encoded size=" + NewEncoder.GetBytes().Length + " bytes"); byte[] TexData = NewEncoder.GetBytes(); bw.Write(TexData); bw.Close(); } fs.Close(); //Debug.Log("JPG saved"); } }
void SaveJPG(string filename, int width, int height, Color[] pixels) { FileStream fs = new FileStream(filename, FileMode.Create); if ( fs != null ) { BinaryWriter bw = new BinaryWriter(fs); if ( bw != null ) { Quality = Mathf.Clamp(Quality, 0.0f, 100.0f); JPGEncoder NewEncoder = new JPGEncoder(pixels, width, height, Quality); NewEncoder.doEncoding(); byte[] TexData = NewEncoder.GetBytes(); bw.Write(TexData); bw.Close(); } fs.Close(); } }