A.Add ZXING LiabaryFor Scanning an Generating QR Code.
compile 'com.google.zxing:core:3.2.1' compile 'com.journeyapps:zxing-android-embedded:3.2.0@aar'
B. Generate QR Code in Android
Step 1 : Select File -> New -> Project -> Android Application Project create a new project .
Step 2 : Open res -> layout -> activity_generate.xml (or) activity_generate.xml and add following code :
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_generate" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/colorPrimary" tools:context="com.example.spider.qrcode.GenerateActivity"> <LinearLayout android:layout_width="match_parent" android:background="#ddd" android:layout_margin="35dp" android:orientation="vertical" android:layout_height="match_parent"> <TextView android:text="QR Code Generator" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="25dp" android:textSize="22dp" android:layout_gravity="center" android:id="@+id/textView" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> <LinearLayout android:layout_width="match_parent" android:orientation="horizontal" android:layout_gravity="center" android:layout_marginTop="35dp" android:padding="10dp" android:layout_height="wrap_content"> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textPersonName" android:hint="Enter The String" android:ems="10" android:id="@+id/generate_editText" /> <Button android:text="Generate" android:layout_width="wrap_content" android:layout_height="40dp" android:textColor="#fff" android:background="@color/colorPrimary" android:id="@+id/button" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:gravity="center" android:layout_height="match_parent"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" app:srcCompat="@mipmap/ic_launcher" android:id="@+id/imageView" /> </LinearLayout> </LinearLayout> </LinearLayout>
Step 3 : Open src -> package -> Generate_Activity.java and add following code :
public class GenerateActivity extends AppCompatActivity { ImageView qrcodeImageview; String QRcode; public final static int WIDTH=300; EditText generate_edittxt; ImageButton backbutton; Button generate_btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_generate); generate_edittxt = (EditText) findViewById(R.id.generate_editText); generate_btn = (Button) findViewById(R.id.button); backbutton=(ImageButton)findViewById(R.id.backbutton); backbutton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(GenerateActivity.this, ReaderActivity.class); startActivity(intent); } }); qrcodeImageview = (ImageView) findViewById(R.id.imageView); generate_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(generate_edittxt.getText().toString().length()==0) { Toast.makeText(GenerateActivity.this, "Please Enter The String", Toast.LENGTH_SHORT).show(); } else { Thread t = new Thread(new Runnable() { public void run() { // this is the msg which will be encode in QRcode QRcode=generate_edittxt.getText().toString(); try { synchronized (this) { wait(1000); // runOnUiThread method used to do UI task in main thread. runOnUiThread(new Runnable() { @Override public void run() { try { Bitmap bitmap = null; bitmap = encodeAsBitmap(QRcode); qrcodeImageview.setImageBitmap(bitmap); } catch (WriterException e) { e.printStackTrace(); } // end of catch block } // end of run method }); } } catch (InterruptedException e) { e.printStackTrace(); } } }); t.start(); } } }); } /*private void getID() { qrCodeImageview=(ImageView) findViewById(R.id.img_qr_code_image); } */ Bitmap encodeAsBitmap(String str) throws WriterException { BitMatrix result; try { result = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, WIDTH, WIDTH, null); } catch (IllegalArgumentException iae) { // Unsupported format return null; } int w = result.getWidth(); int h = result.getHeight(); int[] pixels = new int[w * h]; for (int y = 0; y < h; y++) { int offset = y * w; for (int x = 0; x < w; x++) { pixels[offset + x] = result.get(x, y) ? getResources().getColor(R.color.colorAccent):getResources().getColor(R.color.colorPrimary); } } Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, 300, 0, 0, w, h); return bitmap; } }
Step 4 : Open AndroidManifest.xml and add following code :
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.spider.qrcode"> <uses-permission android:name="android.permission.CAMERA" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".ReaderActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".GenerateActivity"></activity> </application> </manifest>
Step 4: Our output will be like this :
C. Scan or Read QR Code in Android
Step 1 : Open res -> layout -> activity_reader.xml add following code :
<?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:id="@+id/activity_reader" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@color/colorPrimary" tools:context="com.example.spider.qrcode.ReaderActivity"> <LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:layout_margin="35dp" android:background="#ddd" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="QR Code Generator An Scanner" android:textSize="20dp" android:layout_marginTop="50dp" android:textStyle="bold" android:layout_gravity="center"/> <Button android:layout_width="175dp" android:layout_height="40dp" android:text="Generate" android:layout_gravity="center" android:id="@+id/generate_btnn" android:gravity="center" android:textColor="#fff" android:textSize="18dp" android:textStyle="bold" android:background="@color/colorPrimary" android:layout_marginTop="85dp" /> <Button android:layout_width="175dp" android:layout_height="40dp" android:text="SCAN" android:textSize="18dp" android:layout_gravity="center" android:id="@+id/scan_btn" android:gravity="center" android:textColor="#fff" android:textStyle="bold" android:background="@color/colorPrimary" android:layout_marginTop="45dp" /> </LinearLayout> </LinearLayout> </LinearLayout>
Step 2 : Open src -> package -> ReaderActivity.java and add following code :
public class ReaderActivity extends AppCompatActivity { private Button scan_btn; private Button generate_btnn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_reader); scan_btn = (Button) findViewById(R.id.scan_btn); generate_btnn=(Button)findViewById(R.id.generate_btnn); final Activity activity = this; generate_btnn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(ReaderActivity.this,GenerateActivity.class); startActivity(intent); } }); scan_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { IntentIntegrator intentIntegrator = new IntentIntegrator(activity); intentIntegrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES); intentIntegrator.setPrompt("SCAN"); intentIntegrator.setCameraId(0); intentIntegrator.setBeepEnabled(false); intentIntegrator.setBarcodeImageEnabled(false); intentIntegrator.initiateScan(); } }); } public void onActivityResult(int requestCode, int resultCode, Intent data) { IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data); if (result != null) { if (result.getContents() == null) { Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show(); } else { Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show(); System.out.println("SCANNED"+result.getContents()); } } else { // This is important, otherwise the result will not be passed to the fragment super.onActivityResult(requestCode, resultCode, data); } } }
One thought on “In this tutorial we learn how to implement QR Code Scanner & Generator using ZXing Scanner & Generator library in Android Application.”