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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android ViewPager中顯示圖片與播放視頻的填坑記錄
這篇文章主要給介紹了關(guān)于Android ViewPager中顯示圖片與播放視頻的一些填坑記錄,文中通過示例代碼介紹的非常詳細(xì),對各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-05-05
Android實現(xiàn)捕獲未知異常并提交給服務(wù)器的方法
這篇文章主要介紹了Android實現(xiàn)捕獲未知異常并提交給服務(wù)器的方法,涉及Android的異常與錯誤處理機(jī)制相關(guān)操作技巧,需要的朋友可以參考下2016-08-08
Android?TextView的maxEms和maxLength屬性區(qū)別
這篇文章主要為大家介紹了Android?TextView的maxEms和maxLength屬性區(qū)別,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Android連接MySQL數(shù)據(jù)庫實現(xiàn)方法詳解
這篇文章主要介紹了Android連接MySQL數(shù)據(jù)庫實現(xiàn)方法,在Android應(yīng)用程序中連接MySQL數(shù)據(jù)庫可以幫助開發(fā)人員實現(xiàn)更豐富的數(shù)據(jù)管理功能,而且在Android中操作數(shù)據(jù)庫真的太智能了,需要的朋友可以參考下2024-02-02
比較完整的android MP3 LRC歌詞滾動高亮顯示(附源碼)
比較完整的android MP3 LRC歌詞滾動高亮顯示(附源碼)。需要的朋友可以過來參考下,希望對大家有所幫助2013-11-11
Java程序員轉(zhuǎn)Android開發(fā)必讀經(jīng)驗一份
小編最近幾日偷偷的發(fā)現(xiàn)部分Java程序員想轉(zhuǎn)安卓開發(fā),故此加緊補(bǔ)充知識,為大家搜集資料,積極整理前人的經(jīng)驗,希望可以給正處于困惑中的你,帶來些許的幫助。2017-11-11
Android 關(guān)閉多個Activity的實現(xiàn)方法
這篇文章主要介紹了Android 關(guān)閉多個Activity的實現(xiàn)方法的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09

