Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resize or Set quality #85

Open
kadirmenz opened this issue Feb 4, 2022 · 9 comments
Open

Resize or Set quality #85

kadirmenz opened this issue Feb 4, 2022 · 9 comments

Comments

@kadirmenz
Copy link

Hi,
I capture image from camera and then send it as WWWForm to my server. it works on gallery side with small images but when I use camera my server gives this error:
413 Request Entity Too Large
So I think I need to resize or decrease quality of captured picture to send it server for save.

@yasirkula
Copy link
Owner

yasirkula commented Feb 4, 2022

You can call NativeCamera.LoadImageAtPath(imagePath, maxImageDimensions, false) and then re-encode the returned texture to JPEG or PNG.

@kadirmenz
Copy link
Author

kadirmenz commented Feb 4, 2022

Actually I can successfully show image on unity client with LoadImageAtPath. But after that I prepare WWWForm to send this image to server.
Like that:

public void TakePicture()
    {
        NativeCamera.Permission permission = NativeCamera.TakePicture((path) =>
        {
            string fileName = Path.GetFileName(path);
            string extension = Path.GetExtension(path).Substring(1);
            string type = "image/" + extension;
            if (path != null)
            {
                Texture2D texture = NativeCamera.LoadImageAtPath(path, Constants.MAX_SIZE);
                if (texture == null)
                {
                    Debug.Log(Constants.COULDNT_LOAD_TEXTURE);
                    return;
                }
                Sprite tempSprite = Sprite.Create(
                    texture,
                    new Rect(0.0f, 0.0f, texture.width, texture.height),
                    new Vector2(0.5f, 0.5f),
                    100.0f
                );
                userAvatarEditProfile.sprite = tempSprite;
                avatarFormData = new WWWForm();
                avatarFormData.AddBinaryData("avatar", File.ReadAllBytes(path), fileName, type);
            }
        }, Constants.MAX_SIZE);
        CameraAndGalleryPanel.SetActive(false);
        Debug.Log("Permission result: " + permission);
    }

So I send bytes of image in path, before that how can I resize or decrease its quality?

@yasirkula
Copy link
Owner

You can use texture.EncodeToPNG or EncodeToJPG but LoadImageAtPath's 3rd parameter must be false.

@kadirmenz
Copy link
Author

public void TakePicture()
    {
        NativeCamera.Permission permission = NativeCamera.TakePicture((path) =>
        {
            string fileName = Path.GetFileName(path);
            string extension = Path.GetExtension(path).Substring(1);
            string type = "image/" + extension;
            if (path != null)
            {
                Texture2D texture = NativeCamera.LoadImageAtPath(path, Constants.MAX_SIZE, false);
                if (texture == null)
                {
                    Debug.Log(Constants.COULDNT_LOAD_TEXTURE);
                    return;
                }
                Sprite tempSprite = Sprite.Create(
                    texture,
                    new Rect(0.0f, 0.0f, texture.width, texture.height),
                    new Vector2(0.5f, 0.5f),
                    100.0f
                );
                userAvatarEditProfile.sprite = tempSprite;
                byte[] bytes = texture.EncodeToPNG();
                avatarFormData = new WWWForm();
                avatarFormData.AddBinaryData("avatar", bytes, fileName, type);
            }
        }, Constants.MAX_SIZE);
        CameraAndGalleryPanel.SetActive(false);
        Debug.Log("Permission result: " + permission);
    }

Again It gives 413 Request Entity Too Large error when I send avatarFormData to my server, If I choose small images it works fine but when I choose big images or unresized images it gives this image, I font understand why :/
Is there any way to decrease image quality? I think it gives this error for big quality images

@yasirkula
Copy link
Owner

You need to decrease Constants.MAX_SIZE.

@kadirmenz
Copy link
Author

I already try it :/, First I set it 512 then down 256 also try 128 and others

@yasirkula
Copy link
Owner

lol 512 itself is also a fairly small number. I wonder how small the server expects the images to be. You can use EncodeToJPG with a lower quality parameter if you wish but transparency will be lost.

@kadirmenz
Copy link
Author

Yes now it works when I use EncodeToJPG, But why it doesnt work via EncodeToPNG. even I didnt give quality parameter to EncodeToJPG it still works fine. Weird

@yasirkula
Copy link
Owner

I wouldn't expect a massive size difference either. But as a side note, EncodeToJPG's default quality value is 75, not 100.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants