Android中的Zxing条码扫描器问题

时间:2016-12-22 08:50:27

标签: java android zxing

我正在使用条码扫描器应用程序,在第一个Activity中点击按钮,我将转到BarcodeScanner Activity,我将导入Zxing库功能。扫描完成后,我将转到第3个活动,我将显示扫描结果。点击第3个活动中的按钮,我将回到第1个活动。对于具有棉花糖的设备,代码运行正常。但是问题发生在具有低于棉花糖的版本的设备之后,从第3次活动回到第1次活动,当我再次按下按钮时,扫描仪活动正在出现,但相机没有启动。它只显示一个空白页面。请帮忙。下面我发布了所有3个活动的代码。

First Activity:

        public class FirstActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_first);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                getWindow().setStatusBarColor(Color.parseColor("#FDB50A"));
            }
            ImageView Scan= (ImageView) findViewById(R.id.scanButton);
            Scan.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    FirstActivity.this.finish();
                    Intent nextPage= new Intent(FirstActivity.this,MainActivity.class);
                    startActivity(nextPage);


                }
            });


ScannerActivity:

    public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler{

        Integer response = 0 ;
        int currentIndex=0;
        Boolean flash=false;
        DataBaseHelper dataBaseHelper;

private ZXingScannerView mScannerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.e("onCreate", "onCreate");
        checkPermissions();
        mScannerView = new ZXingScannerView(this);
        mScannerView.setResultHandler(this);
boolean cam= isCameraUsebyApp();
        Log.d("cameraBar",cam+"");
        if(cam)
        {
                mScannerView.stopCamera();
        }
        cam= isCameraUsebyApp();
        Log.d("cameraBar",cam+"");

        mScannerView.startCamera();
//        FrameLayout frameLayout= new FrameLayout(this);
//        FrameLayout.LayoutParams mainParam= new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
//        frameLayout.setLayoutParams(mainParam);
//        Button scanButton= new Button(this);
        dataBaseHelper= new DataBaseHelper(this);
        if(dataBaseHelper.checkDataBase()==false)
        {
                try {
                        dataBaseHelper.createDataBase();
                } catch (IOException e)
                {
                        e.printStackTrace();
                }
        }
        else{

        }

        Log.d("AnimeshSQL","copy");

        dataBaseHelper.openDataBase();
//        List<String> data=dataBaseHelper.getQuotes("n",1);
     //   Log.d("AnimeshSQL",data.get(0).toString());
        LayoutParams params =
                new LinearLayout.LayoutParams(
                        LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT);
//        scanButton.setBackground(getResources().getDrawable(R.drawable.round_button));
//        scanButton.setText("Flash");
//        scanButton.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                     if(flash==false)
//                     {
//                             flash=true;
//
//
//                     }
//                        else
//                     {
//                           flash=false;
//                     }
//                        mScannerView.setFlash(flash);
//                }
//        });
//        scanButton.setLayoutParams(params);
//        frameLayout.addView(mScannerView);
//        frameLayout.addView(scanButton);
       // setContentView(mScannerView);
        checkPermissions();
        if(response == 1) {
                mScannerView = null;
                mScannerView = new ZXingScannerView(this);
                setContentView(mScannerView);
                response = 0;

        }



}
        public boolean isCameraUsebyApp() {
                Camera camera = null;
                try {
                        camera = Camera.open();
                } catch (RuntimeException e) {
                        return true;
                } finally {
                        if (camera != null) camera.release();
                }
                return false;
        }
        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, Manifest.permission.WRITE_EXTERNAL_STORAGE};


        private void requestPermission(String permission) {

//
                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) {
//
                                        checkPermissions();
                                } else {
                                        try {
                                                // FuncUtils.showToast(context, permissions[0] + " Denied!!!");
                                        } catch (Exception e) {
                                                e.printStackTrace();
                                        }
//
///
                                }
                                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)
        {
               //Some codes to handle the result
  Intent intent= new Intent(this,ScanResultActivity.class);
                startActivity(intent);
                //vbn
                mScannerView.stopCamera();
                MainActivity.this.finish();

         }
}


Final Activity:


    public class ScanResultActivity extends AppCompatActivity {
    SharedPreferences prefs;
    Button ok;
    ImageView Hubbell,CI;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan_result);
        prefs = getSharedPreferences("ScanPref", MODE_PRIVATE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            getWindow().setStatusBarColor(Color.parseColor("#FDB50A"));
        }
               //Codes to show the data
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ScanResultActivity.this.finish();
                Intent nextPage= new Intent(ScanResultActivity.this,FirstActivity.class);
                startActivity(nextPage);

            }
        });

1 个答案:

答案 0 :(得分:0)

您可以在OnActivityResult中编写Intent。

 // Call Back method  to get the Message form other Activity  
    @Override  
       protected void onActivityResult(int requestCode, int resultCode, Intent data)  
       {  
                 super.onActivityResult(requestCode, resultCode, data);  
                  // check if the request code is same as what is passed  here it is 2  
                   if(requestCode==2)  
                         {  
                            String message=data.getStringExtra("MESSAGE");   
                            textView1.setText(message);  
                         }  
     }