如何解决修复条码扫描仪相机失真
我想在我的应用中使用条形码扫描仪。不幸的是,SurfaceView 中的预览极度失真。
在此 SurfaceView 中,应显示条码扫描器的相机预览。
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="fill_parent"
android:background="@drawable/image_prev"
android:layout_height="261dp" >
<SurfaceView
android:id="@+id/barcode_view"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
</SurfaceView>
<ImageView
android:id="@+id/retry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/retry"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"></ImageView>
</androidx.constraintlayout.widget.ConstraintLayout>
这是我的整个 Kotlin 课程:
class BarcodeActivity : AppCompatActivity() {
private val requestCodeCameraPermission = 1001
private lateinit var cameraSource: CameraSource
private lateinit var detector: BarcodeDetector
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_barcode)
if (ContextCompat.checkSelfPermission(this,android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
askForCameraPermission()
} else {
setupControls()
}
if (Build.VERSION.SDK_INT >= 19 && Build.VERSION.SDK_INT < 21) {
setwindowFlag(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,true)
}
if (Build.VERSION.SDK_INT >= 19) {
window.decorView.systemUIVisibility =
View.SYstem_UI_FLAG_LAYOUT_STABLE or View.SYstem_UI_FLAG_LAYOUT_FULLSCREEN
}
if (Build.VERSION.SDK_INT >= 21) {
setwindowFlag(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,false)
window.statusBarColor = Color.TRANSPARENT
window.decorView.systemUIVisibility =
View.SYstem_UI_FLAG_LAYOUT_STABLE or View.SYstem_UI_FLAG_LAYOUT_FULLSCREEN or View.SYstem_UI_FLAG_LIGHT_STATUS_BAR
}
}
private fun setwindowFlag(bits: Int,on: Boolean) {
val win = window
val winParams = win.attributes
if (on) {
winParams.flags = winParams.flags or bits
} else {
winParams.flags = winParams.flags and bits.inv()
}
win.attributes = winParams
}
private fun setupControls() {
detector = BarcodeDetector.Builder(this).build()
cameraSource = CameraSource.Builder(this,detector)
.setAutoFocusEnabled(true)
.build()
barcode_view.holder.addCallback(surfaceCallBack)
detector.setProcessor(processor)
}
private fun askForCameraPermission() {
ActivityCompat.requestPermissions(
this,arrayOf(android.Manifest.permission.CAMERA),requestCodeCameraPermission
)
}
override fun onRequestPermissionsResult(
requestCode: Int,permissions: Array<out String>,grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode,permissions,grantResults)
if(requestCode == requestCodeCameraPermission && grantResults.isNotEmpty()) {
if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {
setupControls()
} else {
Toast.makeText(applicationContext,"Berechtigung nicht gegeben",Toast.LENGTH_SHORT).show()
}
}
}
private val surfaceCallBack = object : SurfaceHolder.Callback {
override fun surfaceChanged(p0: SurfaceHolder?,p1: Int,p2: Int,p3: Int) {
}
override fun surfaceDestroyed(p0: SurfaceHolder?) {
cameraSource.stop()
}
override fun surfaceCreated(surfaceHolder: SurfaceHolder?) {
try {
if (ActivityCompat.checkSelfPermission(
this@BarcodeActivity,android.Manifest.permission.CAMERA
) != PackageManager.PERMISSION_GRANTED
) {
// Todo: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions,and then overriding
// public void onRequestPermissionsResult(int requestCode,String[] permissions,// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return
}
cameraSource.start(surfaceHolder)
} catch (exception: Exception) {
Toast.makeText(
applicationContext,"Ups,da ist was schief gelaufen...",Toast.LENGTH_SHORT
).show()
}
}
}
private val processor = object : Detector.Processor<Barcode>{
override fun release() {
Todo("Not yet implemented")
}
override fun receiveDetections(detections: Detector.Detections<Barcode>?) {
if(detections != null && detections.detectedItems.isNotEmpty()) {
val qrCodes: SparseArray<Barcode> = detections.detectedItems
val code = qrCodes.valueAt(0)
editTextBarcode.text = code.displayValue
} else {
editTextBarcode.text = ""
}
}
}
}
相机预览是否有可能自动获取Surface View的格式或分辨率?
我希望有人能够帮助我解决这个问题。
非常感谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。