簡單的二維碼實現!
單機監聽
@Override public void onClick(View v) { switch (v.getId()) {
//生成事件 case R.id.erweima: String erweimashuruString = mErweimashuru.getText().toString().trim(); if (TextUtils.isEmpty(erweimashuruString)) { Toast.makeText(this, "請輸入要生成的內容", Toast.LENGTH_SHORT).show(); return; } /** * 生成不帶logo的二維碼圖片 */ mErweimashuru.setText(""); mBitmap = CodeUtils.createImage(erweimashuruString, 400, 400, null); mImageView.setImageBitmap(mBitmap); break; case R.id.erweimaimage: String erweimashuruString1 = mErweimashuru.getText().toString().trim(); if (TextUtils.isEmpty(erweimashuruString1)) { Toast.makeText(this, "請輸入要生成的內容", Toast.LENGTH_SHORT).show(); } mErweimashuru.setText(""); mbitmap = CodeUtils.createImage(erweimashuruString1, 400, 400, BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)); mImageView.setImageBitmap(mbitmap); break;
//這裡是掃描 case R.id.saomiao: Intent intent = new Intent(ChenggongActivity.this, CaptureActivity.class); startActivityForResult(intent, 1); break; } }
//這裡是回撥方法 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); /** * 處理二維碼掃描結果 */ if (requestCode == 1) { //處理掃描結果(在介面上顯示) if (null != data) { Bundle bundle = data.getExtras(); if (bundle == null) { return; }
if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) { String result = bundle.getString(CodeUtils.RESULT_STRING); Toast.makeText(this, “解析結果:” + result, Toast.LENGTH_LONG).show(); } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) { Toast.makeText(ChenggongActivity.this, “解析二維碼失敗”, Toast.LENGTH_LONG).show(); }
} } }
自定義類繼承呼叫
public class Zxing extends Application { @Override public void onCreate() { super.onCreate(); ZXingLibrary.initDisplayOpinion(this); } }
清單檔案許可權
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
新增name
<application
android:name=".zxing.Zxing"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
依賴檔案
implementation ‘cn.yipianfengye.android:zxing-library:2.2’