2017-06-29 52 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是真正 –