yeahdongcn / RSBarcodes_Swift

1D and 2D barcodes reader and generators for iOS 8 with delightful controls. Now Swift.
MIT License
709 stars 185 forks source link

QRCode appears small #46

Closed cafuni closed 7 years ago

cafuni commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

@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 commented 8 years ago

Have you tried to change the scale to larger value?

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

bdrelling commented 8 years ago

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 commented 8 years ago

@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 commented 8 years ago

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 commented 8 years ago

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

yeahdongcn commented 8 years ago

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