在Zxing扫描仪页面上方添加手电筒按钮 [英] Add the flashlight button above the Zxing Scanner page

查看:13

我一直在开发一个扫描器应用程序,我一直在其中实现 Zxing 条码扫描器库.我正在尝试在相机视图上方的扫描仪页面上添加一个新按钮(手电筒按钮),但失败了.谁能帮我在 Zxing 的扫描仪视图上方放置一个按钮?下面我发布我的代码.请帮忙.

公共类 MainActivity 扩展 AppCompatActivity 实现 ZXingScannerView.ResultHandler{整数响应 = 0 ;int currentIndex=0;私人 ZXingScannerView mScannerView;@覆盖protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Log.e("onCreate", "onCreate");mScannerView = new ZXingScannerView(this);Button scanButton= new Button(this);LayoutParams 参数 =新的 LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);scanButton.setBackground(getResources().getDrawable(R.drawable.round_button));scanButton.setText("手电筒");scanButton.setLayoutParams(params);mScannerView.addView(scanButton);setContentView(mScannerView);}私人无效检查权限(){尝试 {for (int i = currentIndex; i  0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){//Snackbar.make(view, "权限已授予,现在您可以访问位置数据.", Snackbar.LENGTH_LONG)//                           .展示();检查权限();} 别的 {尝试 {//FuncUtils.showToast(context, permissions[0] + " Denied!!!");} 捕获(异常 e){e.printStackTrace();}//onBackPressed();////Snackbar.make(view, "Permission Denied, You cannot access location data.", Snackbar.LENGTH_LONG).show();}休息;}}@覆盖公共无效 onResume() {super.onResume();如果(响应== 1){mScannerView = null;mScannerView = new ZXingScannerView(this);setContentView(mScannerView);响应 = 0;}mScannerView.setResultHandler(this);mScannerView.startCamera();}@覆盖公共无效 onDestroy() {super.onDestroy();mScannerView.stopCamera();}@覆盖protected void onRestart() {super.onRestart();Log.d("ani","onrestart");}@覆盖public void handleResult(Result rawResult) {最终对话框对话框 = 新对话框(上下文);dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);dialog.setContentView(R.layout.dialog_screen);dialog.getWindow().setLayout(700, 850);TextView text = (TextView) dialog.findViewById(R.id.CouponCode);按钮 dialogBu​​tton = (Button) dialog.findViewById(R.id.save_code);//如果按钮被点击,关闭自定义对话框text.setText(rawResult.getText());dialogBu​​tton.setOnClickListener(new View.OnClickListener() {@覆盖public void onClick(View v){dialog.dismiss();MainActivity.this.finish();}});对话框显示();Log.e("Ani", rawResult.getText());Log.e("Ani", rawResult.getBarcodeFormat().toString());}}
解决方案

将您的按钮和 ZxingView 添加到父级并删除对 setContentView 的第一次调用

super.onCreate(savedInstanceState);FrameLayout frameLayout = new FrameLayout(this, new ViewGroup.LayoutParams(ViewGroup.MATCH_PARENT,ViewGroup.MATCH_PARENT))mScannerView = new ZXingScannerView(this);Button scanButton= new Button(this);FrameLayout 参数 =新的 FrameLayout.LayoutParams(FrameLayout.WRAP_CONTENT,FrameLayout.WRAP_CONTENT);scanButton.setBackground(getResources().getDrawable(R.drawable.round_button));scanButton.setText("手电筒");scanButton.setLayoutParams(params);frameLayout .addView(mScannerView);frameLayout .addView(scanButton);setContentView(frameLayout);
  • 原文

I have been developing an scanner application where I have been implementing the Zxing bar code scanner library. I am trying to add a new button(flashlight button) on the scanner page above the camera view but failing. Can anyone help me in putting a button above the scanner view of Zxing? Below I am posting my code. Please help.

public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler{

        Integer response = 0 ;
        int currentIndex=0;

private ZXingScannerView mScannerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.e("onCreate", "onCreate");
        mScannerView = new ZXingScannerView(this);
        Button scanButton= new Button(this);
        LayoutParams params =
                new LinearLayout.LayoutParams(
                        LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT);
        scanButton.setBackground(getResources().getDrawable(R.drawable.round_button));
        scanButton.setText("Flash Light");
        scanButton.setLayoutParams(params);
        mScannerView.addView(scanButton);
        setContentView(mScannerView);

        }
        private void checkPermissions() {
                try {
                        for (int i = currentIndex; i < permissions.length; i++) {
                                currentIndex = currentIndex + 1;
                                int result = ContextCompat.checkSelfPermission(context, permissions[i]);
                                if (result == PackageManager.PERMISSION_GRANTED) {
                                } else {
                                        requestPermission(permissions[i]);
                                        return;
                                }
                        }
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }

        Activity activity = this;
        Context context = this;

        String[] permissions = new String[]{Manifest.permission.CAMERA};


        private void requestPermission(String permission) {

//        if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) {
//            Toast.makeText(context,
//                    "GPS permission allows us to access location data. Please allow in App Settings for additional functionality.",
//                    Toast.LENGTH_LONG).show();
//        } else {
                ActivityCompat.requestPermissions(activity, new String[]{permission},
                        101);
//        }
        }

        @Override
        public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
                switch (requestCode) {
                        case 101:
                                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//                    Snackbar.make(view, "Permission Granted, Now you can access location data.", Snackbar.LENGTH_LONG)
//                            .show();
                                        checkPermissions();
                                } else {
                                        try {
                                                // FuncUtils.showToast(context, permissions[0] + " Denied!!!");
                                        } catch (Exception e) {
                                                e.printStackTrace();
                                        }
//                    onBackPressed();
////                    Snackbar.make(view, "Permission Denied, You cannot access location data.", Snackbar.LENGTH_LONG).show();
                                }
                                break;
                }
        }


        @Override
public void onResume() {
        super.onResume();

        if(response == 1) {
        mScannerView = null;
        mScannerView = new ZXingScannerView(this);
        setContentView(mScannerView);
        response = 0;

        }

        mScannerView.setResultHandler(this);
        mScannerView.startCamera();

        }

@Override
public void onDestroy() {
        super.onDestroy();


        mScannerView.stopCamera();

        }

        @Override
        protected void onRestart() {
                super.onRestart();
                Log.d("ani","onrestart");
        }

        @Override
public void handleResult(Result rawResult) {



        final Dialog dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.dialog_screen);
        dialog.getWindow().setLayout(700, 850);
        TextView text = (TextView) dialog.findViewById(R.id.CouponCode);
        Button dialogButton = (Button) dialog.findViewById(R.id.save_code);
        // if button is clicked, close the custom dialog
        text.setText(rawResult.getText());
        dialogButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v)
                {
                        dialog.dismiss();
                        MainActivity.this.finish();
                }
        });

        dialog.show();
        Log.e("Ani", rawResult.getText());
        Log.e("Ani", rawResult.getBarcodeFormat().toString());



        }
}
解决方案

Add your button and ZxingView to a parent and remove the first call to setContentView

super.onCreate(savedInstanceState);
    FrameLayout frameLayout = new FrameLayout(this, new ViewGroup.LayoutParams(
            ViewGroup.MATCH_PARENT,
            ViewGroup.MATCH_PARENT))
    mScannerView = new ZXingScannerView(this);
    Button scanButton= new Button(this);
    FrameLayout params =
            new FrameLayout.LayoutParams(
                    FrameLayout.WRAP_CONTENT,
                    FrameLayout.WRAP_CONTENT);
    scanButton.setBackground(getResources().getDrawable(R.drawable.round_button));
    scanButton.setText("Flash Light");
    scanButton.setLayoutParams(params);
    frameLayout .addView(mScannerView );
    frameLayout .addView(scanButton);
    setContentView(frameLayout );