Skip to content

Commit 5f863c0

Browse files
frat : add implementation for card scanner
1 parent a29087f commit 5f863c0

File tree

12 files changed

+255
-111
lines changed

12 files changed

+255
-111
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,36 @@
99
android:roundIcon="@mipmap/ic_launcher_round"
1010
android:supportsRtl="true"
1111
android:theme="@style/AppTheme">
12-
<meta-data
13-
android:name="com.google.firebase.ml.vision.DEPENDENCIES"
14-
android:value="label" />
1512
<meta-data
1613
android:name="preloaded_fonts"
1714
android:resource="@array/preloaded_fonts" />
15+
1816
<activity
19-
android:name=".ImageLabelActivity"
20-
android:theme="@style/AppThemeNoActionbar"/>
21-
<activity android:name=".HomeActivity">
17+
android:name=".activity.CardScannerActivity"
18+
android:theme="@style/AppThemeNoActionbar">
19+
<meta-data
20+
android:name="com.google.firebase.ml.vision.DEPENDENCIES"
21+
android:value="text" />
22+
</activity>
23+
<activity
24+
android:name=".adapter.ImageLabelActivity"
25+
android:theme="@style/AppThemeNoActionbar">
26+
<meta-data
27+
android:name="com.google.firebase.ml.vision.DEPENDENCIES"
28+
android:value="label" />
29+
</activity>
30+
<activity
31+
android:name=".activity.FaceDetectionActivity"
32+
android:theme="@style/AppThemeNoActionbar">
33+
<meta-data
34+
android:name="com.google.firebase.ml.vision.DEPENDENCIES"
35+
android:value="face" />
36+
</activity>
37+
<activity android:name=".activity.HomeActivity">
2238
<intent-filter>
2339
<action android:name="android.intent.action.MAIN" />
24-
2540
<category android:name="android.intent.category.LAUNCHER" />
2641
</intent-filter>
2742
</activity>
2843
</application>
29-
3044
</manifest>

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

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package io.github.the_dagger.mlkit.activity
2+
3+
import android.os.Bundle
4+
import android.support.annotation.LayoutRes
5+
import android.support.design.widget.BottomSheetBehavior
6+
import android.support.design.widget.CoordinatorLayout
7+
import android.support.v7.app.AppCompatActivity
8+
import android.view.Gravity
9+
import android.view.View
10+
import io.github.the_dagger.mlkit.R
11+
import kotlinx.android.synthetic.main.activity_main.*
12+
13+
abstract class BaseCameraActivity : AppCompatActivity(), View.OnClickListener {
14+
15+
lateinit var sheetBehavior: BottomSheetBehavior<*>
16+
17+
override fun onCreate(savedInstanceState: Bundle?) {
18+
super.onCreate(savedInstanceState)
19+
setContentView(R.layout.activity_main)
20+
btnRetry.setOnClickListener {
21+
if (cameraView.visibility == View.VISIBLE) showPreview() else hidePreview()
22+
}
23+
fab_take_photo.setOnClickListener(this)
24+
}
25+
26+
fun setupBottomSheet(@LayoutRes id : Int){
27+
//Using a ViewStub since changing the layout of an <include> tag dynamically wasn't possible
28+
stubView.layoutResource = id
29+
val inflatedView = stubView.inflate()
30+
//Set layout parameters for the inflated bottomsheet
31+
val lparam = inflatedView.layoutParams as CoordinatorLayout.LayoutParams
32+
lparam.behavior = BottomSheetBehavior<View>()
33+
inflatedView.layoutParams = lparam
34+
sheetBehavior = BottomSheetBehavior.from(inflatedView)
35+
sheetBehavior.peekHeight = 64
36+
//Anchor the FAB to the end of inflated bottom sheet
37+
val lp = fabProgressCircle.layoutParams as CoordinatorLayout.LayoutParams
38+
lp.anchorId = inflatedView.id
39+
lp.anchorGravity = Gravity.END
40+
fabProgressCircle.layoutParams = lp
41+
//Hide the fab as bottomSheet is expanded
42+
sheetBehavior.setBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
43+
override fun onStateChanged(bottomSheet: View, newState: Int) {}
44+
override fun onSlide(bottomSheet: View, slideOffset: Float) {
45+
fab_take_photo.animate().scaleX(1 - slideOffset).scaleY(1 - slideOffset).setDuration(0).start()
46+
}
47+
})
48+
}
49+
50+
override fun onResume() {
51+
super.onResume()
52+
cameraView.start()
53+
}
54+
55+
override fun onPause() {
56+
cameraView.stop()
57+
super.onPause()
58+
}
59+
60+
protected fun showPreview() {
61+
framePreview.visibility = View.VISIBLE
62+
cameraView.visibility = View.GONE
63+
}
64+
65+
protected fun hidePreview() {
66+
framePreview.visibility = View.GONE
67+
cameraView.visibility = View.VISIBLE
68+
}
69+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
import com.google.firebase.ml.vision.FirebaseVision
7+
import com.google.firebase.ml.vision.common.FirebaseVisionImage
8+
import com.google.firebase.ml.vision.face.FirebaseVisionFaceDetector
9+
import com.wonderkiln.camerakit.CameraKit
10+
import io.github.the_dagger.mlkit.activity.BaseCameraActivity
11+
import kotlinx.android.synthetic.main.activity_main.*
12+
13+
class FaceDetectionActivity : BaseCameraActivity(){
14+
15+
override fun onCreate(savedInstanceState: Bundle?) {
16+
super.onCreate(savedInstanceState)
17+
cameraView.facing = CameraKit.Constants.FACING_FRONT
18+
}
19+
20+
private fun getFaceDetails(bitmap: Bitmap) {
21+
// val options : FirebaseVisionFaceDetectorOptions = FirebaseVisionFaceDetectorOptions.Builder().build()
22+
val image : FirebaseVisionImage = FirebaseVisionImage.fromBitmap(bitmap)
23+
val faceDetector : FirebaseVisionFaceDetector = FirebaseVision.getInstance().visionFaceDetector
24+
25+
faceDetector.detectInImage(image)
26+
.addOnSuccessListener {
27+
fabProgressCircle.hide()
28+
for (face in it){
29+
30+
}
31+
}
32+
.addOnFailureListener{
33+
34+
}
35+
}
36+
37+
override fun onClick(v: View?) {
38+
fabProgressCircle.show()
39+
cameraView.captureImage { cameraKitImage ->
40+
// Get the Bitmap from the captured shot
41+
getFaceDetails(cameraKitImage.bitmap)
42+
runOnUiThread {
43+
showPreview()
44+
imagePreview.setImageBitmap(cameraKitImage.bitmap)
45+
}
46+
}
47+
}
48+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package io.github.the_dagger.mlkit.activity
2+
3+
import android.support.v7.app.AppCompatActivity
4+
import android.os.Bundle
5+
import android.support.v7.widget.LinearLayoutManager
6+
import io.github.the_dagger.mlkit.adapter.HomeAdapter
7+
import io.github.the_dagger.mlkit.PojoApi
8+
import io.github.the_dagger.mlkit.R
9+
import kotlinx.android.synthetic.main.activity_home.*
10+
11+
class HomeActivity : AppCompatActivity() {
12+
13+
private val apiList by lazy {
14+
ArrayList<PojoApi>()
15+
}
16+
17+
override fun onCreate(savedInstanceState: Bundle?) {
18+
super.onCreate(savedInstanceState)
19+
setContentView(R.layout.activity_home)
20+
apiList.add(PojoApi(R.drawable.image_labelling, getString(R.string.title_labelling), getString(R.string.desc_labelling), 0))
21+
apiList.add(PojoApi(R.drawable.face_detection, getString(R.string.title_face), getString(R.string.desc_face), 1))
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.text_recognition, getString(R.string.title_text), getString(R.string.desc_text), 3))
24+
apiList.add(PojoApi(R.drawable.landmark_identification, getString(R.string.title_landmark), getString(R.string.desc_landmark), 4))
25+
26+
rvHome.layoutManager = LinearLayoutManager(this)
27+
rvHome.adapter = HomeAdapter(apiList)
28+
}
29+
}

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

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

