Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class QRActivity extends Activity {
- private CameraSource cameraSource;
- private SurfaceView cameraView;
- private final int MY_PERMISSIONS_REQUEST_CAMERA = 1;
- private String token = "";
- private String tokenanterior = "";
- String piezas ="";
- String hora ="";
- String op ="";
- private String[] opciones = new String[]{"Ninguna", "1", "2", "3", "4", "5", "más de 5"};
- private AlertDialog.Builder dialogo;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_qr);
- cameraView = (SurfaceView) findViewById(R.id.camera_view);
- initQR();
- }
- public void initQR() {
- // creo el detector qr
- BarcodeDetector barcodeDetector =
- new BarcodeDetector.Builder(this)
- .setBarcodeFormats(Barcode.ALL_FORMATS)
- .build();
- // creo la camara
- cameraSource = new CameraSource
- .Builder(this, barcodeDetector)
- .setRequestedPreviewSize(1600, 1024)
- .setAutoFocusEnabled(true) //you should add this feature
- .build();
- // listener de ciclo de vida de la camara
- cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
- // verifico si el usuario dio los permisos para la camara
- if (ActivityCompat.checkSelfPermission(QRActivity.this, Manifest.permission.CAMERA)
- != PackageManager.PERMISSION_GRANTED) {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- // verificamos la version de ANdroid que sea al menos la M para mostrar
- // el dialog de la solicitud de la camara
- if (shouldShowRequestPermissionRationale(
- Manifest.permission.CAMERA)) ;
- requestPermissions(new String[]{Manifest.permission.CAMERA},
- MY_PERMISSIONS_REQUEST_CAMERA);
- }
- return;
- } else {
- try {
- cameraSource.start(cameraView.getHolder());
- } catch (IOException ie) {
- Log.e("CAMERA SOURCE", ie.getMessage());
- }
- }
- }
- @Override
- public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
- }
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {
- cameraSource.stop();
- }
- });
- // preparo el detector de QR
- barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
- @Override
- public void release() {
- }
- @Override
- public void receiveDetections(Detector.Detections<Barcode> detections) {
- final SparseArray<Barcode> barcodes = detections.getDetectedItems();
- if (barcodes.size() > 0) {
- // obtenemos el token
- token = barcodes.valueAt(0).displayValue.toString();
- // verificamos que el token anterior no se igual al actual
- // esto es util para evitar multiples llamadas empleando el mismo token
- if (!token.equals(tokenanterior)) {
- // guardamos el ultimo token proceado
- tokenanterior = token;
- Log.i("token", token);
- if (URLUtil.isValidUrl(token)) {
- // si es una URL valida abre el navegador
- /*
- Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(token));
- startActivity(browserIntent);*/
- //AsyncTask con POST
- dialogo = new AlertDialog.Builder(QRActivity.this);
- asignarHora();
- }
- /*else {
- // comparte en otras apps
- Intent shareIntent = new Intent();
- shareIntent.setAction(Intent.ACTION_SEND);
- shareIntent.putExtra(Intent.EXTRA_TEXT, token);
- shareIntent.setType("text/plain");
- startActivity(shareIntent);
- }*/
- new Thread(new Runnable() {
- public void run() {
- try {
- synchronized (this) {
- wait(5000);
- // limpiamos el token
- tokenanterior = "";
- }
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- Log.e("Error", "Waiting didnt work!!");
- e.printStackTrace();
- }
- }
- }).start();
- }
- }
- }
- });
- }
- public void asignarHora()
- {
- QRActivity.AsignarHoraAsyncTask aH = new QRActivity.AsignarHoraAsyncTask();
- aH.execute(token);
- }
- class AsignarHoraAsyncTask extends AsyncTask<String, Void, String>
- {
- @Override
- protected String doInBackground(String... urls)
- {
- return POST(token);
- }
- // onPostExecute displays the results of the AsyncTask.
- protected void onPreExecute() {
- super.onPreExecute();
- op = "embalado";
- obtenerHora();
- dialogo.setCancelable(true);
- dialogo.setTitle("¿Cuántas piezas van sueltas?");
- dialogo.setItems(opciones, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int item) {
- if (item == 0) {
- piezas = "0";
- asignarHora();
- }
- if (item == 1) {
- piezas = "1";
- asignarHora();
- }
- if (item == 2) {
- piezas = "2";
- asignarHora();
- }
- if (item == 3) {
- piezas = "3";
- asignarHora();
- }
- if (item == 4) {
- piezas = "4";
- asignarHora();
- }
- if (item == 5) {
- piezas = "5";
- asignarHora();
- }
- if (item == 6) {
- piezas = "5+";
- asignarHora();
- }
- }
- });
- dialogo.setCancelable(true);
- dialogo.create();
- dialogo.show();
- }
- @Override
- protected void onPostExecute(String result)
- {
- finish();
- }
- public String POST(String url)
- {
- InputStream inputStream = null;
- String result = "";
- try
- {
- HttpClient httpclient = new DefaultHttpClient();
- //make POST request to the given URL
- HttpPost httpPost = new HttpPost(url);
- // pass parameters in this way
- List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
- nameValuePairs.add(new BasicNameValuePair("op",op));
- nameValuePairs.add(new BasicNameValuePair("hEnt",hora));
- nameValuePairs.add(new BasicNameValuePair("piezas",piezas));
- Log.i("Asyn", "referencia:"+ "|| op:" + op + "||piezas:" + piezas + "||hR:" + hora);
- //add data
- httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
- // 8. Execute POST request to the given URL
- HttpResponse httpResponse = httpclient.execute(httpPost);
- // 9. receive response as inputStream
- inputStream = httpResponse.getEntity().getContent();
- // 10. convert inputstream to string
- if(inputStream != null)
- {
- result = convertInputStreamToString(inputStream);
- Log.i("Asyn", "result: " + result);
- }
- //Si pasa por aquí se va a tomar x el culo mirar!!
- else
- {
- result = "Did not work!";
- }
- }
- catch (Exception e)
- {
- //Log.d("InputStream", e.getLocalizedMessage());
- }
- return result;
- }
- }
- private String convertInputStreamToString(InputStream inputStream) throws IOException {
- // TODO Auto-generated method stub
- BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
- String line = "";
- String result = "";
- while((line = bufferedReader.readLine()) != null)
- result += line;
- inputStream.close();
- return result;
- }
- public void obtenerHora()
- {
- Calendar c = new GregorianCalendar(); // This creates a Calendar instance with the current time
- int mHour = c.get(Calendar.HOUR_OF_DAY);
- int mMinute = c.get(Calendar.MINUTE);
- String minuto = String.valueOf(mMinute);
- String mhora = String.valueOf(mHour);
- if (minuto.length()<2)
- {
- minuto = "0"+ minuto;
- }
- if (mhora.length()<2)
- {
- mhora = "0"+ mhora;
- }
- hora = mhora+"-"+minuto;
- }
- }
Add Comment
Please, Sign In to add comment