這篇文章主要介紹了怎么在Android中實現(xiàn)掃描和生成二維碼的相關(guān)知識,內(nèi)容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇怎么在Android中實現(xiàn)掃描和生成二維碼文章都會有所收獲,下面我們一起來看看吧。

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' } //加這行代碼
}
}關(guān)于“怎么在Android中實現(xiàn)掃描和生成二維碼”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“怎么在Android中實現(xiàn)掃描和生成二維碼”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
標(biāo)題名稱:怎么在Android中實現(xiàn)掃描和生成二維碼-創(chuàng)新互聯(lián)
分享網(wǎng)址:http://www.jbt999.com/article46/ceoghg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、定制網(wǎng)站、網(wǎng)站改版、靜態(tài)網(wǎng)站、網(wǎng)頁設(shè)計公司、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:[email protected]。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)