02 Aug, 2018
Categories: Android
Create Project
Create new project in Android Studio with steps as below:
Step 1: Input Project Name and Select Project Location
Step 2: Select SDK for Android App
Step 3: Select Default Activity for App
Step 4: Finish create project
Add Strings
Open res\values\strings.xml file and add new string as below:
<resources>
<string name="app_name">Learn Android with Real Apps</string>
<string name="product_id">Enter Product Id</string>
<string name="generate">Generate</string>
<string name="scan">Scan</string>
</resources>
Add Library as Dependency
Open build.gradle file in Gradle Scripts and add new library as below:
implementation 'com.google.zxing:core:3.2.1'
implementation 'com.journeyapps:zxing-android-embedded:3.2.0@aar'
Main Activity Layout
Open res\layout\activity_main.xml file and create layout as below:
<?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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="vertical"
tools:ignore="HardcodedText">
<EditText
android:id="@+id/editTextProductId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/product_id"
android:inputType="textPersonName" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/buttonGenerate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/generate" />
<Button
android:id="@+id/buttonScan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/scan" />
</LinearLayout>
<ImageView
android:id="@+id/imageViewResult"
android:layout_width="match_parent"
android:layout_height="335dp" />
</LinearLayout>
Main Activity Class
Add code to MainActivity.java file in android.demo.learnandroidwithrealapps package as below:
package android.demo.learnandroidwithrealapps;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.journeyapps.barcodescanner.BarcodeEncoder;
public class MainActivity extends AppCompatActivity {
private EditText editTextProductId;
private Button buttonGenerate, buttonScan;
private ImageView imageViewResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
editTextProductId = findViewById(R.id.editTextProductId);
imageViewResult = findViewById(R.id.imageViewResult);
buttonGenerate = findViewById(R.id.buttonGenerate);
buttonGenerate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
buttonGenerate_onClick(view);
}
});
buttonScan = findViewById(R.id.buttonScan);
buttonScan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
buttonScan_onClick(view);
}
});
}
private void buttonGenerate_onClick(View view) {
try {
String productId = editTextProductId.getText().toString();
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
BitMatrix bitMatrix = multiFormatWriter.encode(productId, BarcodeFormat.QR_CODE, 400, 400);
BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
imageViewResult.setImageBitmap(bitmap);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}
private void buttonScan_onClick(View view) {
IntentIntegrator intentIntegrator = new IntentIntegrator(this);
intentIntegrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
intentIntegrator.setCameraId(0);
intentIntegrator.initiateScan();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(intentResult != null) {
String productId = intentResult.getContents();
Toast.makeText(getApplicationContext(), productId, Toast.LENGTH_LONG).show();
}
}
}
Structure of Project
Run App
Load Main Activity
Create QRCode
Scan QRCode