Android-如何更改条形码扫描仪视图大小?

Android-如何更改条形码扫描仪视图大小?,android,kotlin,Android,Kotlin,我正试图通过跟踪这个YouTube制作条形码扫描仪。通过跟踪这个视频,我能够让应用程序正常工作。但由于条形码阅读器占据整个页面,而扫描光标仅占据屏幕面积的40%左右。所以我想知道是否有办法缩小条形码,剩余的空间可以用来放置一些按钮或警告视图。 所以基本上我想设置QR相机视图的宽度和高度。那么有可能吗 很明显,绿色广场外的空间是浪费的,这就是为什么我想安排它,使它在尺寸上大约是200dp*200dp 代码如下: package com.example.priyanka.qrbarcodescan

我正试图通过跟踪这个YouTube制作条形码扫描仪。通过跟踪这个视频,我能够让应用程序正常工作。但由于条形码阅读器占据整个页面,而扫描光标仅占据屏幕面积的40%左右。所以我想知道是否有办法缩小条形码,剩余的空间可以用来放置一些按钮或警告视图。 所以基本上我想设置QR相机视图的宽度和高度。那么有可能吗

很明显,绿色广场外的空间是浪费的,这就是为什么我想安排它,使它在尺寸上大约是200dp*200dp

代码如下:

package com.example.priyanka.qrbarcodescanner

import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.content.pm.PackageManager
import android.hardware.Camera
import android.net.Uri
import android.os.Build
import android.support.v4.app.ActivityCompat
import android.support.v4.content.ContextCompat
import android.support.v7.app.AlertDialog
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast

import com.google.zxing.Result

import me.dm7.barcodescanner.zxing.ZXingScannerView

import android.Manifest.permission.CAMERA

class MainActivity : AppCompatActivity(), ZXingScannerView.ResultHandler {
    private var scannerView: ZXingScannerView? = null
    internal var mcontext: Context? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mcontext = this

        scannerView = ZXingScannerView(this)
        setContentView(scannerView)
        val currentApiVersion = Build.VERSION.SDK_INT

