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();
}
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" }
}
}