This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private lateinit var sensorManager: SensorManager | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| setContentView(R.layout.activity_main) | |
| // initializing the sensor manager | |
| sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager | |
| // getting the list of all the availabale sensors in the current device |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MainActivity : AppCompatActivity() { | |
| private lateinit var fotoapparat: Fotoapparat | |
| private lateinit var barcodeScanner: BarcodeScanner | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| setContentView(R.layout.activity_main) | |
| initializeBarcodeScanner() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private fun scanImageForBarcode(it: BitmapPhoto) { | |
| val inputImage = InputImage.fromBitmap(it.bitmap, it.rotationDegrees) | |
| val task = barcodeScanner.process(inputImage) | |
| task.addOnSuccessListener { barCodesList -> | |
| for (barcodeObject in barCodesList) { | |
| val barcodeValue = barcodeObject.rawValue | |
| Log.d("Barcode", "The code %s".format(barcodeValue)) | |
| } | |
| } | |
| task.addOnFailureListener { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private lateinit var barcodeScanner: BarcodeScanner | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| setContentView(R.layout.activity_main) | |
| val options = BarcodeScannerOptions.Builder() | |
| .setBarcodeFormats( | |
| Barcode.FORMAT_EAN_13, | |
| Barcode.FORMAT_QR_CODE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private fun takeImage() { | |
| fotoapparat.takePicture() | |
| .toBitmap() | |
| .whenAvailable { picture -> | |
| scanImageForBarcode(picture!!) | |
| } | |
| } | |
| private fun scanImageForBarcode(picture: Bitmap){ | |
| // handle and scan the picture for barcode |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private lateinit var fotoapparat: Fotoapparat | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| setContentView(R.layout.activity_main) | |
| fotoapparat = Fotoapparat.with(this) | |
| .into(cameraView) | |
| .previewScaleType(ScaleType.CenterCrop) | |
| .build() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <io.fotoapparat.view.CameraView | |
| android:id="@+id/cameraView" | |
| android:layout_width="match_parent" | |
| android:layout_height="400dp" | |
| app:layout_constraintEnd_toEndOf="parent" | |
| app:layout_constraintStart_toStartOf="parent" | |
| app:layout_constraintTop_toTopOf="parent" /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| override fun onStart() { | |
| super.onStart() | |
| fotoapparat.start() | |
| } | |
| override fun onStop() { | |
| super.onStop() | |
| fotoapparat.stop() | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private val CAMEREA_REQUEST_ID = 1144 | |
| private fun isCameraPermissionGranted(): Boolean { | |
| return (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) | |
| == PackageManager.PERMISSION_GRANTED) | |
| } | |
| private fun requestCameraPermission() { | |
| ActivityCompat.requestPermissions( | |
| this, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| dependencies { | |
| /* other dependencies | |
| .....*/ | |
| //ml kit barcode | |
| implementation 'com.google.mlkit:barcode-scanning:16.0.1' | |
| // fotoapparat library | |
| implementation 'io.fotoapparat:fotoapparat:2.7.0' | |
| } |