Skip to content Skip to sidebar Skip to footer

How Can I Generate Multiple Values Qr Code In Android Studio

I would like to enter multiple text fields for example name email password address And then I would like to generate a QR code from this input. How can I do that in android studi

Solution 1:

Setting up the library and manifest

Open App level gradle file and import the library.

implementation 'androidmads.library.qrgenearator:QRGenearator:1.0.3'

The, click “Sync Now”. Then, open your Manifest file and add the following permissions. It is used to save QR Code to file storage.

<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

We need to handle runtime permissions from Android Version 6.0.

Generating QR Code

QRGEncoderqrgEncoder=newQRGEncoder(inputValue, null, QRGContents.Type.TEXT, smallerDimension);  

Here, inputValue is an input to be converted to QR Code. Input Type also can be specified while initializing the library. We can specify the dimensions also. Then, add the following lines to create QR Code and encode that into Bitmap Format.

try {  
  // Getting QR-Code as Bitmap  
  bitmap = qrgEncoder.encodeAsBitmap();  
  // Setting Bitmap to ImageView  
  qrImage.setImageBitmap(bitmap);  
} catch (WriterException e) {  
  Log.v(TAG, e.toString());  
}  

qrImage is an ImageView used to preview the generated QR code bitmap.

Saving QR Code

QR Generator has an option to save the generated QR Code Bitmap to storage using the following lines.

// Save with location, value, bitmap returned and type of Image(JPG/PNG).  
QRGSaver.save(savePath, edtValue.getText().toString().trim(), bitmap, QRGContents.ImageType.IMAGE_JPEG);  

We can save QR Code in PNG & JPG format also. We have to handle runtime permissions from Android version 6.0.

Your particular case: Combine the information you want to encode in the QR code, and add it as the inputValue for the QRGEncoder. Here is an example code for clarity:

publicclassMainActivityextendsAppCompatActivity {  

    StringTAG="GenerateQRCode";  
    EditText edtValue;  
    ImageView qrImage;  
    Button start, save;  
    String inputValue;  
    StringsavePath= Environment.getExternalStorageDirectory().getPath() + "/QRCode/";  
    Bitmap bitmap;  
    QRGEncoder qrgEncoder;  

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  

        qrImage = (ImageView) findViewById(R.id.QR_Image);  
        edtValue = (EditText) findViewById(R.id.edt_value);  
        start = (Button) findViewById(R.id.start);  
        save = (Button) findViewById(R.id.save);  

        start.setOnClickListener(newView.OnClickListener() {  
            @OverridepublicvoidonClick(View view) {  
                inputValue = edtValue.getText().toString().trim();  
                if (inputValue.length() > 0) {  
                    WindowManagermanager= (WindowManager) getSystemService(WINDOW_SERVICE);  
                    Displaydisplay= manager.getDefaultDisplay();  
                    Pointpoint=newPoint();  
                    display.getSize(point);  
                    intwidth= point.x;  
                    intheight= point.y;  
                    intsmallerDimension= width < height ? width : height;  
                    smallerDimension = smallerDimension * 3 / 4;  

                    qrgEncoder = newQRGEncoder(  
                            inputValue, null,  
                            QRGContents.Type.TEXT,  
                            smallerDimension);  
                    try {  
                        bitmap = qrgEncoder.encodeAsBitmap();  
                        qrImage.setImageBitmap(bitmap);  
                    } catch (WriterException e) {  
                        Log.v(TAG, e.toString());  
                    }  
                } else {  
                    edtValue.setError("Required");  
                }  
            }  
        });  

        save.setOnClickListener(newView.OnClickListener() {  
            @OverridepublicvoidonClick(View v) {  
                boolean save;  
                String result;  
                try {  
                    save = QRGSaver.save(savePath, edtValue.getText().toString().trim(), bitmap, QRGContents.ImageType.IMAGE_JPEG);  
                    result = save ? "Image Saved" : "Image Not Saved";  
                    Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();  
                } catch (Exception e) {  
                    e.printStackTrace();  
                }  
            }  
        });  

    }  
} 

Article: https://www.c-sharpcorner.com/article/how-to-generate-qr-code-in-android/

Solution 2:

Concatenate all the information in a string and do a hash on said string. Next use a library such as (https://github.com/zxing/zxing) to generate the QR code.

Solution 3:

Use this for generating qr code online. Then use picasso to load the image. ( Use your data in the url parameter ) https://chart.googleapis.com/chart?chs=500x500&cht=qr&chl=data I found it as the best way.

Post a Comment for "How Can I Generate Multiple Values Qr Code In Android Studio"