33
import android.content.Context
44
import android.content.Intent
@@ -7,6 +7,10 @@ import android.view.LayoutInflater
77
import android.view.View
88
import android.view.ViewGroup
99
import android.widget.Toast
10+
import io.github.the_dagger.mlkit.PojoApi
11+
import io.github.the_dagger.mlkit.R
12+
import io.github.the_dagger.mlkit.activity.CardScannerActivity
13+
import io.github.the_dagger.mlkit.activity.FaceDetectionActivity
1014
import kotlinx.android.synthetic.main.item_row_home.view.*
1115

1216
class HomeAdapter(private val apiList: List<PojoApi>) : RecyclerView.Adapter<HomeAdapter.HomeHolder>() {
@@ -23,10 +27,10 @@ class HomeAdapter(private val apiList: List<PojoApi>) : RecyclerView.Adapter<Hom
2327
iViewApi.setImageResource(currItem.imageId)
2428
cViewHome.setOnClickListener {
2529
when(currItem.id){
26-
0 -> Toast.makeText(context,"Work in Progress",Toast.LENGTH_SHORT).show()
27-
1 -> Toast.makeText(context,"Work in Progress",Toast.LENGTH_SHORT).show()
30+
0 -> context.startActivity(Intent(context, ImageLabelActivity::class.java))
31+
1 -> context.startActivity(Intent(context, FaceDetectionActivity::class.java))
2832
2 -> Toast.makeText(context,"Work in Progress",Toast.LENGTH_SHORT).show()
29-
3 -> context.startActivity(Intent(context,ImageLabelActivity::class.java))
33+
3 -> context.startActivity(Intent(context, CardScannerActivity::class.java))
3034
4 -> Toast.makeText(context,"Work in Progress",Toast.LENGTH_SHORT).show()
3135
}
3236
}

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

Lines changed: 18 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,27 @@
1-
package io.github.the_dagger.mlkit
1+
package io.github.the_dagger.mlkit.adapter
22

33
import android.graphics.Bitmap
44
import android.os.Bundle
55
import android.support.design.widget.BottomSheetBehavior
6-
import android.support.v7.app.AppCompatActivity
76
import android.support.v7.widget.LinearLayoutManager
87
import android.view.View
98
import android.widget.Toast
109
import com.google.firebase.ml.vision.FirebaseVision
1110
import com.google.firebase.ml.vision.common.FirebaseVisionImage
11+
import io.github.the_dagger.mlkit.R
12+
import io.github.the_dagger.mlkit.activity.BaseCameraActivity
1213
import kotlinx.android.synthetic.main.activity_main.*
13-
import kotlinx.android.synthetic.main.layout_bottom_sheet.*
14+
import kotlinx.android.synthetic.main.layout_image_label.*
1415

15-
16-
class ImageLabelActivity : AppCompatActivity() {
16+
class ImageLabelActivity : BaseCameraActivity() {
1717

1818
private var itemsList: ArrayList<Any> = ArrayList()
1919
private lateinit var itemAdapter: ImageLabelAdapter
2020

21-
private lateinit var sheetBehavior: BottomSheetBehavior<*>
22-
2321
override fun onCreate(savedInstanceState: Bundle?) {
2422
super.onCreate(savedInstanceState)
25-
setContentView(R.layout.activity_main)
23+
setupBottomSheet(R.layout.layout_image_label)
2624
rvLabel.layoutManager = LinearLayoutManager(this)
27-
btnRetry.setOnClickListener {
28-
if (cameraView.visibility == View.VISIBLE) showPreview() else hidePreview()
29-
}
30-
31-
sheetBehavior = BottomSheetBehavior.from(bottomLayout)
32-
sheetBehavior.setBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
33-
override fun onStateChanged(bottomSheet: View, newState: Int) {}
34-
35-
override fun onSlide(bottomSheet: View, slideOffset: Float) {
36-
fab_take_photo.animate().scaleX(1 - slideOffset).scaleY(1 - slideOffset).setDuration(0).start()
37-
}
38-
})
39-
40-
fab_take_photo.setOnClickListener {
41-
fabProgressCircle.show()
42-
cameraView.captureImage { cameraKitImage ->
43-
// Get the Bitmap from the captured shot
44-
getLabelsFromDevice(cameraKitImage.bitmap)
45-
runOnUiThread {
46-
showPreview()
47-
imagePreview.setImageBitmap(cameraKitImage.bitmap)
48-
}
49-
}
50-
}
5125
}
5226

5327
private fun getLabelsFromDevice(bitmap: Bitmap) {
@@ -59,7 +33,7 @@ class ImageLabelActivity : AppCompatActivity() {
5933
// Task completed successfully
6034
fabProgressCircle.hide()
6135
itemsList.addAll(it)
62-
itemAdapter = ImageLabelAdapter(itemsList,false)
36+
itemAdapter = ImageLabelAdapter(itemsList, false)
6337
rvLabel.adapter = itemAdapter
6438
sheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED)
6539
}
@@ -80,7 +54,7 @@ class ImageLabelActivity : AppCompatActivity() {
8054
// Task completed successfully
8155
fabProgressCircle.hide()
8256
itemsList.addAll(it)
83-
itemAdapter = ImageLabelAdapter(itemsList,true)
57+
itemAdapter = ImageLabelAdapter(itemsList, true)
8458
rvLabel.adapter = itemAdapter
8559
sheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED)
8660
}
@@ -91,24 +65,16 @@ class ImageLabelActivity : AppCompatActivity() {
9165
}
9266
}
9367

