我是Android Studio的新手。如何模拟这个简单的二维码扫描器?
图书馆
compile 'com.journeyapps:zxing-android-embedded:3.4.0'
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//View Objects
private Button buttonScan;
private TextView textViewName, textViewAddress;
//qr code scanner object
private IntentIntegrator qrScan;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//View objects
buttonScan = (Button) findViewById(R.id.buttonScan);
textViewName = (TextView) findViewById(R.id.textViewName);
textViewAddress = (TextView) findViewById(R.id.textViewAddress);
//intializing scan object
qrScan = new IntentIntegrator(this);
//attaching onclick listener
buttonScan.setOnClickListener(this);
}
//Getting the scan results
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
//if qrcode has nothing in it
if (result.getContents() == null) {
Toast.makeText(this, "Result Not Found", Toast.LENGTH_LONG).show();
} else {
//if qr contains data
try {
//converting the data to json
JSONObject obj = new JSONObject(result.getContents());
//setting values to textviews
textViewName.setText(obj.getString("name"));
textViewAddress.setText(obj.getString("address"));
} catch (JSONException e) {
e.printStackTrace();
//if control comes here
//that means the encoded format not matches
//in this case you can display whatever data is available on the qrcode
//to a toast
Toast.makeText(this, result.getContents(), Toast.LENGTH_LONG).show();
}
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
@Override
public void onClick(View view) {
//initiating the qr code scan
qrScan.initiateScan();
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name" />
<TextView
android:id="@+id/textViewName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Address" />
<TextView
android:id="@+id/textViewAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" />
</LinearLayout>
<Button
android:id="@+id/buttonScan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Scan QR Code" />
</RelativeLayout>
我已经从AndroidManifest.xml
插入activity
了
android:screenOrientation="portrait"
和android:screenOrientation="userPortrait"
。
其余的,我无法解决问题。