        if (currentApiVersion >= Build.VERSION_CODES.M) {
            if (checkPermission()) {
                Toast.makeText(applicationContext, "Permission already granted!", Toast.LENGTH_LONG).show()
            } else {
                requestPermission()
            }
        }
    }

    private fun checkPermission(): Boolean {
        return ContextCompat.checkSelfPermission(applicationContext, CAMERA) == PackageManager.PERMISSION_GRANTED
    }

    private fun requestPermission() {
        ActivityCompat.requestPermissions(this, arrayOf(CAMERA), REQUEST_CAMERA)
    }

    public override fun onResume() {
        super.onResume()

        val currentapiVersion = android.os.Build.VERSION.SDK_INT
        if (currentapiVersion >= android.os.Build.VERSION_CODES.M) {
            if (checkPermission()) {
                if (scannerView == null) {
                    scannerView = ZXingScannerView(this)
                    setContentView(scannerView)
                }
                scannerView!!.setResultHandler(this)
                scannerView!!.startCamera()
            } else {
                requestPermission()
            }
        }
    }

    public override fun onDestroy() {
        super.onDestroy()
        scannerView!!.stopCamera()
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
        when (requestCode) {
            REQUEST_CAMERA -> if (grantResults.size > 0) {

                val cameraAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED
                if (cameraAccepted) {
                    Toast.makeText(applicationContext, "Permission Granted, Now you can access camera", Toast.LENGTH_LONG).show()
                } else {
                    Toast.makeText(applicationContext, "Permission Denied, You cannot access and camera", Toast.LENGTH_LONG).show()
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        if (shouldShowRequestPermissionRationale(CAMERA)) {
                            showMessageOKCancel("You need to allow access to both the permissions",
                                    DialogInterface.OnClickListener { dialog, which ->
                                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                                            requestPermissions(arrayOf(CAMERA),
                                                    REQUEST_CAMERA)
                                        }
                                    })
                            return
                        }
                    }
                }
            }
        }
    }

    private fun showMessageOKCancel(message: String, okListener: DialogInterface.OnClickListener) {
        android.support.v7.app.AlertDialog.Builder(mcontext!!)
                .setMessage(message)
                .setPositiveButton("OK", okListener)
                .setNegativeButton("Cancel", null)
                .create()
                .show()
    }

    override fun handleResult(result: Result) {
        val myResult = result.text
        Log.d("QRCodeScanner", result.text)
        Log.d("QRCodeScanner", result.barcodeFormat.toString())

        val builder = AlertDialog.Builder(this)
        builder.setTitle("Scan Result")
        builder.setPositiveButton("OK") { dialog, which -> scannerView!!.resumeCameraPreview(this@MainActivity) }
        builder.setNeutralButton("Visit") { dialog, which ->
            val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(myResult))
            startActivity(browserIntent)
        }
        builder.setMessage(result.text)
        val alert1 = builder.create()
        alert1.show()
    }

    companion object {

        private val REQUEST_CAMERA = 1
        private val camId = Camera.CameraInfo.CAMERA_FACING_BACK
    }
}
package com.example.priyanka.qrbarcodescanner
导入android.content.Context
导入android.content.DialogInterface
导入android.content.Intent
导入android.content.pm.PackageManager
导入android.hardware.Camera
导入android.net.Uri
导入android.os.Build
导入android.support.v4.app.ActivityCompat
导入android.support.v4.content.ContextCompat
导入android.support.v7.app.AlertDialog
导入android.support.v7.app.AppActivity
导入android.os.Bundle
导入android.util.Log
导入android.widget.Toast
导入com.google.zxing.Result
导入me.dm7.barcodescanner.zxing.ZXingScannerView
导入android.Manifest.permission.CAMERA
类MainActivity:AppCompatActivity(),ZXingScannerView.ResultHandler{
私有变量scannerView:ZXingScannerView?=null
内部变量mcontext:上下文?=null
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
mcontext=这个
scannerView=ZXingScannerView(本)
setContentView(scannerView)
val currentApiVersion=Build.VERSION.SDK\u INT
如果(当前APIVersion>=Build.VERSION\u code.M){
if(checkPermission()){
Toast.makeText(applicationContext,“已授予权限!”,Toast.LENGTH\u LONG.show()
}否则{
请求权限()
}
}
}
private fun checkPermission():布尔值{
返回ContextCompat.checkSelfPermission(applicationContext,CAMERA)==PackageManager.PERMISSION\u已授予
}
私有权限(){
ActivityCompat.requestPermissions(此、阵列(摄像头)、请求摄像头)
}
恢复时的公共覆盖功能(){
super.onResume()
val currentapiVersion=android.os.Build.VERSION.SDK\u INT
if(currentapiVersion>=android.os.Build.VERSION\u CODES.M){
if(checkPermission()){
if(scannerView==null){
scannerView=ZXingScannerView(本)
setContentView(scannerView)
}
scannerView!!.setResultHandler(此)
scannerView!!.startCamera()
}否则{
请求权限()
}
}
}
公共存储区(){
super.ondestory()
scannerView!!.stopCamera()
}
重写onRequestPermissionsResult(请求代码:Int,权限:Array,GrantResult:IntArray){
何时(请求代码){
请求摄像头->如果(grantResults.size>0){
val cameraAccepted=grantResults[0]==PackageManager.PERMISSION\u已授予
如果(已接受){
Toast.makeText(applicationContext,“授予权限,现在您可以访问摄像头”,Toast.LENGTH\u LONG.show()
}否则{
Toast.makeText(applicationContext,“权限被拒绝,您无法访问和拍照”,Toast.LENGTH\u LONG.show()
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.M){
如果(应显示请求许可理由(摄像头)){
showMessageOKCancel(“您需要允许访问这两个权限”,
DialogInterface.OnClickListener{dialog,其中->
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.M){
请求权限(摄像头阵列),
请求(摄像头)
}
})
返回
}
}
}
}
}
}
私人娱乐showMessageOKCancel(消息:字符串,okListener:DialogInterface.OnClickListener){
android.support.v7.app.AlertDialog.Builder(mcontext!!)
.setMessage(消息)
.setPositiveButton(“确定”,确定侦听器)
.setNegativeButton(“取消”,null)
.create()
.show()
}
覆盖有趣的HandlerResult(结果:结果){
val myResult=result.text
Log.d(“QRCodeScanner”,result.text)
Log.d(“QRCodeScanner”,result.barcodeFormat.toString())
val builder=AlertDialog.builder(此)
builder.setTitle(“扫描结果”)
setPositiveButton(“OK”){对话框,其中->scannerView!!.resumeCameraPreview(this@MainActivity) }
setNeutralButton(“访问”){对话框,其中->
val browserIntent=Intent(Intent.ACTION_视图,Uri.parse(myResult))
startActivity(浏览器内容)
}
builder.setMessage(result.text)
val alert1=builder.create()
警报1.show()
}
伴星{
私人val请求_摄像头=1
private val camId=Camera.CameraInfo.Camera\u面朝后
}
}

提前感谢。

您可以将zxing嵌入式库与以下gradle依赖项一起使用:

implementation "com.journeyapps:zxing-android-embedded:3.5.0@aar"
implementation "com.google.zxing:core:3.3.0"
然后把它放在你的布局中,就像

<com.journeyapps.barcodescanner.DecoratedBarcodeView
    android:id="@+id/qr_scanner_view"
    android:layout_width="@dimen/your_width"
    android:layout_height="@dimen/your_height" />

然后在代码中使用它,如

DecoratedBarcodeView qrView = findViewById(R.id.qr_scanner_view);
CameraSettings s = new CameraSettings();
s.setRequestedCameraId(0); // front/back/etc
qrView.getBarcodeView().setCameraSettings(s);
qrView.resume();

qrView.decodeSingle(new BarcodeCallback() {
    @Override
    public void barcodeResult(BarcodeResult result) {
        Log.d("barcode result: " + result.toString());
        // do your thing with result
    }

    @Override
    public void possibleResultPoints(List<ResultPoint> resultPoints) {}
});
DecoratedBarcodeView qrView=findviewbyd(R.id.qr\u扫描器\u视图);
CameraSettings s=新的CameraSettings();
s、 setRequestedCameraId(0);//前/后/e