In this tutorial we will learn how to implement a barcode reader application in android. Barcode reader is a device which is used to scan barcodes in products and display information about the product. Here in this application we will learn how to create a barcode reader which is able to scan barcodes in products using an android application using zxing library.
Steps
- Create project in android studio
- Configure gradle file by adding necessary libraries.
- Implement the layout file
- Develop code for scanning barcode and displaying product information
Create project in android studio
Refer Android beginners app development guide if you are beginner or if you don’t know how to create project in android studio.
Configure gradle file
Add the following repository above dependencies
repositories { jcenter() }
And compile the following line inside the dependencies
compile 'com.journeyapps:zxing-android-embedded:3.2.0@aar' compile 'com.google.zxing:core:3.2.1'
Implement the layout file
In this step we need to add the following code to your layout file. You can find the layout file under res/layout if an activity is automatically created. If not you can create and activity by yourself. Following is the code for layout file which will be having a simple button for opening barcode scanner.
<RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/btn_scan" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_marginTop="20dp" android:background="@color/colorAccent" android:text="Click here to Scan BarCode" android:textColor="#FFFFFF" /> </RelativeLayout>
Developing code for scanning barcode
Now you just need to add the following code to your activity’s java file . Note the in this example after scanning the barcode app will display the result as Toast message. You can set it to textview or any other widget based on your requirement.
import com.google.zxing.integration.android.IntentIntegrator; import com.google.zxing.integration.android.IntentResult; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Button; import android.widget.Toast; import android.content.Intent; import android.view.View; public class MainActivity extends AppCompatActivity { Button btn_scan; @Override protected void onCreate( Bundle savedInstanceState ) { super.onCreate( savedInstanceState ); setContentView(R.layout.activity_main ); btn_scan = (Button) findViewById(R.id.btn_scan); btn_scan.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { IntentIntegrator scanIntegrator = new IntentIntegrator(MainActivity.this); scanIntegrator.initiateScan(); } }); } @Override protected void onActivityResult ( int requestCode, int resultCode, Intent in ) { IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, in); if (scanningResult != null) { String contents = in.getStringExtra( "SCAN_RESULT" ); String format = in.getStringExtra( "SCAN_RESULT_FORMAT" ) ; Toast.makeText(MainActivity.this, "Content-" + contents + " Format-" + format, Toast.LENGTH_LONG).show(); } } }
Note that you need to add the following permission in AndroidManifest.xml file
<uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" android:required="true" />
And the following meta data under your application tag in AndroidManifest.xml file
<meta-data android:name="com.google.android.gms.vision.DEPENDENCIES" android:value="barcode" />
Output
works but getting a suggestion form the IDE to use super.onActivityResult ?
Did a little research but no luck yet.
Thanks!! Yours is the first one that works with the recent Android Studio 3.5!
how to set it in portrait orientation