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

QRCode appears small #46

Closed
cafuni opened this issue Feb 16, 2016 · 8 comments
Closed

QRCode appears small #46

cafuni opened this issue Feb 16, 2016 · 8 comments

Comments

@cafuni
Copy link

cafuni commented Feb 16, 2016

the QRCode appears very small...

let image: UIImage? = gen.generateCode(contents, machineReadableCodeObjectType: AVMetadataObjectTypeQRCode)
if let image = image {
self.barcodeView.image = RSAbstractCodeGenerator.resizeImage(image, scale: 1.0)
}

@bdrelling
Copy link

Really have an issue with this and not enthused that it hasn't been acknowledged yet.

Scaling it keeps it blurry and I can't see any way even with the native QR Code generation to get a large image.

@bdrelling
Copy link

@cafuni I was able to get around this by adding a wrapper method that checks the type of barcode. If it is a QR Code, I use the following method:

private class func createQRCode(string: String, correctionLevel: InputCorrectionLevel = .Medium, sizeToFill: CGSize? = nil) -> UIImage? {
        let data = string.dataUsingEncoding(NSISOLatin1StringEncoding, allowLossyConversion: false)

        guard let filter = CIFilter(name: "CIQRCodeGenerator") else {
            return nil
        }

        filter.setValue(data, forKey: "inputMessage")
        filter.setValue(correctionLevel.rawValue, forKey: "inputCorrectionLevel")

        guard let outputImage = filter.outputImage else {
            return nil
        }

        if (sizeToFill != nil) {
            let imageSize = outputImage.extent.size
            let scaleX = sizeToFill!.width / imageSize.width
            let scaleY = sizeToFill!.height / imageSize.height
            let newImage = outputImage.imageByApplyingTransform(CGAffineTransformMakeScale(scaleX, scaleY))

            return UIImage(CIImage: newImage)
        }

        return UIImage(CIImage: outputImage)
    }

Hopefully, the author will support something like this going forward in order to get a proper size and scale the CIImage appropriately.

@yeahdongcn
Copy link
Owner

Have you tried to change the scale to larger value?

self.imageDisplayed.image = RSAbstractCodeGenerator.resizeImage(image!, scale: 10.0)

@bdrelling
Copy link

This works, but it only goes by scale. That's not as efficient when I'm trying to fill the bounds of something to the best capability and don't necessarily know what scale is too much to fill, or if the imageView itself is flexible.

I ended up doing something like this:

let smallerFillLength = (sizeToFill!.height < sizeToFill!.width) ? sizeToFill!.height : sizeToFill!.width
let largerCodeImageLength = (codeImage.size.height > codeImage.size.width) ? codeImage.size.height : codeImage.size.width
let fillScale = smallerFillLength / largerCodeImageLength

return RSAbstractCodeGenerator.resizeImage(codeImage, scale: fillScale)

Where sizeToFill is a CGSize I can pass in, typically the size of the imageView or bounding area I want to get.

I use the smallest of all the bounding lengths and the largest of the code image length since it should be a square.

Any better way to achieve this?

@yeahdongcn
Copy link
Owner

@bdrelling Your solution is great, I can provide one static function later to achieve this, one of the parameters should be contentMode (UIViewContentMode), what do you think?

@yeahdongcn
Copy link
Owner

I've added one helper method to resize image, you may take a look @bdrelling @cafuni

public class func resizeImage(source:UIImage, targetSize:CGSize, contentMode:UIViewContentMode) -> UIImage

@bdrelling
Copy link

@yeahdongcn I will take a look at this soon. Thanks for the response!

@yeahdongcn
Copy link
Owner

Any ideas about the new resize method? @bdrelling @cafuni

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

3 participants