我正在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);
}
}
我應該用這個替換zxingscanner代碼嗎? –
是的研究,鏈接 – Anil