1

I need to create a QRcode from a string that I have randomly generated. I have looked into using zxing for doing this but I'm unsure if its the best way for creating the QRcode. The code below is the work I've done so far. All it does is when I press the button, it displays the random string in a TextView. Just wondering is there a simple way of doing this? Thanks.

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            textView.setText(getRandomString(12));

        }
    });

}

private static String getRandomString(int i ){
    final String chars = "abcdefghijklmonpqrstuvwxyz0123456789";
    StringBuilder results = new StringBuilder();
    while (i > 0) {

        Random rand = new Random();
        results.append(chars.charAt(rand.nextInt(chars.length())));
        i--;
    }
    return results.toString();
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Mark_rath
  • 45
  • 7
  • 1
    Does this answer your question? [How to generate a QR Code for an Android application?](https://stackoverflow.com/questions/8800919/how-to-generate-a-qr-code-for-an-android-application) – Martin Feb 26 '20 at 16:57
  • @Martin I'll have a look at it thanks. – Mark_rath Feb 26 '20 at 17:14

1 Answers1

2

I could propose you an easy to use library QRGen (link to Github). It's based on Zxing and produces bitmaps from String. Code samples are in README. I didn't write this library, but during using it I could say that it works. My code sample:

String str = "your randomized string"
Bitmap bmp = QRCode.from(contactString).bitmap()
// use a bitmap with bmp variable

And inside build.gradle you should use 'implementation' method instead of 'compile' (it's recently deprecated).

implementation 'com.github.kenglxn.QRGen:android:2.6.0'

Remark: remember about adding a jitpack.io repository for maven in a build.gradle file:

allprojects {
    repositories {
        // ...
        maven { url "https://jitpack.io" }
    }
}
Daniel Galion
  • 106
  • 1
  • 6