怎么在Android中實(shí)現(xiàn)掃描和生成二維碼-創(chuàng)新互聯(lián)

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

創(chuàng)新互聯(lián)公司長(zhǎng)期為近1000家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為平果企業(yè)提供專業(yè)的網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作,平果網(wǎng)站改版等技術(shù)服務(wù)。擁有十多年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。

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() { //點(diǎn)擊按鈕掃描二維碼
   @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中實(shí)現(xiàn)掃描和生成二維碼”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“怎么在Android中實(shí)現(xiàn)掃描和生成二維碼”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

標(biāo)題名稱:怎么在Android中實(shí)現(xiàn)掃描和生成二維碼-創(chuàng)新互聯(lián)
分享網(wǎng)址:http://bm7419.com/article46/ceoghg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)定制網(wǎng)站、網(wǎng)站改版靜態(tài)網(wǎng)站、網(wǎng)頁(yè)設(shè)計(jì)公司、ChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司