ExportedFunctions/Format-QRCode.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<#
.SYNOPSIS
    Takes a QRCode object and formats it for output on the screen.

.DESCRIPTION
    Uses line drawing to convert a QRCode for display on the screen.

.PARAMETER QRCode
    A QRCode output from ConvertTo-QRCode

.PARAMETER TopPadding
    The ammount of padding to put around the code.

.PARAMETER SidePadding
    The ammount of padding to put around the code. You may need to add padding depending on the font used in your terminal.

.PARAMETER CharacterWidth
    The number of characters to use to output each block of the QRCode.

.PARAMETER Invert
    The default PowerShell console is light text on a dark background. If you are using dark text on a light background
    you will need to invert the output so the barcode is the correct color.
#>

function Format-QRCode {
    param(
        [Parameter(ValueFromPipeline=$true,Mandatory=$true)]
        [ZXing.QrCode.Internal.QRCode]$QRCode,
        
        [Parameter(Mandatory=$false)]
        $TopPadding = 2,
        
        [Parameter(Mandatory=$false)]
        $SidePadding = 2,
        
        [Parameter(Mandatory=$false)]
        $CharacterWidth = 1,
        
        [switch]$Invert
    )
    
    #These seem backwards, but in the default ps console they come out correct (dark bg, light text)
    if($Invert) {
        $BlankChar = " "
        $SolidChar = [string][char]0x2588
        $TopActiveChar = [string][char]0x2580
        $BottomActiveChar = [string][char]0x2584
    } else {
        $BlankChar = [string][char]0x2588
        $SolidChar = " "
        $TopActiveChar = [string][char]0x2584
        $BottomActiveChar = [string][char]0x2580
    }

    $SB = New-Object Text.StringBuilder
    
    1..$TopPadding | %{
        #Write-Host ($BlankChar * ($QRCode.Matrix.Width + $SidePadding + $SidePadding) * $CharacterWidth) -fore black -back white
        $SB.AppendLine($BlankChar * ($QRCode.Matrix.Width + $SidePadding + $SidePadding) * $CharacterWidth) > $null
    }
    for($r = 0; $r -lt $QRCode.Matrix.Height; $r++) {
        $SB.Append($BlankChar * $SidePadding * $CharacterWidth) > $null
        for($c = 0; $c -lt $QRCode.Matrix.Width; $c++) {
            $ThisRowEntry = $QRCode.Matrix.Array[$c][$r]
            if($r -lt $QRCode.Matrix.Height - 1) {
                $NextRowEntry = $QRCode.Matrix.Array[$c][$r + 1]
            } else {
                $NextRowEntry = 0
            }
            
            if($ThisRowEntry -And $NextRowEntry) {
                #Make a solid block
                $Out = $SolidChar
            } elseif ($ThisRowEntry -And !$NextRowEntry) {
                $Out = $TopActiveChar
            } elseif (!$ThisRowEntry -And $NextRowEntry) {
                $Out = $BottomActiveChar
            } else {
                $Out = $BlankChar
            }
            
            $SB.Append($Out) > $null
        }
        $r++
        $SB.AppendLine($BlankChar * $SidePadding * $CharacterWidth) > $null
    }
    1..$TopPadding | %{
        #Write-Host ($BlankChar * ($QRCode.Matrix.Width + $SidePadding + $SidePadding) * $CharacterWidth) -fore black -back white
        $SB.AppendLine($BlankChar * ($QRCode.Matrix.Width + $SidePadding + $SidePadding) * $CharacterWidth) > $null
    }
    $SB.ToString()
}
Export-ModuleMember -Function Format-QRCode