Skip to content

Commit

Permalink
add zoom_v4
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksandrN committed Mar 23, 2020
1 parent 67c0a93 commit 73592b1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ android {
dependencies {
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.50'
implementation 'me.dm7.barcodescanner:zxing:1.9.13'
implementation 'com.github.AlexVegner:barcodescanner:zoom_v4'
api 'com.google.android.material:material:1.0.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,83 @@ import me.dm7.barcodescanner.zxing.ZXingScannerView
class BarcodeScannerActivity : Activity(), ZXingScannerView.ResultHandler {

lateinit var scannerView: me.dm7.barcodescanner.zxing.ZXingScannerView
var autoFocus = true
var zoom = ZOOM_2X

companion object {
val REQUEST_TAKE_PHOTO_CAMERA_PERMISSION = 100
val TOGGLE_FLASH = 200
val AUTO_FOCUS = 300
val ZOOM = 400
val ZOOM_1X = 1.0
val ZOOM_2X = 2.0
val ZOOM_4X = 4.0

}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
title = ""
scannerView = ZXingScannerView(this)
scannerView.setAutoFocus(true)
scannerView.setAutoFocus(autoFocus)
// this paramter will make your HUAWEI phone works great!
scannerView.setAspectTolerance(0.5f)
setContentView(scannerView)
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
if (zoom == ZOOM_4X) {
val item = menu.add(0,
ZOOM, 0, "4x")
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)
} else if (zoom == ZOOM_2X) {
val item = menu.add(0,
ZOOM, 0, "2x")
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)
} else {
val item = menu.add(0,
ZOOM, 0, "1x")
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)
}
if (scannerView.flash) {
val item = menu.add(0,
TOGGLE_FLASH, 0, "Flash Off")
TOGGLE_FLASH, 1, "Flash Off")
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)
} else {
val item = menu.add(0,
TOGGLE_FLASH, 1, "Flash On")
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)
}
if (autoFocus) {
val item = menu.add(0,
AUTO_FOCUS, 2, "Auto Focus Off")
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)
} else {
val item = menu.add(0,
TOGGLE_FLASH, 0, "Flash On")
AUTO_FOCUS, 2, "Auto Focus On")
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)
}
return super.onCreateOptionsMenu(menu)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == ZOOM) {
zoom = if (zoom == ZOOM_1X) ZOOM_2X else if (zoom == ZOOM_2X) ZOOM_4X else ZOOM_1X
scannerView.zoom = zoom
this.invalidateOptionsMenu()
return true
}
if (item.itemId == TOGGLE_FLASH) {
scannerView.flash = !scannerView.flash
this.invalidateOptionsMenu()
return true
}
if (item.itemId == AUTO_FOCUS) {
autoFocus = !autoFocus
scannerView.setAutoFocus(autoFocus)
this.invalidateOptionsMenu()
return true
}
return super.onOptionsItemSelected(item)
}

Expand All @@ -60,6 +101,7 @@ class BarcodeScannerActivity : Activity(), ZXingScannerView.ResultHandler {
scannerView.setResultHandler(this)
// start camera immediately if permission is already given
if (!requestCameraAccessIfNecessary()) {
scannerView.zoom = zoom
scannerView.startCamera()
}
}
Expand Down Expand Up @@ -99,6 +141,7 @@ class BarcodeScannerActivity : Activity(), ZXingScannerView.ResultHandler {
when (requestCode) {
REQUEST_TAKE_PHOTO_CAMERA_PERMISSION -> {
if (PermissionUtil.verifyPermissions(grantResults)) {
scannerView.zoom = zoom
scannerView.startCamera()
} else {
finishWithError("PERMISSION_NOT_GRANTED")
Expand Down

0 comments on commit 73592b1

Please sign in to comment.