2017-06-29 67 views
0

我正在QR扫描仪基于Android的应用程序,它会从QR码中的数据发送到服务器,检查数据,但扫描无效的QR码Zxing扫描仪在运行一次后卡住了?

后我的扫描仪卡住,这里是我的代码

Qrresult。 java的

public class qrresult extends AppCompatActivity implements ZXingScannerView.ResultHandler { 

    private ZXingScannerView mScannerView; 
    private FocusHandler focusHandler; 
    public ImageButton back; 

public void backbut() { 
    back = (ImageButton) findViewById(R.id.bckbut); 
    back.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      //onBackPressed(); 
      Intent back = new Intent(getApplicationContext(),mainmenu.class); 
      startActivity(back); 
     } 
    }); 
    } 

    @Override 
    public void onCreate(Bundle state) { 
     super.onCreate(state); 
     setContentView(R.layout.activity_qrresult); 
     RelativeLayout zscanner = (RelativeLayout) findViewById(R.id.zxscan); 
     mScannerView = new ZXingScannerView(this); // Programmatically initialize the scanner view 

     focusHandler = new FocusHandler(new Handler(), mScannerView); 
     zscanner.addView(mScannerView); 
     backbut(); 

    } 

@Override 
public void onResume() { 
    super.onResume(); 
    mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results. 
    mScannerView.setAutoFocus(false); 
    mScannerView.startCamera(); 
    focusHandler.start(); 

} 

@Override 
public void onPause() { 
    super.onPause(); 
    mScannerView.stopCamera(); 
    focusHandler.stop(); //Stop camera on pause 
} 
@Override 
public void onStop(){ 
    super.onStop(); 
    mScannerView.stopCamera(); 
} 
@Override 
public void handleResult(Result rawResult) { 
     // Do something with the result here 
     String qrdata= rawResult.getText(); 
     qrresultWorker qrresultWorker= new qrresultWorker(this); 
     qrresultWorker.execute(qrdata); 
     //mScannerView.resumeCameraPreview(this); 

     // If you would like to resume scanning, call this method below: 
     //mScannerView.resumeCameraPreview(this); 
    } 
} 

QrresultWorker.java

public class qrresultWorker extends AsyncTask<String,Void,String> { 



private Context context; 
private AlertDialog alertDialog; 
private ProgressDialog Asycdialog; 
qrresultWorker(Context ctx) { 
    context = ctx; 
} 

@Override 
protected String doInBackground(String... params) { 



    String login_url = "http://10.0.2.2/projects/dbcon1/qrresult.php"; 


     try { 

      //Used for Sending data to Database 

      String qrdata = params[0]; 
      URL url = new URL(login_url); 
      HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); 
      httpURLConnection.setRequestMethod("POST"); 
      //httpURLConnection.setConnectTimeout(100000); 
      //httpURLConnection.setReadTimeout(100000); 
      httpURLConnection.setDoOutput(true); 
      httpURLConnection.setDoInput(true); 
      OutputStream outputStream = httpURLConnection.getOutputStream(); 
      BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8")); 
      String post_data = URLEncoder.encode("qrdata", "UTF-8") + "=" + URLEncoder.encode(qrdata, "UTF-8"); 
      bufferedWriter.write(post_data); 
      bufferedWriter.flush(); 
      bufferedWriter.close(); 
      outputStream.close(); 

      //Used for Getting data from Database 
      InputStream inputStream = httpURLConnection.getInputStream(); 
      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1")); 
      String result = ""; //initializing with empty string 
      String line = ""; //initializing with empty string 
      while ((line = bufferedReader.readLine()) != null) { 
       result += line; 
      } 
      bufferedReader.close(); 
      inputStream.close(); 
      httpURLConnection.disconnect(); 
      return result; 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    return null; 
} 

@Override 
protected void onPreExecute() { 
    //progress dialog 
    //Asycdialog = new ProgressDialog(context); 
    //Asycdialog.setMessage("Scanning..."); 
    //Asycdialog.show(); 

    //alertdialog 
    AlertDialog.Builder builder = new AlertDialog.Builder(context); 
    builder.setPositiveButton("Cancel", 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, 
            int id) { 
        //Intent go = new Intent(context.getApplicationContext(),qrresult.class); 
        //context.startActivity(go); 
       dialog.dismiss(); 

       } 
      }); 
    alertDialog = builder.create(); 
    alertDialog.setTitle("Scan Status"); 
} 

@Override 
protected void onPostExecute(String result) { 
    //Asycdialog.dismiss(); 

    /*String name = null; 
    int balance = 0; 
    String qrcode=null; 
    JSONObject jsonResponse = null; 
    try { 
     jsonResponse = new JSONObject(result); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
*/ 
    if (result.equals("success")){ 

     Intent intent = new Intent(context.getApplicationContext(),sendmoney.class); 
     context.startActivity(intent); 

    } 
    else { 

     Toast.makeText(context.getApplicationContext(),"Coundn't scan the QR code",Toast.LENGTH_SHORT).show(); 
     //alertDialog.setMessage("Scanning Failed,Invalid Qrcode, please retry"); 
     //alertDialog.show(); 

    } 


} 

@Override 
protected void onProgressUpdate(Void... values) { 
    super.onProgressUpdate(values); 
} 

} 

回答

0

尝试把条件,它仅扫描QR码下面的代码只扫描QR码

只需添加以下行gradle和使用下面的代码下面的代码

compile 'com.journeyapps:zxing-android-embedded:3.5.0' 

使用扫描QR码

IntentIntegrator integrator = new IntentIntegrator(this); 
    integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES); 
    integrator.setPrompt("Scan a barcode"); 
    integrator.setCameraId(0); // Use a specific camera of the device 
    integrator.setBeepEnabled(false); 
    integrator.setBarcodeImageEnabled(true); 
    integrator.initiateScan(); 

更多参考CLICK HERE

+0

我应该用这个替换zxingscanner代码吗? –

+0

是的研究,链接 – Anil

0

好像你AR当您完成处理扫描数据后,您的ZXingScannerView缺少对resumeCameraPreview(ZXingScannerView.ResultHandler resultHandler)的呼叫。为此,您需要通知您的活动在AsyncTask的onPostExecute方法的扫描器视图中调用该方法。既然你已经在一个单独的类中实现了AsyncTask,你将不得不实现某种类似于这样的回调机制:What is the best way for AsyncTask to notify parent Activity about completion?

+0

感谢您的信息:),我称之为“resumeCameraPreview(ZXingScannerView.ResultHandler resultHandler)”qrresult nd qrressultworker,但问题来了,它扫描3到4次,并打开3至4活动如果qrresult是真正 –