Android掃描和生成二維碼
本文實例為大家分享了Android掃描和生成二維碼的具體代碼,供大家參考,具體內(nèi)容如下
MainActivity.java
public class MainActivity extends AppCompatActivity {
private ImageView mImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button =findViewById(R.id.btn);
mImageView =findViewById(R.id.img);
button.setOnClickListener(new View.OnClickListener() { //點擊按鈕掃描二維碼
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,CaptureActivity.class);
startActivityForResult(intent,2);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==200&& resultCode==RESULT_OK){
if (data!=null){
String content = data.getStringExtra(Constant.CODED_CONTENT);
if (TextUtils.isEmpty(content)){
Toast.makeText(MainActivity.this, "您的輸入為空!", Toast.LENGTH_SHORT).show();
return;
}
Bitmap logo = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
try {
Bitmap bitmap = CodeCreator.createQRCode(content, 400, 400, logo);
mImageView.setImageBitmap(bitmap);
} catch (WriterException e) {
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"掃描"+content,Toast.LENGTH_SHORT).show();
}
}
}
}
activity.main.xml
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical"> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="掃一掃"/> <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </android.support.constraint.ConstraintLayout>
需要配置的權(quán)限
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.CAMERA"></uses-permission> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" /> <uses-permission android:name="android.permission.FLASHLIGHT" />
build.gradle
minSdkVersion 16 //配置16 implementation'com.github.yuzhiqiang1993:zxing:2.2.1' //依賴
外部build.gradle
allprojects {
repositories {
google()
jcenter()
maven { url 'https://jitpack.io' } //加這行代碼
}
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android開發(fā)實現(xiàn)拍照功能的方法實例解析
這篇文章主要介紹了Android開發(fā)實現(xiàn)拍照功能的方法,結(jié)合實例形式較為詳細的分析了Android拍照功能的具體實現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
Android實現(xiàn)TextView中的部分文字實現(xiàn)點擊跳轉(zhuǎn)功能
在移動端應用中,往往需要在一段文字中讓某幾個關(guān)鍵詞或短語具備點擊跳轉(zhuǎn)功能,如果將整段文字放到 Button、Link 或單獨的 View 中,通常會帶來布局復雜、樣式難以統(tǒng)一、可維護性差等問題,所以本文將給大家介紹Android實現(xiàn)TextView中的部分文字實現(xiàn)點擊跳轉(zhuǎn)功能2025-04-04
詳解Android自定義控件屬性TypedArray以及attrs
這篇文章主要為大家介紹了android自定義控件屬性TypedArray以及attrs,感興趣的小伙伴們可以參考一下2016-01-01
Android?TextView跑馬燈實現(xiàn)原理及方法實例
字的跑馬燈效果在移動端開發(fā)中是一個比較常見的需求場景,下面這篇文章主要給大家介紹了關(guān)于Android?TextView跑馬燈實現(xiàn)原理及方法的相關(guān)資料,需要的朋友可以參考下2022-05-05
Android設備adb連接后顯示device unauthorized解決方案
這篇文章主要為大家介紹了Android設備adb連接后顯示device unauthorized解決方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06
Android實現(xiàn)分享微信好友及出現(xiàn)閃退的解決辦法
這篇文章主要介紹了Android實現(xiàn)分享微信好友及出現(xiàn)閃退的解決辦法的相關(guān)資料,需要的朋友可以參考下2016-03-03
發(fā)布?Android?library?到?Maven?解析
這篇文章主要介紹了發(fā)布?Android?library到Maven解析,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09

