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

Image size issue #74

Open
Ahmedlag opened this issue Jun 5, 2024 · 5 comments
Open

Image size issue #74

Ahmedlag opened this issue Jun 5, 2024 · 5 comments

Comments

@Ahmedlag
Copy link

Ahmedlag commented Jun 5, 2024

Hello,

I'm facing an issue setting captured image size in iOS & Android

I'm using capacitor 6 and plugin version 6.2.13

When i set the image size :

return CameraPreview.capture({
  quality: 66,
  width: 600,
  height: 1000
});

The captured image will never have the resolution i've chosen ! The only thing i can control is the quality

Any solution to this ?

@riderx
Copy link
Contributor

riderx commented Jun 5, 2024

That an issue I'm currently trying to solve.
The size should be a size available on the device, if not, we try to send the one who matches the ratio. If we don't have any, we return the biggest.
I think I will disable the width and height and let it only be chosen in the configuration, that will be less misleading

@Ahmedlag
Copy link
Author

Ahmedlag commented Jun 5, 2024

@riderx thank you

i've worked on something but i'm not sure if it is the right approach

(For iOS)
I've added a function called resizeImage that will resize the image before converting it to a jpeg

func resizeImage(image: UIImage, targetSize: CGSize) -> UIImage {
    let size = image.size

    let widthRatio  = targetSize.width  / size.width
    let heightRatio = targetSize.height / size.height

    var newSize: CGSize
    if widthRatio > heightRatio {
        newSize = CGSize(width: size.width * heightRatio, height: size.height * heightRatio)
    } else {
        newSize = CGSize(width: size.width * widthRatio, height: size.height * widthRatio)
    }

    let rect = CGRect(origin: .zero, size: newSize)

    UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
    image.draw(in: rect)
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return newImage!
}

And i call the method once the image is captured

@objc func capture(_ call: CAPPluginCall) {
    DispatchQueue.main.async {

        let quality: Int = call.getInt("quality", 85)
        let width: Int = call.getInt("width", 650)
        let height: Int = call.getInt("height", 1000)


        self.cameraController.captureImage { (image, error) in

            guard let image = image else {
                print(error ?? "Image capture error")
                guard let error = error else {
                    call.reject("Image capture error")
                    return
                }
                call.reject(error.localizedDescription)
                return
            }
            
            let resizedImage = self.resizeImage(image: image, targetSize: CGSize(width: width, height: height))
            
            let imageData: Data?
            if self.cameraController.currentCameraPosition == .front {
                let flippedImage = resizedImage.withHorizontallyFlippedOrientation()
                imageData = flippedImage.jpegData(compressionQuality: CGFloat(quality/100))
            } else {
                imageData = resizedImage.jpegData(compressionQuality: CGFloat(quality/100))
            }

            if self.storeToFile == false {
                let imageBase64 = imageData?.base64EncodedString()
                call.resolve(["value": imageBase64!])
            } else {
                do {
                    let fileUrl=self.getTempFilePath()
                    try imageData?.write(to: fileUrl)
                    call.resolve(["value": fileUrl.absoluteString])
                } catch {
                    call.reject("error writing image to file")
                }
            }
        }
    }
}

@riderx
Copy link
Contributor

riderx commented Jun 10, 2024

This will break the aspect ratio of the image right?

@Ahmedlag
Copy link
Author

@riderx "resizeImage" doesnt break the aspect ratio of the image, it ensures that the aspect ratio is maintained by calculating the correct scaling factor based on the target size.

@riderx
Copy link
Contributor

riderx commented Jun 11, 2024

if the image is 19:9 and we want 4:3 where does the pixel go then ?
Or we break the pixel size and force the ratio or the image will have empty space?
Maybe can you share image output to explain if i miss something?

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