如何使用 Fragments 为 Android 创建二维码生成器

How to create a QR code Generator for Android using Fragments

我是 android 开发的新手,到目前为止我还没有了解片段的用途。不管怎样,我正在尝试为每个登录我的应用程序的用户创建一个唯一的二维码。这个二维码填充了我从数据库中检索到的用户信息。现在我遇到的唯一 麻烦是生成上述二维码 。我浏览了数十个教程,但通常它们对我的用例无效,或者我不能简单地让它们工作。我也查看了 ZXING api 但这无济于事。我请求 Whosebug 社区帮助完成这项工作,感谢所有帮助

Activity 和 Fragment 的用法略有不同。它们都可以用来显示UI。要使用片段,您需要 Activity 作为它的宿主,因为片段必须始终嵌入在 Activity.

来自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.

您需要阅读 Building a Dynamic UI with Fragments 才能掌握片段。创建片段的步骤(注意,这不是严格的规则):

  1. 创建Activity作为主机
  2. 为 Activity 创建布局。在其中,您需要创建一个 FrameLayout 视图作为片段持有者。
  3. 通过扩展 Fragment 创建 Fragment class
  4. 为片段创建 UI 布局
  5. 使用 FragmentTransaction
  6. 将片段附加到 Activity

现在是创建二维码部分。您需要确定您的二维码中需要显示哪些信息。不要在二维码中提供所有信息,因为您不能将所有用户数据暴露给全世界。如果你有超过1串的信息,你可以使用“;”或其他任何加入文本信息的内容。

要构建二维码图像,首先需要包含ZXing library by using the following line in your app build.gradle (use the latest version):

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

然后使用以下代码创建二维码位图:

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;
}

然后你可以使用它将生成的图像设置到你的 Fragment 中的 ImageView,如下所示:

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;
}
    try {
Bitmap bitmap= encodeAsBitmap("Muhammad Qasim Android Developer", BarcodeFormat.QR_CODE, Width, Height);

                        if (bitmap!= null) {
                            img.setImageBitmap(bitmap);
                        }
                    } catch (WriterException e) {

                            Log.e(""+e,"Exception");
                    }

你可以找到here how to create and use Fragments. But for generating QR Code I found one which is very tiny and smart library QRGen

示例代码:

Bitmap myBitmap = QRCode.from("www.example.org").bitmap();
ImageView myImage = (ImageView) findViewById(R.id.imageView);
myImage.setImageBitmap(myBitmap);