檢查是否有安裝 Barcode Scanner 方式
if (getPackageManager().queryIntentActivities(new Intent( "com.google.zxing.client.android.SCAN"), PackageManager.MATCH_DEFAULT_ONLY).size() == 0) { //表示未安裝 }
解碼方式
//2D 解碼 Intent intent = new Intent("com.google.zxing.client.android.SCAN"); intent.putExtra("SCAN_MODE", "QR_CODE_MODE"); startActivityForResult(intent, 0); //1D 解碼 Intent intent = new Intent("com.google.zxing.client.android.SCAN"); intent.putExtra("SCAN_MODE", "PRODUCT_MODE"); startActivityForResult(intent, 0); //傳回結果 public void onActivityResult(int requestCode, int resultCode, Intent intent) { if (requestCode == 0) { if (resultCode == RESULT_OK) {//掃描成功 String contents = intent.getStringExtra("SCAN_RESULT"); String format = intent.getStringExtra("SCAN_RESULT_FORMAT"); Toast.makeText(this, "Content:" + contents + " Format:" + format, Toast.LENGTH_LONG).show(); } else if (resultCode == RESULT_CANCELED) {// Handle cancel Toast.makeText(this, "取消掃描!", Toast.LENGTH_LONG).show(); } } }
編碼方式
//TEXT, EMAIL, PHONE, SMS 格式 Intent i = new Intent("com.google.zxing.client.android.ENCODE"); i.putExtra("ENCODE_TYPE", "TEXT_TYPE");//EMAIL_TYPE,PHONE_TYPE,SMS_TYPE i.putExtra("ENCODE_DATA", "編碼資料內容" ); startActivity(i); //聯絡人格式 Intent i = new Intent(""); Bundle b = new Bundle("com.google.zxing.client.android.ENCODE"); b.putString(Contacts.Intents.Insert.NAME, "Shioulo"); b.putString(Contacts.Intents.Insert.PHONE, "1234567"); b.putString(Contacts.Intents.Insert.EMAIL, "shioulo@barcode.com"); b.putString(Contacts.Intents.Insert.POSTAL, "123 Google St. Android"); b.putString(Contacts.Intents.Insert.NOTES, "ps data..."); i.putExtra(Intents.Encode.TYPE, CONTACT); i.putExtra(Intents.Encode.DATA, bundle); startActivity(i);