FX100 & FX200ΒΆ
Step 1: Import the zxing library
This library will allow you scan different formats (qrcode, barcode). The list of supported formats can be found at this GitHub link.
To import a library into your project, add it to the Dependencies of the project. To do this, add the following to your build.gradle file:
repositories {
jcenter()
}
dependencies {
implementation 'com.journeyapps:zxing-android-embedded:3.4.0'
implementation 'com.android.support:appcompat-v7:23.1.0' // Version 23+ is required
}
android {
buildToolsVersion '23.0.2' // Older versions may give compile errors
}
Step 2: Add the necessary permissions
For the application to scan codes, the camera must be enabled.
Open the manifest file and add the following:
<uses-permission android:name="android.permission.CAMERA" />
Step 3: Create the view
Here we will add a simple button to launch the scan and two text fields to display the results (format detected and content).
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button android:id="@+id/scan_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Scan" />
<TextView
android:id="@+id/result_format"
android:layout_below="@id/scan_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
/>
<TextView
android:id="@+id/result_content"
android:layout_below="@id/result_format"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
Step 4: Put into action
This last step links the previous ones together
- Inject the view elements into the code
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scanButton = (Button) findViewById(R.id.scan_button);
result_format = (TextView) findViewById(R.id.result_format);
result_content = (TextView) findViewById(R.id.result_content);
}
- Define the button click action
scanButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new IntentIntegrator(MainActivity.this).initiateScan();
}
});
- The result of this action is retrieved and displayed in the two dedicated text fields
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanningResult != null) {
result_format.setText("FORMAT: " + scanningResult.getFormatName());
result_content.setText("CONTENT: " + scanningResult.getContents());
}
else{
Toast.makeText(getApplicationContext(), "No data received", Toast.LENGTH_SHORT).show();
}
}