我正在制作基于QR扫描程序的Android应用程序,它从QR码获取数据并发送到服务器检查数据,但我的扫描仪在扫描无效的QR码后卡住了
这是我的代码
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 :(得分: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
答案 1 :(得分:0)
在处理完扫描数据后,您似乎错过了resumeCameraPreview(ZXingScannerView.ResultHandler resultHandler)
上ZXingScannerView
的来电。为此,您需要通知您的Activity在AsyncTask的onPostExecute
方法中的扫描程序视图上调用该方法。由于您已在单独的类中实现了AsyncTask,因此您将不得不实现某种类似于以下内容的回调机制:What is the best way for AsyncTask to notify parent Activity about completion?。