Search code examples
javaandroidqr-code

QR Code Generator with number picker and date picker in android


I am new to android studio, I am doing a qr code project which i have used date picker and number picker and i have tried to write code and it came out with different result which not i expected. I have seen most of the qr code generator tutorial or example, it used zxing library and edittext for the qr code generator, so I have tried with myself coding it with date picker and nubmer picker. How Can I make it the result which i expected? result like this: 1234567<-id 1019<-time 15<-minutes

MyCode java class:

public class Visitor extends Activity {

    DatePicker timeDp;
    NumberPicker minNp;
    private String[] minutesArray;
    Button createBtn;
    ImageView qrcodeIv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.qrcode);
        minNp = findViewById(R.id.npMin);
        minNp.setMaxValue(5);
        minNp.setMinValue(0);
        minutesArray = new String[] {"15","30","45","60","75","90"};
        minNp.setDisplayedValues(minutesArray);
        qrcodeIv = findViewById(R.id.ivQrcode);
        createBtn = findViewById(R.id.btnCreate);

        Map<String,String> parameters = new HashMap<String,String>();
        int value = minNp.getValue();
        parameters.put("mins", Integer.toString(value));

        createBtn.setOnClickListener(view -> {
            MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
            try {
                BitMatrix bitMatrix = multiFormatWriter.encode(String.valueOf(minNp), BarcodeFormat.QR_CODE,400,400);
                BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
                Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
                qrcodeIv.setImageBitmap(bitmap);
                InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                manager.hideSoftInputFromWindow(minNp.getApplicationWindowToken(), 0);
            } catch (WriterException e) {
                e.printStackTrace();
            }
        });
    }
}

qr code design page:

<TextView
        android:id="@+id/txtTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Please Select Time"
        android:textSize="20dp"
        android:textStyle="bold"
        android:textColor="@color/black"/>

    <TimePicker
        android:id="@+id/tp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:timePickerMode="spinner" />

    <TextView
        android:id="@+id/txtMin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Please Select Minutes"
        android:textSize="20dp"
        android:textStyle="bold"
        android:textColor="@color/black"/>

    <NumberPicker
        android:id="@+id/npMin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btnCreate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Create"
        />

    <ImageView
        android:id="@+id/ivQrcode"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginTop="50dp"/>

I had my qr code result show like this: android.widget.NumberPicker{ba98302 VFED..... ........ 452,914-628,1409 #7f08013f app:id/npMin}.

I had import everything which I needed in android studio, but the result didn't show what I want.

My expected result should be like this: 1234567 1019 15 ^ ^ ^ id time minutes


Solution

  • In the line

    BitMatrix bitMatrix = multiFormatWriter.encode(String.valueOf(minNp), BarcodeFormat.QR_CODE,400,400);

    by calling String.valueOf(minNp) you create a String that describes the instance of NumberPicker. Basically this is just another way of calling the toString() method of minNp.

    You need to call minNp.getValue() which will return the value as an int. This value should be the index in the minutesArray.

    The code would look like this:

    BitMatrix bitMatrix = multiFormatWriter.encode(minutesArray[minNp.getValue()], BarcodeFormat.QR_CODE,400,400);