Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lock screen rotation when scanner is shown #7

Merged
merged 5 commits into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 4.1.0

- Lock screen rotation when scanner is shown

## 4.0.1

- Fix When ViewController whose view is not in the window hierarchy, present Scan ViewController failed will failed.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package de.mintware.barcode_scan

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.pm.ActivityInfo
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.Surface
import android.view.WindowManager
import com.google.zxing.BarcodeFormat
import com.google.zxing.Result
import me.dm7.barcodescanner.zxing.ZXingScannerView
Expand Down Expand Up @@ -45,6 +49,18 @@ class BarcodeScannerActivity : Activity(), ZXingScannerView.ResultHandler {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

//lock orientation
val rotation = (getSystemService(
Context.WINDOW_SERVICE) as WindowManager).defaultDisplay.rotation
var orientation = when (rotation) {
Surface.ROTATION_0 -> ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
Surface.ROTATION_90 -> ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
Surface.ROTATION_180 -> ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT
else -> ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
}

requestedOrientation = orientation

config = Protos.Configuration.parseFrom(intent.extras!!.getByteArray(EXTRA_CONFIG))
}

Expand Down Expand Up @@ -120,6 +136,8 @@ class BarcodeScannerActivity : Activity(), ZXingScannerView.ResultHandler {
override fun handleResult(result: Result?) {
val intent = Intent()

requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED

val builder = Protos.ScanResult.newBuilder()
if (result == null) {

Expand Down
2 changes: 1 addition & 1 deletion example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:4.1.0'
classpath 'com.android.tools.build:gradle:4.1.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
Expand Down
6 changes: 6 additions & 0 deletions ios/Classes/BarcodeScannerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class BarcodeScannerViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

UIDevice.current.endGeneratingDeviceOrientationNotifications()

#if targetEnvironment(simulator)
view.backgroundColor = .lightGray
Expand Down Expand Up @@ -88,6 +90,8 @@ class BarcodeScannerViewController: UIViewController {
scanner!.stopScanning()
}

UIDevice.current.endGeneratingDeviceOrientationNotifications()

scanRect?.startAnimating()
MTBBarcodeScanner.requestCameraPermission(success: { success in
if success {
Expand All @@ -104,6 +108,8 @@ class BarcodeScannerViewController: UIViewController {
scanner?.stopScanning()
scanRect?.stopAnimating()

UIDevice.current.beginGeneratingDeviceOrientationNotifications()

if isFlashOn {
setFlashState(false)
}
Expand Down