Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a QR code Generator for Android using Fragments

I am relatively new to android development and so far I haven't picked up on the purpose of fragments. Anyways, I am trying to create a unique QR code for every user that sign's into my app. This QR code is filled with the user's info which I have retrieved from the database. Now the only trouble I am experiencing is the generation of said qr Code. I have looked through dozens of tutorials but oftentimes they are rendered invalid for my use case or I can't simply get them to work. I have also looked through the ZXING api but that was of not help. I ask of the StackoverFlow community to help with this endeavour and all help is appreciated

like image 376
saj5211 Avatar asked Dec 05 '22 13:12

saj5211


1 Answers

There is a slight difference usage between Activity and Fragment. Both of them can be used to show the UI. To use a fragment, you need the Activity as its host, because Fragment must always be embedded in the Activity.

From the documentation:

A Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities).

A fragment must always be embedded in an activity and the fragment's lifecycle is directly affected by the host activity's lifecycle.

You need to read Building a Dynamic UI with Fragments to mastering the Fragment. The steps to create a Fragment (Note, this not a strict rule):

  1. Create the Activity as the host
  2. Create the layout for Activity. Within, you need to create a FrameLayout view as the fragment holder.
  3. Create the Fragment by extending Fragment class
  4. Create the UI layout for the Fragment
  5. Attach the Fragment to Activity by using FragmentTransaction

Now the create QR code part. You need to determine what information need to be shown in your QR code. Don't give all information in the QR code, because you mustn't expose all your user data to the world. If you have more than 1 string of information, you can use ";" or anything else to join the text info.

To build the QR code image, first, you need to include the ZXing library by using the following line in your app build.gradle (use the latest version):

compile 'com.google.zxing:core:3.3.0'

Then create the QR code bitmap using the following code:

private Bitmap textToImage(String text, int width, int height) throws WriterException, NullPointerException {
  BitMatrix bitMatrix;
  try {
    bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.DATA_MATRIX.QR_CODE, 
                   width, height, null);
  } catch (IllegalArgumentException Illegalargumentexception) {
    return null;
  }

  int bitMatrixWidth = bitMatrix.getWidth();
  int bitMatrixHeight = bitMatrix.getHeight();
  int[] pixels = new int[bitMatrixWidth * bitMatrixHeight];

  int colorWhite = 0xFFFFFFFF;
  int colorBlack = 0xFF000000;

  for (int y = 0; y < bitMatrixHeight; y++) {
    int offset = y * bitMatrixWidth;
    for (int x = 0; x < bitMatrixWidth; x++) {
      pixels[offset + x] = bitMatrix.get(x, y) ? colorBlack : colorWhite;
    }
  }
  Bitmap bitmap = Bitmap.createBitmap(bitMatrixWidth, bitMatrixHeight, Bitmap.Config.ARGB_4444);

  bitmap.setPixels(pixels, 0, width, 0, 0, bitMatrixWidth, bitMatrixHeight);
  return bitmap;
}

Then you can use it to set the generated image to your ImageView in your Fragment with something like this:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
  View view = inflater.inflate(R.layout.your_fragment_layout, container, false);
  ImageView imvQrCode = (ImageView) view.findViewById(R.id.your_image_view);

  Bitmap bitmap = textToImage("your_text_info", 500, 500);
  imageView.setImageBitmap(bitmap);

  return view;
}
like image 156
ישו אוהב אותך Avatar answered Dec 08 '22 01:12

ישו אוהב אותך