Skip to content

Commit

Permalink
feat: use camera direction to decide camera facing
Browse files Browse the repository at this point in the history
Context: Instead of passing each field of OSBARCScanParameters in an extra through the intent, we might as well pass the whole object. To do that, it has to be made a Serializable.

References: https://outsystemsrd.atlassian.net/browse/RMET-2764
  • Loading branch information
alexgerardojacinto committed Nov 13, 2023
1 parent 1a3f2c7 commit 87182c8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,7 @@ class OSBARCController {

companion object {
private const val SCAN_REQUEST_CODE = 112
private const val SCAN_INSTRUCTIONS = "SCAN_INSTRUCTIONS"
private const val CAMERA_DIRECTION = "CAMERA_DIRECTION"
private const val SCAN_ORIENTATION = "SCAN_ORIENTATION"
private const val SCAN_BUTTON = "SCAN_BUTTON"
private const val SCAN_BUTTON_TEXT = "SCAN_BUTTON_TEXT"
private const val SCAN_HINT = "SCAN_HINT"
private const val SCAN_LIBRARY = "SCAN_LIBRARY"
private const val SCAN_PARAMETERS = "SCAN_PARAMETERS"
private const val SCAN_RESULT = "scanResult"
private const val LOG_TAG = "OSBARCController"
}
Expand All @@ -33,16 +27,11 @@ class OSBARCController {
* @param parameters - object that contains all the barcode parameters to be used when scanning.
*/
fun scanCode(activity: Activity, parameters: OSBARCScanParameters) {
val scanningIntent = Intent(activity, OSBARCScannerActivity::class.java).apply {
putExtra(SCAN_INSTRUCTIONS, parameters.scanInstructions)
putExtra(CAMERA_DIRECTION, parameters.cameraDirection)
putExtra(SCAN_ORIENTATION, parameters.scanOrientation)
putExtra(SCAN_BUTTON, parameters.scanButton)
putExtra(SCAN_BUTTON_TEXT, parameters.scanText)
putExtra(SCAN_HINT, parameters.hint)
putExtra(SCAN_LIBRARY, parameters.androidScanningLibrary)
}
activity.startActivityForResult(scanningIntent, SCAN_REQUEST_CODE)
activity.startActivityForResult(
Intent(
activity, OSBARCScannerActivity::class.java
).putExtra(SCAN_PARAMETERS, parameters), SCAN_REQUEST_CODE
)
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.outsystems.plugins.barcode.model

import java.io.Serializable

/**
* Data class that represents the object with the scan parameters.
*/
Expand All @@ -11,4 +13,4 @@ data class OSBARCScanParameters(
val scanText: String?,
val hint: Int?,
val androidScanningLibrary: String?
)
) : Serializable
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import com.outsystems.plugins.barcode.controller.OSBARCScanLibraryFactory
import com.outsystems.plugins.barcode.controller.helper.OSBARCMLKitHelper
import com.outsystems.plugins.barcode.controller.helper.OSBARCZXingHelper
import com.outsystems.plugins.barcode.model.OSBARCError
import com.outsystems.plugins.barcode.model.OSBARCScanParameters
import com.outsystems.plugins.barcode.view.ui.theme.BarcodeScannerTheme
import java.lang.Exception

Expand All @@ -46,9 +47,11 @@ class OSBARCScannerActivity : ComponentActivity() {
private const val SCAN_SUCCESS_RESULT_CODE = -1
private const val SCAN_RESULT = "scanResult"
private const val LOG_TAG = "OSBARCScannerActivity"
private const val SCAN_LIBRARY = "SCAN_LIBRARY"
private const val SCAN_PARAMETERS = "SCAN_PARAMETERS"
}

private lateinit var parameters: OSBARCScanParameters

/**
* Overrides the onCreate method from Activity, setting the UI of the screen
*/
Expand All @@ -57,7 +60,7 @@ class OSBARCScannerActivity : ComponentActivity() {

setContent {
BarcodeScannerTheme {
ScanScreen()
ScanScreen(intent.extras?.getSerializable(SCAN_PARAMETERS) as OSBARCScanParameters)
}
}
}
Expand All @@ -67,7 +70,7 @@ class OSBARCScannerActivity : ComponentActivity() {
* as well as creating an instance of OSBARCBarcodeAnalyzer for image analysis.
*/
@Composable
fun ScanScreen() {
fun ScanScreen(parameters: OSBARCScanParameters) {
val lifecycleOwner = LocalLifecycleOwner.current
val context = LocalContext.current

Expand Down Expand Up @@ -102,7 +105,7 @@ class OSBARCScannerActivity : ComponentActivity() {
val previewView = PreviewView(context)
val preview = Preview.Builder().build()
val selector = CameraSelector.Builder()
.requireLensFacing(CameraSelector.LENS_FACING_BACK) // temporary
.requireLensFacing(if (parameters.cameraDirection == 2) CameraSelector.LENS_FACING_FRONT else CameraSelector.LENS_FACING_BACK)
.build()
preview.setSurfaceProvider(previewView.surfaceProvider)
val imageAnalysis = ImageAnalysis.Builder()
Expand All @@ -112,7 +115,7 @@ class OSBARCScannerActivity : ComponentActivity() {
ContextCompat.getMainExecutor(context),
OSBARCBarcodeAnalyzer(
OSBARCScanLibraryFactory.createScanLibraryWrapper(
intent.extras?.getString(SCAN_LIBRARY) ?: "",
parameters.androidScanningLibrary ?: "",
OSBARCZXingHelper(),
OSBARCMLKitHelper()
), // temporary
Expand Down

0 comments on commit 87182c8

Please sign in to comment.