UnityNativeCamera icon indicating copy to clipboard operation
UnityNativeCamera copied to clipboard

Resize or Set quality

Open kadirmenz opened this issue 3 years ago • 9 comments

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.

kadirmenz avatar Feb 04 '22 14:02 kadirmenz

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

yasirkula avatar Feb 04 '22 14:02 yasirkula

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?

kadirmenz avatar Feb 04 '22 14:02 kadirmenz

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

yasirkula avatar Feb 04 '22 14:02 yasirkula

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

kadirmenz avatar Feb 04 '22 14:02 kadirmenz

You need to decrease Constants.MAX_SIZE.

yasirkula avatar Feb 04 '22 14:02 yasirkula

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

kadirmenz avatar Feb 04 '22 15:02 kadirmenz

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.

yasirkula avatar Feb 04 '22 15:02 yasirkula

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

kadirmenz avatar Feb 04 '22 15:02 kadirmenz

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

yasirkula avatar Feb 04 '22 15:02 yasirkula