[Android] zxing에서 QR 코드를 읽을 때 화면이 가로 회전하지 않도록 합니다.
화면이 누워 ...
Android에서 QR 코드를 읽을 때 zxing-android-embedded라는 라이브러리를 사용할 수 있다고 생각합니다.
그러나 문제는 원래 바코드 용 라이브러리이기 때문에 참조대로 사용하면 카메라가 가로 회전 (랜드 스케이프 모드)되어 버립니다. QR코드만이라면 세로 그대로 읽고 싶네요.
언뜻 보면 Activity의 android:screenOrientation를 portrait로 만드는 것일까라고 생각했는데, 뒤에서 다른 Activity를 호출하는 것 같고, 이것만으로는 치료되지 않습니다. 그래서 세로로합시다.
어쩌면 2 분 정도로 끝납니다.

(이런 간지. 힘들다.)
종속성
gradle에 쓰지 않은 분은 쓰고 나서 시작합시다.
build.gradledependencies {
...
implementation 'com.journeyapps:zxing-android-embedded:3.6.0'
}
CaptureActivity를 상속한 Activity 만들기
zxing을 도입하면 com.journeyapps.barcodescanner.CaptureActivity Activity가 존재한다는 것을 알 수 있습니다. IntentIntegrator를 보면이 CaptureActivity가 분명히 기본적으로 사용되는 것 같습니다. 그래서 이것을 상속 한 Activity를 선언합시다. 내용은 비어도 상관 없습니다.
적당히 좋아하는 파일에 선언해도 좋고, 새롭게 이것 전용의 파일을 만들어 줘도 좋습니다.
MyCaptureActivity.ktclass MyCaptureActivity : CaptureActivity()
AndroidManifest 편집
새롭게 Activity를 만들었으므로, manifest 파일에 기술합니다.
android:name 이외는 기술하지 않아도 세로 화면이 되었습니다만, 일단 써 두면 안심이군요.
AndroidManifest.xml<application>
...
<activity
android:name=".MyCaptureActivity"
android:screenOrientation="portrait"
android:stateNotNeeded="true"
android:theme="@style/zxing_CaptureTheme"
android:windowSoftInputMode="stateAlwaysHidden">
</activity>
</application>
IntentIntegrator의 captureActivity로 설정
마지막으로 적용하고 끝입니다.
QR코드 카메라의 intent를 발행할 때는 IntentIntegrator를 사용한다고 생각합니다만, 생성한 인스턴스의 captureActivity에 방금 선언한 클래스를 할당해 주면, 화면이 세로가 됩니다.
Activity에서 호출하는 경우와 Fragment에서 호출하는 경우에 약간 쓰는 방법이 다릅니다.
dependencies {
...
implementation 'com.journeyapps:zxing-android-embedded:3.6.0'
}
zxing을 도입하면
com.journeyapps.barcodescanner.CaptureActivity Activity가 존재한다는 것을 알 수 있습니다. IntentIntegrator를 보면이 CaptureActivity가 분명히 기본적으로 사용되는 것 같습니다. 그래서 이것을 상속 한 Activity를 선언합시다. 내용은 비어도 상관 없습니다.적당히 좋아하는 파일에 선언해도 좋고, 새롭게 이것 전용의 파일을 만들어 줘도 좋습니다.
MyCaptureActivity.kt
class MyCaptureActivity : CaptureActivity()
AndroidManifest 편집
새롭게 Activity를 만들었으므로, manifest 파일에 기술합니다.
android:name 이외는 기술하지 않아도 세로 화면이 되었습니다만, 일단 써 두면 안심이군요.
AndroidManifest.xml<application>
...
<activity
android:name=".MyCaptureActivity"
android:screenOrientation="portrait"
android:stateNotNeeded="true"
android:theme="@style/zxing_CaptureTheme"
android:windowSoftInputMode="stateAlwaysHidden">
</activity>
</application>
IntentIntegrator의 captureActivity로 설정
마지막으로 적용하고 끝입니다.
QR코드 카메라의 intent를 발행할 때는 IntentIntegrator를 사용한다고 생각합니다만, 생성한 인스턴스의 captureActivity에 방금 선언한 클래스를 할당해 주면, 화면이 세로가 됩니다.
Activity에서 호출하는 경우와 Fragment에서 호출하는 경우에 약간 쓰는 방법이 다릅니다.
<application>
...
<activity
android:name=".MyCaptureActivity"
android:screenOrientation="portrait"
android:stateNotNeeded="true"
android:theme="@style/zxing_CaptureTheme"
android:windowSoftInputMode="stateAlwaysHidden">
</activity>
</application>
마지막으로 적용하고 끝입니다.
QR코드 카메라의 intent를 발행할 때는
IntentIntegrator를 사용한다고 생각합니다만, 생성한 인스턴스의 captureActivity에 방금 선언한 클래스를 할당해 주면, 화면이 세로가 됩니다.Activity에서 호출하는 경우와 Fragment에서 호출하는 경우에 약간 쓰는 방법이 다릅니다.
MainActivity.kt
val intentIntegrator = IntentIntegrator(this).apply {
setPrompt("Scan a QR code")
captureActivity = MyCaptureActivity::class.java
}
intentIntegrator.initiateScan()
MyFragment.kt
val intentIntegrator = IntentIntegrator.forSupportFragment(this).apply {
setPrompt("Scan a QR code")
captureActivity = MyCaptureActivity::class.java
}
intentIntegrator.initiateScan()
결과

이런 것이

이렇게 되었습니다!
결론
꽤 과거에 논의되고 있던 문제 같고, 공식 리포지토리의 Issue나 StackOverflow에는 정보가 있었습니다만, 일본어에는 되어 있지 않은 것 같네요.
setScreenOrientation() property for IntentIntegrator #16
Change QR Scanner orientation with ZXING in Android Studio
이상입니다, 감사합니다.
Reference
이 문제에 관하여([Android] zxing에서 QR 코드를 읽을 때 화면이 가로 회전하지 않도록 합니다.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/tktktks10/items/3b327b2900d38e672996
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
꽤 과거에 논의되고 있던 문제 같고, 공식 리포지토리의 Issue나 StackOverflow에는 정보가 있었습니다만, 일본어에는 되어 있지 않은 것 같네요.
setScreenOrientation() property for IntentIntegrator #16
Change QR Scanner orientation with ZXING in Android Studio
이상입니다, 감사합니다.
Reference
이 문제에 관하여([Android] zxing에서 QR 코드를 읽을 때 화면이 가로 회전하지 않도록 합니다.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/tktktks10/items/3b327b2900d38e672996텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)