A QR Code is an abbreviation of Quick Response Code, a two-dimensional barcode containing text data. Originally developed by the auto industry in Japan for inventory control, today they commonly contain URL’s for immediate access to a website. They are on everything from billboards to cereal boxes to business cards.

AdminsCache

This QR Code contains the text – http://AdminsCache.Wordpress.com.
Scanning with the barcode scanner of a smart camera will allow you to be redirected to the site.

A company called SYN Systems GmbH has very graciously provide a public API to their QR Code generation service called “create-qr-code“. It is a nice API providing many functions to customize your QR Code including fore and background color, ECC depth, different file formats and image and quiet zone sizing. You can squeeze up to 900 characters in the code, depending on quiet zone size and other factors. The Quiet Zone is blank space around the barcode which allows the scanner to recognize the object as a barcode, separating it from the background.

In a WinForms Form I setup the Labels and TextBoxes for gathering the parameters to send to the QR Code generation service. I center the form in the display and select the colors and size. For some input I provide a Help Label to show valid parameters.

Add-Type -AssemblyName System.Windows.Forms
# Main Form
$mainForm = New-Object System.Windows.Forms.Form
$mainForm.Font = "Comic Sans MS,10"
$mainForm.Text = " Generate QR Code Image File"
$mainForm.StartPosition = "centerscreen"
$mainForm.ForeColor = "White"
$mainForm.BackColor = "Wheat"
$mainForm.Size = "460, 460"
$mainForm.MinimumSize = "460, 460"

# Size Label
$sizeLabel = New-Object System.Windows.Forms.Label
$sizeLabel.Location = "40,40"
$sizeLabel.ForeColor = "MediumBlue"
$sizeLabel.Size = "80, 22"
$sizeLabel.Text = "Image Size"
$mainForm.Controls.Add($sizeLabel)

# Size Help Label
$sizeHelpLabel = New-Object System.Windows.Forms.Label
$sizeHelpLabel.Location = "230,40"
$sizeHelpLabel.ForeColor = "DarkOrange"
$sizeHelpLabel.Size = "200, 22"
$sizeHelpLabel.Text = "10x10 - 1000x1000  (Square)"
$mainForm.Controls.Add($sizeHelpLabel)

# Size TextBox
$sizeTextBox = New-Object System.Windows.Forms.TextBox
$sizeTextBox.Location = "120,40"
$sizeTextBox.Size = "100,20"
$sizeTextBox.ForeColor = "MediumBlue"
$sizeTextBox.BackColor = "White"
$sizeTextBox.Text = "250x250"
$mainForm.Controls.Add($sizeTextBox)

For convenience I use a ComboBox control to offer the valid File Format selections. Since there were only three options I just plugged them in directly instead of using an Array or Here-String to hold the values. EPS and SVG are also supported by the API but the memory used and files sizes get very large fast and so I didn’t include them.

# Format Label
$fileFormatLabel = New-Object System.Windows.Forms.Label
$fileFormatLabel.Location = "40,100"
$fileFormatLabel.ForeColor = "MediumBlue"
$fileFormatLabel.Size = "80, 22"
$fileFormatLabel.Text = "File Format"
$mainForm.Controls.Add($fileFormatLabel)

# Format ComboBox
$fileFormatComboBox = New-Object System.Windows.Forms.ComboBox
$fileFormatComboBox.Location = "120,100"
$fileFormatComboBox.Size = "60,20"
$fileFormatComboBox.ForeColor = "MediumBlue"
$fileFormatComboBox.BackColor = "White"
$fileFormatComboBox.Items.Add("gif")
$fileFormatComboBox.Items.Add("jpg")
$fileFormatComboBox.Items.Add("png")
$fileFormatComboBox.Text = "jpg"
$mainForm.Controls.Add($fileFormatComboBox)

After completing the parameter selection you press the “Generate” button which calls the GenerateQRCode Function. In that Function I first reset the error checking variables. I define the path to the QR Code image file as the location of the script file.

$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition

The file name consist of “QRCode” followed by the Date and the Time all underscore separated. The Extension is selectable. This will allow for all files generated to be uniquely named.

QRCodeFileNames

I then define the WebClient Class object. I use this to download the newly created image file from the website of the generation service. So I call the DownLoadFile Method of the WebClient which takes two parameters, URI and local path. The URI is the URL of the code generation site calling the API while passing the collected configuration parameters.

I validate no errors during the call or file write and write the results code to the Form. Next the PictureBox Function is called to display the created QR Code, dispose of the WebClient object and exit the Function.

# Generate Button
$generateButton = New-Object System.Windows.Forms.Button
$generateButton.Location = "15,340"
$generateButton.Size = "75,28"
$generateButton.ForeColor = "DarkBlue"
$generateButton.BackColor = "White"
$generateButton.Text = "Generate"
$generateButton.add_Click({GenerateQRCode($scriptPath)})
$mainForm.Controls.Add($generateButton)

Function GenerateQRcode {
    $error.clear()
	$path = $scriptPath + "\QRCode_" + (Get-Date -UFormat %Y%m%d_%H%M%S) + "." + $fileFormatComboBox.Text
    $WebClient = new-object System.Net.WebClient
    $WebClient.DownloadFile(("http://api.qrserver.com/v1/create-qr-code/?data=" + $encodeTextBox.Text + `
        "&ecc=" + $eccTextBox.Text +`
        "&size=" + $sizeTextBox.Text +`
        "&qzone=" + $quietZoneTextBox.Text + `
        "&color=" + $foreColorTextBox.Text + `
        "&bgcolor=" + $backColorTextBox.Text + `
        "&format=" + $fileFormatComboBox.Text), $path)
    If($error.count -gt 0) {
        $completedLabel.Text = "Error!"
        $completedLabel.ForeColor = "Red"
    }
    Else {
        $completedLabel.Text = "Success!"
        $completedLabel.ForeColor = "Green"
    }
    PictureBox($path)
    $WebClient.Dispose()
}

In the PictureBox Function I read the QR Code image file back in and display it in a PictureBox Control. I use the Zoom SizeMode Property to keep the image sized to fit within the defined ClientSize Property boundary.

# PictureBox
$pictureBox = New-Object System.Windows.Forms.PictureBox
$pictureBox.Location = "120, 150"
$pictureBox.ClientSize = "250, 250"
$pictureBox.SizeMode = "Zoom"
$mainForm.Controls.Add($pictureBox)

Function PictureBox {
    $file = (Get-Item $path)
    $image = [System.Drawing.Image]::Fromfile($file)
    $pictureBox.Image = $image
}

Lastly I have an Exit button to close the Form and end the Session.

# Exit Button
$exitButton = New-Object System.Windows.Forms.Button
$exitButton.Location = "15,380"
$exitButton.Size = "75,28"
$exitButton.ForeColor = "Red"
$exitButton.BackColor = "White"
$exitButton.Text = "Exit"
$exitButton.add_Click({$mainForm.close()})
$mainForm.Controls.Add($exitButton)

QRCode

Download – QRCode.ps1

Advertisements