Image saved to gallery is black
Hello everyone. In my app i want to make a picture and save it to gallery for android and iOS. As far as I need to capture a specific part of the screen and save only specific objects, I am using the second camera to capture what I need. Following code shows how I do it:
private Texture2D TakeSnapshot()
{
//this is for background so the empty space under the objects is not just black or something
snapshotBackground.transform.position = new Vector3(0, 0, 1);
//read the texture from camera and save it in my Texture2D object
Texture2D snapShot = new Texture2D(Screen.width, Screen.height, TextureFormat.ARGB32, false);
RenderTexture snapShotRT = new RenderTexture(Screen.width, Screen.height, 24, RenderTextureFormat.ARGB32);
RenderTexture.active = snapShotRT;
lineRenderingCamera.targetTexture = snapShotRT;
lineRenderingCamera.Render();
snapShot.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
snapShot.Apply();
//release
RenderTexture.active = null;
lineRenderingCamera.targetTexture = null;
//move the background away so it cannot be seen anymore
snapshotBackground.transform.position = new Vector3(0, 50, 0);
return snapShot;
}
Afterwards I save this snapshot to the asset folder when I am in the editor, and everything is just fine - it saves exactly what I want it to save. But when I test it on any android device - the returned Texture2D is just black, without any screenshots, and I have no idea why. I tried to read pixels from the main camera(thought that on device camera coordinates somehow differ), but still, just a fully black texture. Please, help!
Answer by Lazybones94 · Oct 19, 2015 at 01:41 PM
And again I solved the problem by myself, lol, sorry for disturbing, guys. In case someone experienced the same problem - I just needed to convert the method into IEnumerator and use WaitForEndOfFrame before using RenderTexture.
Hey there, I'm having what sounds like the same problem. Could you share your solution?
$$anonymous$$y current code looks like this:
IEnumerator TakePhoto(string filename){
// We should only read the screen buffer after rendering is complete
yield return new WaitForEndOfFrame();
Texture2D photo = new Texture2D(webCamTexture.width, webCamTexture.height);
photo.SetPixels(webCamTexture.GetPixels());
photo.Apply();
//Encode to a JPG
byte[] bytes = photo.EncodeToJPG();
//Write out the JPG
System.IO.File.WriteAllBytes( filename , bytes);
}
Your answer
Follow this Question
Related Questions
ScreenCapture.CaptureScreenshotAsTexture() is making a milky white tinted screenshot. 1 Answer
how can i store a screenshot into android mobile? 0 Answers
Wrong Coloring (Sometimes) when capturing "Screen"-shot 1 Answer
Zooming in to a certain area of UI 0 Answers
Taking a Screenshot and then displaying the Screenshot in game 0 Answers