Question by
keninger · Jul 25, 2018 at 07:02 PM ·
androidscreenshotqualitycompression
Android Screenshot Quality
Hi,
I have a script which takes a screenshot of my screen and saves it. I get the desired resolution, but the quality is very low as an HD image comes out to be a 50kb image.
What needs to be modified in the code so it won't compress the image so much?
protected const string MEDIA_STORE_IMAGE_MEDIA = "android.provider.MediaStore$Images$Media";
protected static AndroidJavaObject m_Activity;
protected static string SaveImageToGallery(Texture2D a_Texture, string a_Title, string a_Description)
{
using (AndroidJavaClass mediaClass = new AndroidJavaClass(MEDIA_STORE_IMAGE_MEDIA))
{
using (AndroidJavaObject contentResolver = Activity.Call<AndroidJavaObject>("getContentResolver"))
{
AndroidJavaObject image = Texture2DToAndroidBitmap(a_Texture);
return mediaClass.CallStatic<string>("insertImage", contentResolver, image, a_Title, a_Description);
}
}
}
protected static AndroidJavaObject Texture2DToAndroidBitmap(Texture2D a_Texture)
{
byte[] encodedTexture = a_Texture.EncodeToJPG(100);
using (AndroidJavaClass bitmapFactory = new AndroidJavaClass("android.graphics.BitmapFactory"))
{
return bitmapFactory.CallStatic<AndroidJavaObject>("decodeByteArray", encodedTexture, 0, encodedTexture.Length);
}
}
protected static AndroidJavaObject Activity
{
get
{
if (m_Activity == null)
{
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
m_Activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
}
return m_Activity;
}
}
StartCoroutine(CaptureScreenshotCoroutine(Screen.width,Screen.height));
yield return new WaitForSeconds(0.02f);
Instantiate(screenshotSavedMessage);
for (int i = 0; i < itemsToHideForScreenshot.Length; i++)
{
itemsToHideForScreenshot[i].SetActive(true);
}
}
//-------------------------------------------------------------------------------------
private IEnumerator CaptureScreenshotCoroutine(int width, int height)
{
// I am assuming somethings should be changed here somewhere
yield return new WaitForEndOfFrame();
Texture2D tex = new Texture2D(width, height);
tex.Compress(false);
tex.ReadPixels(new Rect(0, 0, width, height), 0,0,false);
tex.Apply();
yield return tex;
string path = SaveImageToGallery(tex, "LogDyno", "Dyno Graph");
Debug.Log("Picture has been saved at:\n" + path);
}
Comment
Your answer
Follow this Question
Related Questions
How to save screenshots to com.company.product folder? 1 Answer
Making VR-screenshot on Android 0 Answers
Losing image quality in Android after build 2 Answers
Take screenshot with no lags Android 0 Answers
Texture compression for Android devices 0 Answers