Android ZXing整合二維碼---簡易
阿新 • • 發佈:2018-12-07
1.匯入依賴
implementation 'cn.bingoogolapple:bga-qrcode-zxing:1.2.5'
2.寫佈局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" tools:context=".MainActivity"> <ImageView android:id="@+id/myimg" android:layout_width="250dp" android:layout_gravity="center" android:layout_height="250dp" /> </LinearLayout>
3.看程式碼
public class MainActivity extends AppCompatActivity { private ImageView img; Handler handler = new Handler(Looper.getMainLooper()){ @Override public void handleMessage(Message msg) { Bitmap v = (Bitmap) msg.obj; //強轉 // 將整合後的二維碼設定到ImageView上進行展示 img.setImageBitmap(v); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); img = findViewById(R.id.myimg); // 整合的程式碼必須在子執行緒裡 new Thread(new Runnable() { @Override public void run() { // 第一個也可以是圖片的網址展示圖片,也可以是網址進行跳轉,如果是圖片的話size大一些就可以了 Bitmap bitmap = QRCodeEncoder.syncEncodeQRCode("這是掃描後展示的文字", 50); Message message = handler.obtainMessage(); message.obj = bitmap; handler.sendMessage(message);// 傳送Handler } }).start(); } }
注:文字也可以是圖片網址,把後面的size改大一些就可以了(例 80)
// 簡單實現不喜勿噴