I did my own deep dive on TOTP a few years ago and wrote a blog post on how to implement a Google Authenticator compatible 2fa system in java including generating QR codes[0]. Demo code is on GitHub[1].
[0] https://www.asaph.org/2016/04/google-authenticator-2fa-java....