94-
override fun onResume() {
95-
super.onResume()
96-
cameraView.start()
97-
}
98-
99-
override fun onPause() {
100-
cameraView.stop()
101-
super.onPause()
102-
}
103-
104-
private fun showPreview() {
105-
framePreview.visibility = View.VISIBLE
106-
cameraView.visibility = View.GONE
107-
}
108-
109-
private fun hidePreview() {
110-
framePreview.visibility = View.GONE
111-
cameraView.visibility = View.VISIBLE
68+
override fun onClick(v: View?) {
69+
fabProgressCircle.show()
70+
cameraView.captureImage { cameraKitImage ->
71+
// Get the Bitmap from the captured shot
72+
getLabelsFromClod(cameraKitImage.bitmap)
73+
runOnUiThread {
74+
showPreview()
75+
imagePreview.setImageBitmap(cameraKitImage.bitmap)
76+
}
77+
}
11278
}
11379

11480
}

app/src/main/java/io/github/the_dagger/mlkit/ImageLabelAdapter.kt renamed to app/src/main/java/io/github/the_dagger/mlkit/adapter/ImageLabelAdapter.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
1+
package io.github.the_dagger.mlkit.adapter
22

33
import android.content.Context
44
import android.support.v4.content.ContextCompat
@@ -8,6 +8,7 @@ import android.view.View
88
import android.view.ViewGroup
99
import com.google.firebase.ml.vision.cloud.label.FirebaseVisionCloudLabel
1010
import com.google.firebase.ml.vision.label.FirebaseVisionLabel
11+
import io.github.the_dagger.mlkit.R
1112
import kotlinx.android.synthetic.main.item_row.view.*
1213

1314
class ImageLabelAdapter(private val firebaseVisionList: List<Any>, private val isCloud: Boolean) : RecyclerView.Adapter<ImageLabelAdapter.ItemHolder>() {
@@ -36,7 +37,6 @@ class ImageLabelAdapter(private val firebaseVisionList: List<Any>, private val i
3637

3738
}
3839

39-
4040
override fun onBindViewHolder(holder: ItemHolder, position: Int) {
4141
val currentItem = firebaseVisionList[position]
4242
if (isCloud)

0 commit comments

Comments
 (0)