温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

利用Android怎么实现一个二维码生成功能

发布时间:2020-11-26 17:13:17 来源:亿速云 阅读:194 作者:Leah 栏目:移动开发

利用Android怎么实现一个二维码生成功能?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

1.在project的build.gradle添加如下代码:

allprojects {
  repositories {
    maven { 
      url 'https://jitpack.io'
     }
  }
}

2.在build.gradle添加依赖:

dependencies {
  compile 'com.github.goodboy321:Scan-Zxing:1.0'
}

布局:

<EditText
  android:id="@+id/et"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:hint="输入内容,生成二维码"
  android:text="http://www.baidu.com" />

<Button
  android:background="@color/colorAccent"
  android:id="@+id/btn2"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:text="生成二维码" />

<Button
  android:layout_marginTop="10dp"
  android:background="@color/colorPrimaryDark"
  android:id="@+id/btn1"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:text="扫码(识别相册中二维码)" />

<ImageView
  android:id="@+id/image"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center_horizontal" />

<ImageView
  android:id="@+id/image_callback"
  android:layout_marginTop="10dp"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_gravity="center_horizontal" />

主方法:

public void onClick(View view) {
  switch (view.getId()) {
    case R.id.btn1:
      Intent intent = new Intent(mContext, CaptureActivity.class);
      startActivityForResult(intent, REQUEST);
      break;
    case R.id.btn2:
      image.setVisibility(View.VISIBLE);
      //隐藏扫码结果view
      imageCallback.setVisibility(View.GONE);

      String content = et.getText().toString().trim();
      Bitmap bitmap = null;
      try {
        bitmap = BitmapUtils.create2DCode(content);//根据内容生成二维码
        tvResult.setVisibility(View.GONE);
        image.setImageBitmap(bitmap);
      } catch (Exception e) {
        e.printStackTrace();
      }
      break;
   }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  if (requestCode == REQUEST) {
    image.setVisibility(View.GONE);
    imageCallback.setVisibility(View.VISIBLE);

    String result = data.getStringExtra(CaptureActivity.SCAN_QRCODE_RESULT);
    Bitmap bitmap = data.getParcelableExtra(CaptureActivity.SCAN_QRCODE_BITMAP);

    if(bitmap != null){
      imageCallback.setImageBitmap(bitmap);//现实扫码图片
    }
  }

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI