TeamsやZoomの会議のログインには、招待URLをQR画像にして表示、スマホやタブレットにかざして、さっと入室するのが便利ですよね。特にOutlook上、招待された会議スケジュールを選択した状態でボタンに登録したOutlookマクロを実行、自動で会議スケジュール本文内の招待URLをQR画像に変換してシームレスに表示するマクロを作成し、おお、これはよい!と思っていたところ、一つの懸念が・・・。
QR生成にGoogleなど外部APIを使用したら、社外に会議URLが駄々洩れになってしまうではないか。
したがいまして、社内セキュアなサーバーにURL文字列をリクエストするとQR画像(というか画像のBASE64文字列)を返すAPIを実装して、それを使用していたのですが、どうも納得がいかない、釈然としない。VBAプログラム歴30年の私としては(外部や内部)APIなどに頼らず、自力VBAだけでQRコードを生成したいではありませんか!Outlookで動作させたいので、ExcelやAccessのランタイムも使用せず、純粋かつピュアな自作モジュール一つで実現しようではありませんか!
調べてみると、本当にごくわずか、先人達の事例がありました。https://github.com/yas78/QRCodeLibVBA
いや、これは素晴らしそうなのですが、モジュールがたくさんあってコードも大量、いやいや多機能は不要で、シンプルなモジュール一つで実装したいのです。
さらに調査を進めたらこちら
https://qiita.com/santarou6/items/d623417ea8ba33756108
とてもよさそう!
でも文字数に限界があるようで、超長いテキストだと作成されず、ExcelVBAでセルの背景色でQRを表現しているので、OutookVBAでそのまま利用するのは難しいかなと。
(一方で、セルドット方式かつ条件付き書式でワークシート上にQR画像を表現されていることに感銘を覚えました)
ということで、自作しかない!と思い立ち、以下のコンセプトで作り始めたのが、もうかれこれ2年前。
・他言語含む既存コードの移植ではなくゼロから実装しVBAの威力を最大化させる
・バージョンは最大の40まで対応し、長文テキストにも対応
・画像はWin32APIのGDI+で高速に作成
さらに、outlookVBAのモジュール一つに収めたく、ソースコードを簡素化するため、実用性を確保しつつ最小限のコードで実装する以下のアプローチをとりました。
・「日本語+英数字」を安全確実に扱うため汎用性の高いバイナリUTF-8に特化(Shift-JIS必須なKanji モードは非採用)
・マスクパターンは0で固定
・誤り訂正レベルはM(復元15%)で固定、文字数によりL(復元7%)に自動移行し格納文字数を最大化(これにより日本語全角984文字、半角英数字2953文字まで達成!)
まあ、レベルMでも、全角777文字まで行けますし、会議URLが2953文字になることはないので、復元レベルはM固定でエスカレーションしなくてもよい気もしますが、このあたりはVBAエンジニア魂といいましょうか、ソースコードが複雑にならない範囲で、出来る限り実装したわけです。
ここに至るまでは長い紆余曲折がありました。書いては実行、書いては実行、正常なQRが作成されない日々、いったいどこが悪いのか、核となる生成多項式が誤っているのか、はたまた画像作成GDI+でこけているのか、いやいやそもそもQRの仕様理解が足りていないのか、ChatGPTに聞いても一向に解決する兆しなし・・・2年に及ぶプログラミングバトルの結果、ついに完成したoutlookVBA(というか全Office共通VBA)コード、公開します!
このコードは例によって著作権フリーです。自由に利用いただき、outlookでのWeb会議ログイン業務の効率化や、OfficeVBAでQRコードを生成するアプリケーション作成にお役立てください。
|
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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 |
'Lを実装した980文字までいけるやつ Option Explicit ' ============================================================================ ' 1. Windows API Declarations ' ============================================================================ Private Type GdiplusStartupInput GdiplusVersion As Long DebugEventCallback As LongPtr SuppressBackgroundThread As Long SuppressExternalCodecs As Long End Type Private Type GUID Data1 As Long Data2 As Integer Data3 As Integer Data4(0 To 7) As Byte End Type ' Kernel & Shell Private Declare PtrSafe Function WideCharToMultiByte Lib "kernel32" ( _ ByVal CodePage As Long, ByVal dwFlags As Long, _ ByVal lpWideCharStr As LongPtr, ByVal cchWideChar As Long, _ ByVal lpMultiByteStr As LongPtr, ByVal cbMultiByte As Long, _ ByVal lpDefaultChar As LongPtr, ByVal lpUsedDefaultChar As LongPtr) As Long Private Declare PtrSafe Function GetTempPath Lib "kernel32" Alias "GetTempPathA" ( _ ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long Private Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _ ByVal hwnd As LongPtr, ByVal lpOperation As String, _ ByVal lpFile As String, ByVal lpParameters As String, _ ByVal lpDirectory As String, ByVal nShowCmd As Long) As LongPtr Private Declare PtrSafe Function CLSIDFromString Lib "ole32" ( _ ByVal lpsz As LongPtr, ByRef pclsid As GUID) As Long ' GDI+ Private Declare PtrSafe Function GdiplusStartup Lib "gdiplus" ( _ ByRef token As LongPtr, ByRef inputBuf As GdiplusStartupInput, _ ByVal outputBuf As LongPtr) As Long Private Declare PtrSafe Function GdiplusShutdown Lib "gdiplus" ( _ ByVal token As LongPtr) As Long Private Declare PtrSafe Function GdipCreateBitmapFromScan0 Lib "gdiplus" ( _ ByVal Width As Long, ByVal Height As Long, ByVal stride As Long, _ ByVal PixelFormat As Long, ByVal scan0 As LongPtr, _ ByRef bitmap As LongPtr) As Long Private Declare PtrSafe Function GdipGetImageGraphicsContext Lib "gdiplus" ( _ ByVal image As LongPtr, ByRef graphics As LongPtr) As Long Private Declare PtrSafe Function GdipFillRectangleI Lib "gdiplus" ( _ ByVal graphics As LongPtr, ByVal brush As LongPtr, _ ByVal x As Long, ByVal y As Long, _ ByVal Width As Long, ByVal Height As Long) As Long Private Declare PtrSafe Function GdipCreateSolidFill Lib "gdiplus" ( _ ByVal Color As Long, ByRef brush As LongPtr) As Long Private Declare PtrSafe Function GdipDeleteBrush Lib "gdiplus" ( _ ByVal brush As LongPtr) As Long Private Declare PtrSafe Function GdipDeleteGraphics Lib "gdiplus" ( _ ByVal graphics As LongPtr) As Long Private Declare PtrSafe Function GdipDisposeImage Lib "gdiplus" ( _ ByVal image As LongPtr) As Long Private Declare PtrSafe Function GdipSaveImageToFile Lib "gdiplus" ( _ ByVal image As LongPtr, ByVal filename As LongPtr, _ ByRef clsidEncoder As GUID, ByVal encoderParams As LongPtr) As Long Private Const CP_UTF8 As Long = 65001 Private Const PixelFormat32bppARGB As Long = &H26200A ' ============================================================================ ' 2. GF(256) / Global Tables ' ============================================================================ Private EXP_TABLE(255) As Long Private LOG_TABLE(255) As Long Private IsTablesInit As Boolean ' ============================================================================ ' 3. Public Entry Point ' - まず EC=M で生成を試し、ダメなら EC=L でフォールバック ' - 使用された Ver / ECレベル は Debug.Print で出力 ' ============================================================================ Sub testQR() Dim inputStr As String Dim url As String Dim i As Long '日本語930文字を生成 Randomize For i = 1 To 930 inputStr = inputStr & Chr(-32096 + Int(Rnd * 50)) Next i url = GenerateQR(inputStr) If url <> "" Then Call ShellExecute(0, "open", "mspaint.exe", """" & url & """", "", 1) End If End Sub Public Function GenerateQR(inputStr As String) As String Dim qrBytes() As Byte Dim mSize As Long Dim tempPath As String If Len(inputStr) = 0 Then Exit Function If Not CreateQR_Main(inputStr, qrBytes, mSize) Then MsgBox "生成エラー:データ量が多すぎるか、不正な文字が含まれています。", vbCritical Exit Function End If tempPath = GetSystemTempPath() & "qr_vba_" & Format(Now, "yyyymmdd_hhnnss") & ".png" If SaveImageGDI(qrBytes, mSize, tempPath, 10) Then GenerateQR = tempPath Else MsgBox "画像の保存に失敗しました。", vbCritical End If End Function ' ============================================================================ ' 4. Core QR Logic (ラッパ:M→L フォールバック) ' ============================================================================ ' まず EC=M でトライし、ダメなら EC=L で再トライ Private Function CreateQR_Main(ByVal text As String, _ ByRef mOut() As Byte, _ ByRef sizeOut As Long) As Boolean ' 1. 誤り訂正レベル M でチャレンジ If CreateQR_OneLevel(text, mOut, sizeOut, "M") Then Debug.Print "QR ENCODED: EC=M" CreateQR_Main = True Exit Function End If ' 2. M で入らなかったら、誤り訂正レベル L で再チャレンジ If CreateQR_OneLevel(text, mOut, sizeOut, "L") Then Debug.Print "QR ENCODED: EC=L" CreateQR_Main = True Exit Function End If ' 3. L でもダメなら完全に容量オーバー Debug.Print "QR ENCODE FAILED: too long for Ver40-L" CreateQR_Main = False End Function ' 指定した EC レベル( "M" または "L" )で生成(実体) Private Function CreateQR_OneLevel(ByVal text As String, _ ByRef mOut() As Byte, _ ByRef sizeOut As Long, _ ByVal ecLevel As String) As Boolean Dim rawBytes() As Byte Dim dataLen As Long Dim ver As Long Dim dataCW As Long, ecCW As Long, numBlocks As Long Dim headBits As Long Dim capBits As Long Dim bitStream() As Byte Dim bitPtr As Long Dim i As Long, j As Long If Not IsTablesInit Then InitGaloisTables ' 1. UTF-8 に変換 rawBytes = StrToUtf8(text) If (Not Not rawBytes) = 0 Then CreateQR_OneLevel = False Exit Function End If dataLen = UBound(rawBytes) + 1 ' 2. バージョン決定 (Ver1?40) For ver = 1 To 40 If ecLevel = "M" Then GetVerParamsM ver, dataCW, ecCW, numBlocks Else GetVerParamsL ver, dataCW, ecCW, numBlocks End If If dataCW <= 0 Then Exit For headBits = 4 + IIf(ver <= 9, 8, 16) ' モード(4bit) + 文字数ビット(Byte) If (dataLen * 8 + headBits) <= (dataCW * 8) Then Exit For Next ver If ver > 40 Or dataCW <= 0 Then CreateQR_OneLevel = False Exit Function End If ' ★ここで決定したバージョン/データバイト数をデバッグ出力 Debug.Print "QR DETAIL: EC=" & ecLevel & _ ", Ver=" & ver & ", DataBytes=" & dataLen ' 3. データビット列作成 sizeOut = 17 + 4 * ver ReDim mOut(sizeOut - 1, sizeOut - 1) capBits = dataCW * 8 ReDim bitStream(capBits - 1) bitPtr = 0 ' モード (0100 = Byte) AppendBits bitStream, bitPtr, 4, 4 ' 文字数 (Byte 数) AppendBits bitStream, bitPtr, dataLen, IIf(ver <= 9, 8, 16) ' データ本体 For i = 0 To dataLen - 1 AppendBits bitStream, bitPtr, rawBytes(i), 8 Next i ' Terminator Dim remainBits As Long remainBits = capBits - bitPtr If remainBits > 4 Then remainBits = 4 If remainBits > 0 Then AppendBits bitStream, bitPtr, 0, remainBits ' 8bit 境界に合わせる If (bitPtr Mod 8) <> 0 Then AppendBits bitStream, bitPtr, 0, 8 - (bitPtr Mod 8) End If ' パディング (0xEC, 0x11) Dim padA As Long, padB As Long padA = &HEC: padB = &H11 Do While bitPtr < capBits AppendBits bitStream, bitPtr, padA, 8 If bitPtr >= capBits Then Exit Do AppendBits bitStream, bitPtr, padB, 8 Loop ' 4. データコードワード配列化 Dim finalData() As Byte ReDim finalData(dataCW - 1) For i = 0 To dataCW - 1 finalData(i) = bitStream(i) Next i ' 5. RSブロック分割 & パリティ計算 Dim cwPerBlockBase As Long Dim numLongBlocks As Long Dim numShortBlocks As Long Dim blocksData() As Variant Dim blocksEcc() As Variant Dim cursor As Long Dim genPoly() As Long Dim thisLen As Long cwPerBlockBase = dataCW \ numBlocks numLongBlocks = dataCW Mod numBlocks numShortBlocks = numBlocks - numLongBlocks ReDim blocksData(numBlocks - 1) ReDim blocksEcc(numBlocks - 1) cursor = 0 genPoly = CalcGenPoly(ecCW) For j = 0 To numBlocks - 1 If j < numShortBlocks Then thisLen = cwPerBlockBase Else thisLen = cwPerBlockBase + 1 End If Dim chunk() As Byte ReDim chunk(thisLen - 1) For i = 0 To thisLen - 1 chunk(i) = finalData(cursor) cursor = cursor + 1 Next i blocksData(j) = chunk blocksEcc(j) = CalcReedSolomon(chunk, ecCW, genPoly) Next j ' 6. インターリーブ (データ + EC) Dim totalCW As Long totalCW = dataCW + ecCW * numBlocks Dim interleaved() As Byte ReDim interleaved(totalCW - 1) Dim idx As Long Dim maxDataLen As Long idx = 0 maxDataLen = cwPerBlockBase + IIf(numLongBlocks > 0, 1, 0) ' データ部 For i = 0 To maxDataLen - 1 For j = 0 To numBlocks - 1 Dim tmp() As Byte tmp = blocksData(j) If i <= UBound(tmp) Then interleaved(idx) = tmp(i) idx = idx + 1 End If Next j Next i ' EC 部 For i = 0 To ecCW - 1 For j = 0 To numBlocks - 1 Dim ecc() As Byte ecc = blocksEcc(j) interleaved(idx) = ecc(i) idx = idx + 1 Next j Next i ' 7. 機能パターン配置 Dim isFunc() As Byte ReDim isFunc(sizeOut - 1, sizeOut - 1) SetupPatterns mOut, isFunc, ver, sizeOut ' 8. データ配置 (ZigZag) Dim r As Long, c As Long Dim bitIdx As Long Dim dir As Long Dim byteVal As Byte Dim row As Long, col As Long Dim val As Long bitIdx = 0 dir = -1 c = sizeOut - 1 Do While c > 0 If c = 6 Then c = c - 1 ' タイミングパターン列をスキップ For i = 0 To sizeOut - 1 If dir = -1 Then row = sizeOut - 1 - i Else row = i End If For j = 0 To 1 col = c - j If isFunc(row, col) = 0 Then val = 0 If bitIdx < totalCW * 8 Then byteVal = interleaved(bitIdx \ 8) If (byteVal And (2 ^ (7 - (bitIdx Mod 8)))) <> 0 Then val = 1 End If bitIdx = bitIdx + 1 End If mOut(row, col) = val End If Next j Next i c = c - 2 dir = -dir Loop ' 9. マスク適用 (パターン 0 : (r+c) Mod 2 = 0) For r = 0 To sizeOut - 1 For c = 0 To sizeOut - 1 If isFunc(r, c) = 0 Then If ((r + c) Mod 2) = 0 Then mOut(r, c) = mOut(r, c) Xor 1 End If End If Next c Next r ' 10. フォーマット情報 (EC と Mask=0) Dim fmtBits As String If ecLevel = "M" Then ' EC=M, Mask=0 → 101010000010010 fmtBits = "101010000010010" Else ' EC=L, Mask=0 → 111011111000100 fmtBits = "111011111000100" End If ' Top-left 周辺 SetModule mOut, 8, 0, Mid$(fmtBits, 1, 1) SetModule mOut, 8, 1, Mid$(fmtBits, 2, 1) SetModule mOut, 8, 2, Mid$(fmtBits, 3, 1) SetModule mOut, 8, 3, Mid$(fmtBits, 4, 1) SetModule mOut, 8, 4, Mid$(fmtBits, 5, 1) SetModule mOut, 8, 5, Mid$(fmtBits, 6, 1) ' (8,6) はタイミングパターン SetModule mOut, 8, 7, Mid$(fmtBits, 7, 1) SetModule mOut, 8, 8, Mid$(fmtBits, 8, 1) SetModule mOut, 7, 8, Mid$(fmtBits, 9, 1) ' (6,8) はタイミングパターン SetModule mOut, 5, 8, Mid$(fmtBits, 10, 1) SetModule mOut, 4, 8, Mid$(fmtBits, 11, 1) SetModule mOut, 3, 8, Mid$(fmtBits, 12, 1) SetModule mOut, 2, 8, Mid$(fmtBits, 13, 1) SetModule mOut, 1, 8, Mid$(fmtBits, 14, 1) SetModule mOut, 0, 8, Mid$(fmtBits, 15, 1) ' 反対側コピー SetModule mOut, sizeOut - 1, 8, Mid$(fmtBits, 1, 1) SetModule mOut, sizeOut - 2, 8, Mid$(fmtBits, 2, 1) SetModule mOut, sizeOut - 3, 8, Mid$(fmtBits, 3, 1) SetModule mOut, sizeOut - 4, 8, Mid$(fmtBits, 4, 1) SetModule mOut, sizeOut - 5, 8, Mid$(fmtBits, 5, 1) SetModule mOut, sizeOut - 6, 8, Mid$(fmtBits, 6, 1) SetModule mOut, sizeOut - 7, 8, Mid$(fmtBits, 7, 1) SetModule mOut, 8, sizeOut - 8, Mid$(fmtBits, 8, 1) SetModule mOut, 8, sizeOut - 7, Mid$(fmtBits, 9, 1) SetModule mOut, 8, sizeOut - 6, Mid$(fmtBits, 10, 1) SetModule mOut, 8, sizeOut - 5, Mid$(fmtBits, 11, 1) SetModule mOut, 8, sizeOut - 4, Mid$(fmtBits, 12, 1) SetModule mOut, 8, sizeOut - 3, Mid$(fmtBits, 13, 1) SetModule mOut, 8, sizeOut - 2, Mid$(fmtBits, 14, 1) SetModule mOut, 8, sizeOut - 1, Mid$(fmtBits, 15, 1) ' 11. バージョン情報 (Ver7 以上) If ver >= 7 Then ApplyVersionInfo mOut, ver, sizeOut End If ' 12. Dark module mOut(4 * ver + 9, 8) = 1 CreateQR_OneLevel = True End Function Private Sub SetModule(ByRef m() As Byte, ByVal r As Long, ByVal c As Long, ByVal valStr As String) m(r, c) = IIf(valStr = "1", 1, 0) End Sub ' ============================================================================ ' 5. Math Helpers (GF(256), RS) ' ============================================================================ Private Sub InitGaloisTables() Dim i As Long, x As Long x = 1 For i = 0 To 255 EXP_TABLE(i) = x LOG_TABLE(x) = i x = x * 2 If x > 255 Then x = x Xor &H11D Next i IsTablesInit = True End Sub Private Function GFMul(ByVal x As Long, ByVal y As Long) As Long Dim v As Long If x = 0 Or y = 0 Then GFMul = 0 Else v = LOG_TABLE(x) + LOG_TABLE(y) GFMul = EXP_TABLE(v Mod 255) End If End Function ' 生成多項式 g(x) = Π (x - α^i), i = 0..ecCount-1 Private Function CalcGenPoly(ByVal ecCount As Long) As Long() Dim poly() As Long Dim i As Long, j As Long Dim root As Long Dim newPoly() As Long ReDim poly(0) poly(0) = 1 For i = 0 To ecCount - 1 root = EXP_TABLE(i) ReDim newPoly(UBound(poly) + 1) ' x倍 For j = 0 To UBound(poly) newPoly(j) = poly(j) Next j newPoly(UBound(newPoly)) = 0 ' (x - α^i) との掛け算 For j = 0 To UBound(poly) newPoly(j + 1) = newPoly(j + 1) Xor GFMul(poly(j), root) Next j poly = newPoly Next i CalcGenPoly = poly End Function Private Function CalcReedSolomon(data() As Byte, _ ByVal ecCount As Long, _ genPoly() As Long) As Byte() Dim ec() As Long Dim i As Long, j As Long Dim coef As Long ReDim ec(ecCount - 1) For i = 0 To UBound(data) coef = data(i) Xor ec(0) ' シフト For j = 0 To ecCount - 2 ec(j) = ec(j + 1) Next j ec(ecCount - 1) = 0 If coef <> 0 Then For j = 0 To ecCount - 1 ec(j) = ec(j) Xor GFMul(genPoly(j + 1), coef) Next j End If Next i Dim ret() As Byte ReDim ret(ecCount - 1) For i = 0 To ecCount - 1 ret(i) = CByte(ec(i)) Next i CalcReedSolomon = ret End Function Private Sub AppendBits(arr() As Byte, ByRef ptr As Long, ByVal val As Long, ByVal bits As Long) Dim i As Long Dim bit As Long Dim idx As Long Dim off As Long For i = bits - 1 To 0 Step -1 bit = (val \ (2 ^ i)) And 1 If bit = 1 Then idx = ptr \ 8 off = 7 - (ptr Mod 8) arr(idx) = arr(idx) Or (2 ^ off) End If ptr = ptr + 1 Next i End Sub Private Function StrToUtf8(ByVal s As String) As Byte() Dim L As Long Dim b() As Byte L = WideCharToMultiByte(CP_UTF8, 0, StrPtr(s), -1, 0, 0, 0, 0) If L <= 1 Then ReDim b(0) b(0) = 0 StrToUtf8 = b Exit Function End If ReDim b(L - 2) Call WideCharToMultiByte(CP_UTF8, 0, StrPtr(s), -1, VarPtr(b(0)), L, 0, 0) StrToUtf8 = b End Function ' ============================================================================ ' 6. Patterns (Finder / Alignment / Timing / Version) ' ============================================================================ Private Sub SetupPatterns(ByRef m() As Byte, ByRef f() As Byte, _ ByVal ver As Long, ByVal size As Long) Dim i As Long, r As Long, c As Long ' Finder patterns FillFinder m, f, 0, 0, size FillFinder m, f, size - 7, 0, size FillFinder m, f, 0, size - 7, size ' Alignment patterns If ver >= 2 Then Dim ap As Variant Dim j As Long ap = GetAP(ver) For i = LBound(ap) To UBound(ap) For j = LBound(ap) To UBound(ap) r = ap(i): c = ap(j) If f(r, c) = 0 Then FillAlignment m, f, r, c Next j Next i End If ' Timing patterns For i = 8 To size - 9 m(6, i) = IIf(i Mod 2 = 0, 1, 0): f(6, i) = 1 m(i, 6) = IIf(i Mod 2 = 0, 1, 0): f(i, 6) = 1 Next i ' Format 情報予約 For i = 0 To 8 f(i, 8) = 1 f(8, i) = 1 Next i For i = 0 To 7 f(size - 1 - i, 8) = 1 f(8, size - 1 - i) = 1 Next i ' Version 情報予約 (ver >= 7) If ver >= 7 Then For r = 0 To 5 For c = 0 To 2 f(size - 11 + c, r) = 1 f(r, size - 11 + c) = 1 Next c Next r End If ' Dark module f(4 * ver + 9, 8) = 1 End Sub Private Sub FillFinder(ByRef m() As Byte, ByRef f() As Byte, _ ByVal r0 As Long, ByVal c0 As Long, _ ByVal size As Long) Dim i As Long, j As Long Dim y As Long, x As Long For i = -1 To 7 For j = -1 To 7 y = r0 + i x = c0 + j If y >= 0 And y < size And x >= 0 And x < size Then f(y, x) = 1 If i >= 0 And i <= 6 And j >= 0 And j <= 6 Then If i = 0 Or i = 6 Or j = 0 Or j = 6 _ Or (i >= 2 And i <= 4 And j >= 2 And j <= 4) Then m(y, x) = 1 Else m(y, x) = 0 End If Else m(y, x) = 0 End If End If Next j Next i End Sub Private Sub FillAlignment(ByRef m() As Byte, ByRef f() As Byte, _ ByVal r As Long, ByVal c As Long) Dim i As Long, j As Long For i = -2 To 2 For j = -2 To 2 f(r + i, c + j) = 1 If i = -2 Or i = 2 Or j = -2 Or j = 2 Or (i = 0 And j = 0) Then m(r + i, c + j) = 1 Else m(r + i, c + j) = 0 End If Next j Next i End Sub ' --- Version / EC パラメータ (EC Level M) ------------------------- ' dataCW: データコードワード総数 ' ecCW : 1ブロック当たりのECコードワード数 ' blocks: ブロック総数 (Group1 + Group2) Private Sub GetVerParamsM(ByVal v As Long, _ ByRef dataCW As Long, _ ByRef ecCW As Long, _ ByRef blocks As Long) Select Case v Case 1: dataCW = 16: ecCW = 10: blocks = 1 Case 2: dataCW = 28: ecCW = 16: blocks = 1 Case 3: dataCW = 44: ecCW = 26: blocks = 1 Case 4: dataCW = 64: ecCW = 18: blocks = 2 Case 5: dataCW = 86: ecCW = 24: blocks = 2 Case 6: dataCW = 108: ecCW = 16: blocks = 4 Case 7: dataCW = 124: ecCW = 18: blocks = 4 Case 8: dataCW = 154: ecCW = 22: blocks = 4 Case 9: dataCW = 182: ecCW = 22: blocks = 5 Case 10: dataCW = 216: ecCW = 26: blocks = 5 Case 11: dataCW = 254: ecCW = 30: blocks = 5 Case 12: dataCW = 290: ecCW = 22: blocks = 8 Case 13: dataCW = 334: ecCW = 22: blocks = 9 Case 14: dataCW = 365: ecCW = 24: blocks = 9 Case 15: dataCW = 415: ecCW = 24: blocks = 10 Case 16: dataCW = 453: ecCW = 28: blocks = 10 Case 17: dataCW = 507: ecCW = 28: blocks = 11 Case 18: dataCW = 563: ecCW = 26: blocks = 13 Case 19: dataCW = 627: ecCW = 26: blocks = 14 Case 20: dataCW = 669: ecCW = 26: blocks = 16 Case 21: dataCW = 714: ecCW = 26: blocks = 17 Case 22: dataCW = 782: ecCW = 28: blocks = 17 Case 23: dataCW = 860: ecCW = 28: blocks = 18 Case 24: dataCW = 914: ecCW = 28: blocks = 20 Case 25: dataCW = 1000: ecCW = 26: blocks = 21 Case 26: dataCW = 1062: ecCW = 28: blocks = 23 Case 27: dataCW = 1128: ecCW = 28: blocks = 25 Case 28: dataCW = 1193: ecCW = 28: blocks = 26 Case 29: dataCW = 1267: ecCW = 28: blocks = 28 Case 30: dataCW = 1373: ecCW = 28: blocks = 29 Case 31: dataCW = 1455: ecCW = 28: blocks = 31 Case 32: dataCW = 1541: ecCW = 28: blocks = 33 Case 33: dataCW = 1631: ecCW = 28: blocks = 35 Case 34: dataCW = 1725: ecCW = 28: blocks = 37 Case 35: dataCW = 1812: ecCW = 28: blocks = 38 Case 36: dataCW = 1914: ecCW = 28: blocks = 40 Case 37: dataCW = 1992: ecCW = 28: blocks = 43 Case 38: dataCW = 2102: ecCW = 28: blocks = 45 Case 39: dataCW = 2216: ecCW = 28: blocks = 47 Case 40: dataCW = 2334: ecCW = 28: blocks = 49 Case Else dataCW = 0: ecCW = 0: blocks = 0 End Select End Sub ' --- Version / EC パラメータ (EC Level L) ------------------------- Private Sub GetVerParamsL(ByVal v As Long, _ ByRef dataCW As Long, _ ByRef ecCW As Long, _ ByRef blocks As Long) Select Case v Case 1: dataCW = 19: ecCW = 7: blocks = 1 Case 2: dataCW = 34: ecCW = 10: blocks = 1 Case 3: dataCW = 55: ecCW = 15: blocks = 1 Case 4: dataCW = 80: ecCW = 20: blocks = 1 Case 5: dataCW = 108: ecCW = 26: blocks = 1 Case 6: dataCW = 136: ecCW = 18: blocks = 2 Case 7: dataCW = 156: ecCW = 20: blocks = 2 Case 8: dataCW = 194: ecCW = 24: blocks = 2 Case 9: dataCW = 232: ecCW = 30: blocks = 2 Case 10: dataCW = 274: ecCW = 18: blocks = 4 Case 11: dataCW = 324: ecCW = 20: blocks = 4 Case 12: dataCW = 370: ecCW = 24: blocks = 4 Case 13: dataCW = 428: ecCW = 26: blocks = 4 Case 14: dataCW = 461: ecCW = 30: blocks = 4 Case 15: dataCW = 523: ecCW = 22: blocks = 6 Case 16: dataCW = 589: ecCW = 24: blocks = 6 Case 17: dataCW = 647: ecCW = 28: blocks = 6 Case 18: dataCW = 721: ecCW = 30: blocks = 6 Case 19: dataCW = 795: ecCW = 28: blocks = 7 Case 20: dataCW = 861: ecCW = 28: blocks = 8 Case 21: dataCW = 932: ecCW = 28: blocks = 8 Case 22: dataCW = 1006: ecCW = 28: blocks = 9 Case 23: dataCW = 1094: ecCW = 30: blocks = 9 Case 24: dataCW = 1174: ecCW = 30: blocks = 10 Case 25: dataCW = 1276: ecCW = 26: blocks = 12 Case 26: dataCW = 1370: ecCW = 28: blocks = 12 Case 27: dataCW = 1468: ecCW = 30: blocks = 12 Case 28: dataCW = 1531: ecCW = 30: blocks = 13 Case 29: dataCW = 1631: ecCW = 30: blocks = 14 Case 30: dataCW = 1735: ecCW = 30: blocks = 15 Case 31: dataCW = 1843: ecCW = 30: blocks = 16 Case 32: dataCW = 1955: ecCW = 30: blocks = 17 Case 33: dataCW = 2071: ecCW = 30: blocks = 18 Case 34: dataCW = 2191: ecCW = 30: blocks = 19 Case 35: dataCW = 2306: ecCW = 30: blocks = 19 Case 36: dataCW = 2434: ecCW = 30: blocks = 20 Case 37: dataCW = 2566: ecCW = 30: blocks = 21 Case 38: dataCW = 2702: ecCW = 30: blocks = 22 Case 39: dataCW = 2812: ecCW = 30: blocks = 24 Case 40: dataCW = 2956: ecCW = 30: blocks = 25 Case Else dataCW = 0: ecCW = 0: blocks = 0 End Select End Sub ' Alignment pattern center coordinates Private Function GetAP(ByVal v As Long) As Variant Select Case v Case 1: GetAP = Array(6) Case 2: GetAP = Array(6, 18) Case 3: GetAP = Array(6, 22) Case 4: GetAP = Array(6, 26) Case 5: GetAP = Array(6, 30) Case 6: GetAP = Array(6, 34) Case 7: GetAP = Array(6, 22, 38) Case 8: GetAP = Array(6, 24, 42) Case 9: GetAP = Array(6, 26, 46) Case 10: GetAP = Array(6, 28, 50) Case 11: GetAP = Array(6, 30, 54) Case 12: GetAP = Array(6, 32, 58) Case 13: GetAP = Array(6, 34, 62) Case 14: GetAP = Array(6, 26, 46, 66) Case 15: GetAP = Array(6, 26, 48, 70) Case 16: GetAP = Array(6, 26, 50, 74) Case 17: GetAP = Array(6, 30, 54, 78) Case 18: GetAP = Array(6, 30, 56, 82) Case 19: GetAP = Array(6, 30, 58, 86) Case 20: GetAP = Array(6, 34, 62, 90) Case 21: GetAP = Array(6, 28, 50, 72, 94) Case 22: GetAP = Array(6, 26, 50, 74, 98) Case 23: GetAP = Array(6, 30, 54, 78, 102) Case 24: GetAP = Array(6, 28, 54, 80, 106) Case 25: GetAP = Array(6, 32, 58, 84, 110) Case 26: GetAP = Array(6, 30, 58, 86, 114) Case 27: GetAP = Array(6, 34, 62, 90, 118) Case 28: GetAP = Array(6, 26, 50, 74, 98, 122) Case 29: GetAP = Array(6, 30, 54, 78, 102, 126) Case 30: GetAP = Array(6, 26, 52, 78, 104, 130) Case 31: GetAP = Array(6, 30, 56, 82, 108, 134) Case 32: GetAP = Array(6, 34, 60, 86, 112, 138) Case 33: GetAP = Array(6, 30, 58, 86, 114, 142) Case 34: GetAP = Array(6, 34, 62, 90, 118, 146) Case 35: GetAP = Array(6, 30, 54, 78, 102, 126, 150) Case 36: GetAP = Array(6, 24, 50, 76, 102, 128, 154) Case 37: GetAP = Array(6, 28, 54, 80, 106, 132, 158) Case 38: GetAP = Array(6, 32, 58, 84, 110, 136, 162) Case 39: GetAP = Array(6, 26, 54, 82, 110, 138, 166) Case 40: GetAP = Array(6, 30, 58, 86, 114, 142, 170) Case Else: GetAP = Array(6) End Select End Function ' --- Version information (Ver7?40, 18bit BCH result) --- Private Sub ApplyVersionInfo(ByRef m() As Byte, ByVal ver As Long, ByVal size As Long) Dim bits As String bits = GetVersionBits(ver) If bits = "" Then Exit Sub Dim i As Long Dim L As Long Dim mod3 As Long Dim u As Long, v As Long Dim b As Long L = Len(bits) ' =18 固定 For i = 0 To 17 mod3 = i Mod 3 u = (i - mod3) \ 3 ' 0..5 v = size - 11 + mod3 ' size-11 .. size-9 ' 下位ビットから使用 If Mid$(bits, L - i, 1) = "1" Then b = 1 Else b = 0 ' bottom-left m(v, u) = b ' top-right m(u, v) = b Next i End Sub Private Function GetVersionBits(ByVal ver As Long) As String Select Case ver Case 7: GetVersionBits = "000111110010010100" Case 8: GetVersionBits = "001000010110111100" Case 9: GetVersionBits = "001001101010011001" Case 10: GetVersionBits = "001010010011010011" Case 11: GetVersionBits = "001011101111110110" Case 12: GetVersionBits = "001100011101100010" Case 13: GetVersionBits = "001101100001000111" Case 14: GetVersionBits = "001110011000001101" Case 15: GetVersionBits = "001111100100101000" Case 16: GetVersionBits = "010000101101111000" Case 17: GetVersionBits = "010001010001011101" Case 18: GetVersionBits = "010010101000010111" Case 19: GetVersionBits = "010011010100110010" Case 20: GetVersionBits = "010100100110100110" Case 21: GetVersionBits = "010101011010000011" Case 22: GetVersionBits = "010110100011001001" Case 23: GetVersionBits = "010111011111101100" Case 24: GetVersionBits = "011000111011000100" Case 25: GetVersionBits = "011001000111100001" Case 26: GetVersionBits = "011010111110101011" Case 27: GetVersionBits = "011011000010001110" Case 28: GetVersionBits = "011100110000011010" Case 29: GetVersionBits = "011101001100111111" Case 30: GetVersionBits = "011110110101110101" Case 31: GetVersionBits = "011111001001010000" Case 32: GetVersionBits = "100000100111010101" Case 33: GetVersionBits = "100001011011110000" Case 34: GetVersionBits = "100010100010111010" Case 35: GetVersionBits = "100011011110011111" Case 36: GetVersionBits = "100100101100001011" Case 37: GetVersionBits = "100101010000101110" Case 38: GetVersionBits = "100110101001100100" Case 39: GetVersionBits = "100111010101000001" Case 40: GetVersionBits = "101000110001101001" Case Else: GetVersionBits = "" End Select End Function ' ============================================================================ ' 7. Image Saving (GDI+) ' ============================================================================ Private Function SaveImageGDI(ByRef qData() As Byte, ByVal mSize As Long, _ ByVal fPath As String, ByVal dScale As Long) As Boolean Const MAX_PIX As Long = 240 ' 画像の1辺の最大ピクセル数 Dim t As LongPtr Dim si As GdiplusStartupInput Dim bmp As LongPtr, gr As LongPtr Dim brW As LongPtr, brB As LongPtr Dim dw As Long Dim r As Long, c As Long Dim enc As GUID Dim res As Long Dim totalSize As Long ' ▼今のdScaleでどのくらいのサイズになるか totalSize = (mSize + 8) * dScale ' 大きすぎる場合は、自動で縮小スケールを計算 If totalSize > MAX_PIX Then dScale = MAX_PIX \ (mSize + 8) If dScale < 3 Then dScale = 3 ' モジュールが細かすぎるのを防ぐ(最小3px) End If ' 念のため dScale が 0 以下になっていたらデフォルト If dScale <= 0 Then dScale = 3 ' 実際の画像サイズを再計算 dw = (mSize + 8) * dScale Debug.Print "SAVE: mSize=" & mSize & ", scale=" & dScale & ", pixels=" & dw si.GdiplusVersion = 1 If GdiplusStartup(t, si, 0) <> 0 Then Exit Function Call GdipCreateBitmapFromScan0(dw, dw, 0, PixelFormat32bppARGB, 0, bmp) Call GdipGetImageGraphicsContext(bmp, gr) Call GdipCreateSolidFill(&HFFFFFFFF, brW) Call GdipCreateSolidFill(&HFF000000, brB) Call GdipFillRectangleI(gr, brW, 0, 0, dw, dw) For r = 0 To mSize - 1 For c = 0 To mSize - 1 If qData(r, c) = 1 Then Call GdipFillRectangleI(gr, brB, (c + 4) * dScale, (r + 4) * dScale, dScale, dScale) End If Next c Next r Call CLSIDFromString(StrPtr("{557CF406-1A04-11D3-9A73-0000F81EF32E}"), enc) ' PNG res = GdipSaveImageToFile(bmp, StrPtr(fPath), enc, 0) GdipDeleteBrush brW GdipDeleteBrush brB GdipDeleteGraphics gr GdipDisposeImage bmp GdiplusShutdown t SaveImageGDI = (res = 0) End Function Private Function GetSystemTempPath() As String Dim b As String * 260 Dim n As Long n = GetTempPath(260, b) If n > 0 Then GetSystemTempPath = Left$(b, InStr(b, vbNullChar) - 1) Else GetSystemTempPath = Environ$("TEMP") & "\" End If End Function |

コメント