利用Android怎么实现一个二维码生成功能?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。在project的build.gradle添加如下代码:allproj
利用Android怎么实现一个二维码生成功能?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
在project的build.gradle添加如下代码:
allprojects { repositories { Maven { url 'https://jitpack.io' } }}
在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; }}@Overrideprotected 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);//现实扫码图片 } }
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注编程网精选频道,感谢您对编程网的支持。
--结束END--
本文标题: 利用Android怎么实现一个二维码生成功能
本文链接: https://www.lsjlt.com/news/224793.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0