Skip to content

Instantly share code, notes, and snippets.

@saeed-khalafinejad
Last active October 16, 2022 10:10
Show Gist options
  • Save saeed-khalafinejad/cb12fbc06225f4bdd17edd898f5683f7 to your computer and use it in GitHub Desktop.
Save saeed-khalafinejad/cb12fbc06225f4bdd17edd898f5683f7 to your computer and use it in GitHub Desktop.
Android barcode scanner activity that is implemented by Google ML Kit and Fotoapparat library with the Kotlin language
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<io.fotoapparat.view.CameraView
android:id="@+id/cameraView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:layout_width="60dp"
android:layout_height="match_parent"
android:layout_marginTop="60dp"
android:layout_marginBottom="60dp"
android:background="#7F808080"
app:layout_constraintLeft_toLeftOf="parent"/>
<TextView
android:layout_width="60dp"
android:layout_height="match_parent"
android:background="#7F808080"
android:layout_marginTop="60dp"
android:layout_marginBottom="60dp"
app:layout_constraintRight_toRightOf="parent"/>
<TextView
android:id="@+id/textViewTop"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#7F808080"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:id="@+id/textViewBottom"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#7F808080"
app:layout_constraintBottom_toBottomOf="parent"/>
<LinearLayout
android:id="@+id/layScan"
android:layout_width="match_parent"
android:layout_height="80dp"
android:gravity="center_vertical"
android:orientation="vertical"
android:background="@drawable/rect_grad"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#FF0000"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.kasabeh.barcode_ml_foto">
<uses-permission android:name="android.permission.CAMERA"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 29
defaultConfig {
applicationId "org.kasabeh.barcode_ml_foto"
minSdkVersion 16
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.mlkit:barcode-scanning:16.0.3'
implementation 'io.fotoapparat:fotoapparat:2.7.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
class MainActivity : AppCompatActivity() {
private lateinit var fotoapparat: Fotoapparat
private val barcodeScanner: BarcodeScanner = BarcodeScanning.getClient()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
checkCameraPermission()
initFrameProcessor()
val ani = AnimationUtils.loadAnimation(this, R.anim.scan_anim);
ani.repeatCount = Animation.INFINITE
layScan.animation = ani
}
private fun initFrameProcessor() {
fotoapparat = Fotoapparat.with(this)
.into(cameraView)
.previewScaleType(ScaleType.CenterCrop)
.frameProcessor { it ->
val inputImage = InputImage.fromByteArray(it.image, it.size.width, it.size.height, it.rotation, InputImage.IMAGE_FORMAT_NV21)
barcodeScanner.process(inputImage)
.addOnSuccessListener { barCodes ->
for (br in barCodes) {
val brValue = br.rawValue
println(">>>>>>>>>>>> Barcode value is $brValue")
fotoapparat.stop()
Toast.makeText(this, "Barcode value is $brValue", Toast.LENGTH_LONG).show()
}
}
.addOnFailureListener {
println(">>>>>>>>>>>> Error")
it.printStackTrace()
}
}
.build()
}
override fun onStart() {
super.onStart()
fotoapparat.start()
}
override fun onStop() {
super.onStop()
fotoapparat.stop()
}
private fun checkCameraPermission() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CAMERA), 1 )
Toast.makeText(this, "Camera permission requested. Try again.", Toast.LENGTH_LONG).show()
}
}
}
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:centerColor="#AFFF0000"
android:endColor="#5FFF0000"
android:angle="90"
android:startColor="#5FFF0000"
android:type="linear">
</gradient>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:duration="15000"
android:fromYDelta="0%p"
android:toYDelta="100%p" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment