Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.rmp.app1admin;
- import android.app.Activity;
- import android.app.ProgressDialog;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.pm.PackageManager;
- import android.net.Uri;
- import android.os.AsyncTask;
- import android.os.Build;
- import android.os.Bundle;
- import android.support.v4.app.ActivityCompat;
- import android.support.v4.content.ContextCompat;
- import android.support.v7.app.AlertDialog;
- import android.util.Log;
- import android.widget.Toast;
- import com.google.zxing.Result;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.OutputStreamWriter;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import me.dm7.barcodescanner.zxing.ZXingScannerView;
- import static android.Manifest.permission.CAMERA;
- public class QRCodeScannerActivity extends Activity implements ZXingScannerView.ResultHandler
- {
- private static final int REQUEST_CAMERA = 1;
- private ZXingScannerView mScannerView;
- public static final int CONNECTION_TIMEOUT=10000;
- public static final int READ_TIMEOUT=15000;
- public void onCreate(Bundle savedinstancestate)
- {
- super.onCreate(savedinstancestate);
- mScannerView = new ZXingScannerView(this);
- setContentView(mScannerView);
- int currentapiVersion = Build.VERSION.SDK_INT;
- if (currentapiVersion >= Build.VERSION_CODES.M)
- {
- if (checkPermission())
- {
- Toast.makeText(getApplicationContext(), "Permission already granted", Toast.LENGTH_LONG).show();
- }
- else
- {
- requestPermission();
- }
- }
- }
- private boolean checkPermission()
- {
- return ( ContextCompat.checkSelfPermission(getApplicationContext(), CAMERA ) == PackageManager.PERMISSION_GRANTED);
- }
- private void requestPermission()
- {
- ActivityCompat.requestPermissions(this, new String[]{CAMERA}, REQUEST_CAMERA);
- }
- public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)
- {
- switch (requestCode)
- {
- case REQUEST_CAMERA:
- if (grantResults.length > 0)
- {
- boolean cameraAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
- if (cameraAccepted)
- {
- Toast.makeText(getApplicationContext(), "Permission Granted, Now you can access camera", Toast.LENGTH_LONG).show();
- }
- else
- {
- Toast.makeText(getApplicationContext(), "Permission Denied, You cannot access and camera", Toast.LENGTH_LONG).show();
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- if (shouldShowRequestPermissionRationale(CAMERA)) {
- showMessageOKCancel("You need to allow access to both the permissions",
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- requestPermissions(new String[]{CAMERA},
- REQUEST_CAMERA);
- }
- }
- });
- return;
- }
- }
- }
- }
- break;
- }
- }
- private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener)
- {
- new android.support.v7.app.AlertDialog.Builder(QRCodeScannerActivity.this)
- .setMessage(message)
- .setPositiveButton("OK", okListener)
- .setNegativeButton("Cancel", null)
- .create()
- .show();
- }
- @Override
- public void onResume()
- {
- super.onResume();
- int currentapiVersion = Build.VERSION.SDK_INT;
- if (currentapiVersion >= Build.VERSION_CODES.M) {
- if (checkPermission()) {
- if(mScannerView == null) {
- mScannerView = new ZXingScannerView(this);
- setContentView(mScannerView);
- }
- mScannerView.setResultHandler(this);
- mScannerView.startCamera();
- } else {
- requestPermission();
- }
- }
- }
- @Override
- public void onDestroy()
- {
- super.onDestroy();
- mScannerView.stopCamera();
- }
- @Override
- public void handleResult(Result rawResult)
- {
- final String result = rawResult.getText();
- Log.d("QRCodeScanner", rawResult.getText());
- Log.d("QRCodeScanner", rawResult.getBarcodeFormat().toString());
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setTitle("Scan Result");
- builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- mScannerView.resumeCameraPreview(QRCodeScannerActivity.this);
- }
- });
- builder.setNeutralButton("Verify", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which)
- {
- new AsyncVerifyParking().execute(result);
- }
- });
- builder.setMessage(rawResult.getText());
- AlertDialog alert1 = builder.create();
- alert1.show();
- }
- private class AsyncVerifyParking extends AsyncTask<String, String, String>
- {
- ProgressDialog pdLoading = new ProgressDialog(QRCodeScannerActivity.this);
- HttpURLConnection conn;
- URL url = null;
- @Override
- protected void onPreExecute()
- {
- super.onPreExecute();
- pdLoading.setMessage("\tLoading...");
- pdLoading.setCancelable(false);
- pdLoading.show();
- }
- @Override
- protected String doInBackground(String... params)
- {
- try
- {
- String address = getString (R.string.serveraddress);
- url = new URL("http://"+address+"/PDRM_WEB/verifyparking.php");
- }
- catch (MalformedURLException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return "exception";
- }
- try
- {
- conn = (HttpURLConnection)url.openConnection();
- conn.setReadTimeout(READ_TIMEOUT);
- conn.setConnectTimeout(CONNECTION_TIMEOUT);
- conn.setRequestMethod("POST");
- conn.setDoInput(true);
- conn.setDoOutput(true);
- Uri.Builder builder = new Uri.Builder()
- .appendQueryParameter("qrcode", params[0]);
- String query = builder.build().getEncodedQuery();
- OutputStream os = conn.getOutputStream();
- BufferedWriter writer = new BufferedWriter(
- new OutputStreamWriter(os, "UTF-8"));
- writer.write(query);
- writer.flush();
- writer.close();
- os.close();
- conn.connect();
- }
- catch (IOException e1)
- {
- e1.printStackTrace();
- return "exception";
- }
- try
- {
- int response_code = conn.getResponseCode();
- if (response_code == HttpURLConnection.HTTP_OK)
- {
- InputStream input = conn.getInputStream();
- BufferedReader reader = new BufferedReader(new InputStreamReader(input));
- StringBuilder result = new StringBuilder();
- String line;
- while ((line = reader.readLine()) != null)
- {
- result.append(line);
- }
- return(result.toString());
- }
- else
- {
- return("unsuccessful");
- }
- } catch (IOException e)
- {
- e.printStackTrace();
- return "exception";
- }
- finally {
- conn.disconnect();
- }
- }
- @Override
- protected void onPostExecute(String result)
- {
- pdLoading.dismiss();
- if(result.equalsIgnoreCase("true"))
- {
- Intent intent = new Intent(QRCodeScannerActivity.this,ScanResultActivity.class);
- intent.putExtra("scanresult",1);
- startActivity(intent);
- }
- else if (result.equalsIgnoreCase("false"))
- {
- Intent intent = new Intent(QRCodeScannerActivity.this,ScanResultActivity.class);
- intent.putExtra("scanresult",0);
- startActivity(intent);
- }
- else if (result.equalsIgnoreCase("exception") || result.equalsIgnoreCase("unsuccessful"))
- {
- Intent intent = new Intent(QRCodeScannerActivity.this,ScanResultActivity.class);
- intent.putExtra("scanresult",0);
- startActivity(intent);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement