Skip to content

Commit e6b8c70

Browse files
feat : add code for barcode scanner
1 parent 4fcf8b0 commit e6b8c70

File tree

23 files changed

+123
-57
lines changed

23 files changed

+123
-57
lines changed
0 Bytes
Binary file not shown.

app/src/main/AndroidManifest.xml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
android:allowBackup="true"
77
android:icon="@mipmap/ic_launcher"
88
android:label="@string/app_name"
9-
android:roundIcon="@mipmap/ic_launcher_round"
109
android:supportsRtl="true"
1110
android:theme="@style/AppTheme">
1211
<meta-data
@@ -21,7 +20,7 @@
2120
android:value="text" />
2221
</activity>
2322
<activity
24-
android:name=".adapter.ImageLabelActivity"
23+
android:name=".activity.ImageLabelActivity"
2524
android:theme="@style/AppThemeNoActionbar">
2625
<meta-data
2726
android:name="com.google.firebase.ml.vision.DEPENDENCIES"
@@ -40,5 +39,12 @@
4039
<category android:name="android.intent.category.LAUNCHER" />
4140
</intent-filter>
4241
</activity>
42+
<activity
43+
android:name=".activity.BarCodeReaderActivity"
44+
android:theme="@style/AppThemeNoActionbar">
45+
<meta-data
46+
android:name="com.google.firebase.ml.vision.DEPENDENCIES"
47+
android:value="barcode" />
48+
</activity>
4349
</application>
4450
</manifest>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package io.github.the_dagger.mlkit.activity
2+
3+
import android.graphics.Bitmap
4+
import android.os.Bundle
5+
import android.view.View
6+
7+
import io.github.the_dagger.mlkit.R
8+
import com.google.firebase.ml.vision.FirebaseVision
9+
import com.google.firebase.ml.vision.barcode.FirebaseVisionBarcodeDetector
10+
import kotlinx.android.synthetic.main.activity_main.*
11+
import android.R.attr.bitmap
12+
import android.support.design.widget.BottomSheetBehavior
13+
import android.util.Log
14+
import android.widget.Toast
15+
import com.google.firebase.ml.vision.barcode.FirebaseVisionBarcode
16+
import com.google.firebase.ml.vision.common.FirebaseVisionImage
17+
import com.google.firebase.ml.vision.barcode.FirebaseVisionBarcodeDetectorOptions
18+
import io.github.the_dagger.mlkit.R.id.*
19+
import kotlinx.android.synthetic.main.layout_qr_code_reader.*
20+
21+
22+
class BarCodeReaderActivity : BaseCameraActivity() {
23+
24+
override fun onCreate(savedInstanceState: Bundle?) {
25+
super.onCreate(savedInstanceState)
26+
setupBottomSheet(R.layout.layout_qr_code_reader)
27+
}
28+
29+
override fun onClick(v: View) {
30+
fabProgressCircle.show()
31+
cameraView.captureImage { cameraKitImage ->
32+
// Get the Bitmap from the captured shot
33+
getQRCodeDetails(cameraKitImage.bitmap)
34+
runOnUiThread {
35+
showPreview()
36+
imagePreview.setImageBitmap(cameraKitImage.bitmap)
37+
}
38+
}
39+
}
40+
41+
private fun getQRCodeDetails(bitmap: Bitmap) {
42+
val options = FirebaseVisionBarcodeDetectorOptions.Builder()
43+
.setBarcodeFormats(
44+
FirebaseVisionBarcode.FORMAT_ALL_FORMATS)
45+
.build()
46+
val detector = FirebaseVision.getInstance().getVisionBarcodeDetector(options);
47+
val image = FirebaseVisionImage.fromBitmap(bitmap)
48+
detector.detectInImage(image)
49+
.addOnSuccessListener {
50+
for (firebaseBarcode in it) {
51+
52+
codeData.text = firebaseBarcode.displayValue //Display contents inside the barcode
53+
sheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
54+
55+
when (firebaseBarcode.valueType) {
56+
//Handle the URL here
57+
FirebaseVisionBarcode.TYPE_URL -> firebaseBarcode.url
58+
// Handle the contact info here, i.e. address, name, phone, etc.
59+
FirebaseVisionBarcode.TYPE_CONTACT_INFO -> firebaseBarcode.contactInfo
60+
// Handle the wifi here, i.e. firebaseBarcode.wifi.ssid, etc.
61+
FirebaseVisionBarcode.TYPE_WIFI -> firebaseBarcode.wifi
62+
//Handle more type of Barcodes
63+
}
64+
65+
}
66+
}
67+
.addOnFailureListener {
68+
it.printStackTrace()
69+
Toast.makeText(baseContext, "Sorry, something went wrong!", Toast.LENGTH_SHORT).show()
70+
}
71+
.addOnCompleteListener {
72+
fabProgressCircle.hide()
73+
}
74+
}
75+
76+
}

app/src/main/java/io/github/the_dagger/mlkit/activity/BaseCameraActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ abstract class BaseCameraActivity : AppCompatActivity(), View.OnClickListener {
3232
lparam.behavior = BottomSheetBehavior<View>()
3333
inflatedView.layoutParams = lparam
3434
sheetBehavior = BottomSheetBehavior.from(inflatedView)
35-
sheetBehavior.peekHeight = 64
35+
sheetBehavior.peekHeight = 224
3636
//Anchor the FAB to the end of inflated bottom sheet
3737
val lp = fabProgressCircle.layoutParams as CoordinatorLayout.LayoutParams
3838
lp.anchorId = inflatedView.id

app/src/main/java/io/github/the_dagger/mlkit/activity/HomeActivity.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ class HomeActivity : AppCompatActivity() {
1919
setContentView(R.layout.activity_home)
2020
apiList.add(PojoApi(R.drawable.image_labelling, getString(R.string.title_labelling), getString(R.string.desc_labelling), 0))
2121
apiList.add(PojoApi(R.drawable.text_recognition, getString(R.string.title_text), getString(R.string.desc_text), 1))
22-
apiList.add(PojoApi(R.drawable.face_detection, getString(R.string.title_face), getString(R.string.desc_face), 2))
23-
apiList.add(PojoApi(R.drawable.barcode_scanning, getString(R.string.title_barcode), getString(R.string.desc_barcode), 3))
22+
apiList.add(PojoApi(R.drawable.barcode_scanning, getString(R.string.title_barcode), getString(R.string.desc_barcode), 2))
23+
apiList.add(PojoApi(R.drawable.face_detection, getString(R.string.title_face), getString(R.string.desc_face), 3))
2424
apiList.add(PojoApi(R.drawable.landmark_identification, getString(R.string.title_landmark), getString(R.string.desc_landmark), 4))
2525

2626
rvHome.layoutManager = LinearLayoutManager(this)

app/src/main/java/io/github/the_dagger/mlkit/adapter/ImageLabelActivity.kt renamed to app/src/main/java/io/github/the_dagger/mlkit/activity/ImageLabelActivity.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.the_dagger.mlkit.adapter
1+
package io.github.the_dagger.mlkit.activity
22

33
import android.graphics.Bitmap
44
import android.os.Bundle
@@ -9,7 +9,7 @@ import android.widget.Toast
99
import com.google.firebase.ml.vision.FirebaseVision
1010
import com.google.firebase.ml.vision.common.FirebaseVisionImage
1111
import io.github.the_dagger.mlkit.R
12-
import io.github.the_dagger.mlkit.activity.BaseCameraActivity
12+
import io.github.the_dagger.mlkit.adapter.ImageLabelAdapter
1313
import kotlinx.android.synthetic.main.activity_main.*
1414
import kotlinx.android.synthetic.main.layout_image_label.*
1515

app/src/main/java/io/github/the_dagger/mlkit/adapter/HomeAdapter.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import android.view.ViewGroup
99
import android.widget.Toast
1010
import io.github.the_dagger.mlkit.PojoApi
1111
import io.github.the_dagger.mlkit.R
12+
import io.github.the_dagger.mlkit.activity.BarCodeReaderActivity
1213
import io.github.the_dagger.mlkit.activity.CardScannerActivity
1314
import io.github.the_dagger.mlkit.activity.FaceDetectionActivity
15+
import io.github.the_dagger.mlkit.activity.ImageLabelActivity
1416
import kotlinx.android.synthetic.main.item_row_home.view.*
1517

1618
class HomeAdapter(private val apiList: List<PojoApi>) : RecyclerView.Adapter<HomeAdapter.HomeHolder>() {
@@ -29,7 +31,7 @@ class HomeAdapter(private val apiList: List<PojoApi>) : RecyclerView.Adapter<Hom
2931
when(currItem.id){
3032
0 -> context.startActivity(Intent(context, ImageLabelActivity::class.java))
3133
1 -> context.startActivity(Intent(context, CardScannerActivity::class.java))
32-
2 -> context.startActivity(Intent(context, FaceDetectionActivity::class.java))
34+
2 -> context.startActivity(Intent(context, BarCodeReaderActivity::class.java))
3335
3 -> Toast.makeText(context,"Work in Progress",Toast.LENGTH_SHORT).show()
3436
4 -> Toast.makeText(context,"Work in Progress",Toast.LENGTH_SHORT).show()
3537
}

app/src/main/res/drawable-v24/ic_launcher_foreground.xml

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:app="http://schemas.android.com/apk/res-auto"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:layout_width="match_parent"
5+
android:layout_height="wrap_content"
6+
android:background="#ffffff"
7+
android:orientation="vertical"
8+
android:padding="16dp"
9+
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
10+
11+
<TextView
12+
android:layout_width="match_parent"
13+
android:layout_height="wrap_content"
14+
android:text="QR Code Data"
15+
android:textSize="24sp"
16+
android:textStyle="bold" />
17+
18+
<TextView
19+
android:id="@+id/codeData"
20+
android:autoLink="all"
21+
android:layout_width="match_parent"
22+
android:layout_height="wrap_content"
23+
android:textSize="20sp"
24+
tools:text="Card Number" />
25+
26+
</LinearLayout>

app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)