這篇文章給大家介紹如何在Android中實現(xiàn)掃描和生成二維碼,內(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' } //加這行代碼
}
}關(guān)于如何在Android中實現(xiàn)掃描和生成二維碼就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
當(dāng)前文章:如何在Android中實現(xiàn)掃描和生成二維碼-創(chuàng)新互聯(lián)
文章網(wǎng)址:http://chinadenli.net/article8/gdsip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、全網(wǎng)營銷推廣、品牌網(wǎng)站制作、定制網(wǎng)站、動態(tài)網(wǎng)站、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容