在Zxing扫描仪页面上方添加手电筒按钮

时间:2016-12-01 09:47:00

标签: android zxing

我一直在开发扫描仪应用程序,我一直在实施Zxing条形码扫描程序库。我正在尝试在摄像机视图上方的扫描仪页面上添加一个新按钮(手电筒按钮),但是失败了。任何人都可以帮我在Zxing的扫描仪视图上方放一个按钮吗?下面我发布我的代码。请帮忙。

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());



        }
}

1 个答案:

答案 0 :(得分:2)

将您的按钮和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 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 );