Guest User

Untitled

a guest
Oct 17th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.07 KB | None | 0 0
  1. public class QRActivity extends Activity {
  2.  
  3. private CameraSource cameraSource;
  4. private SurfaceView cameraView;
  5. private final int MY_PERMISSIONS_REQUEST_CAMERA = 1;
  6. private String token = "";
  7. private String tokenanterior = "";
  8.  
  9. String piezas ="";
  10. String hora ="";
  11. String op ="";
  12.  
  13. private String[] opciones = new String[]{"Ninguna", "1", "2", "3", "4", "5", "más de 5"};
  14. private AlertDialog.Builder dialogo;
  15.  
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.activity_qr);
  20.  
  21. cameraView = (SurfaceView) findViewById(R.id.camera_view);
  22. initQR();
  23. }
  24.  
  25. public void initQR() {
  26.  
  27. // creo el detector qr
  28. BarcodeDetector barcodeDetector =
  29. new BarcodeDetector.Builder(this)
  30. .setBarcodeFormats(Barcode.ALL_FORMATS)
  31. .build();
  32.  
  33. // creo la camara
  34. cameraSource = new CameraSource
  35. .Builder(this, barcodeDetector)
  36. .setRequestedPreviewSize(1600, 1024)
  37. .setAutoFocusEnabled(true) //you should add this feature
  38. .build();
  39.  
  40. // listener de ciclo de vida de la camara
  41. cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
  42. @Override
  43. public void surfaceCreated(SurfaceHolder holder) {
  44.  
  45. // verifico si el usuario dio los permisos para la camara
  46. if (ActivityCompat.checkSelfPermission(QRActivity.this, Manifest.permission.CAMERA)
  47. != PackageManager.PERMISSION_GRANTED) {
  48.  
  49. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  50. // verificamos la version de ANdroid que sea al menos la M para mostrar
  51. // el dialog de la solicitud de la camara
  52. if (shouldShowRequestPermissionRationale(
  53. Manifest.permission.CAMERA)) ;
  54. requestPermissions(new String[]{Manifest.permission.CAMERA},
  55. MY_PERMISSIONS_REQUEST_CAMERA);
  56. }
  57. return;
  58. } else {
  59. try {
  60. cameraSource.start(cameraView.getHolder());
  61. } catch (IOException ie) {
  62. Log.e("CAMERA SOURCE", ie.getMessage());
  63. }
  64. }
  65. }
  66.  
  67. @Override
  68. public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
  69. }
  70.  
  71. @Override
  72. public void surfaceDestroyed(SurfaceHolder holder) {
  73. cameraSource.stop();
  74. }
  75. });
  76.  
  77. // preparo el detector de QR
  78. barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
  79. @Override
  80. public void release() {
  81. }
  82.  
  83.  
  84. @Override
  85. public void receiveDetections(Detector.Detections<Barcode> detections) {
  86. final SparseArray<Barcode> barcodes = detections.getDetectedItems();
  87.  
  88. if (barcodes.size() > 0) {
  89.  
  90. // obtenemos el token
  91. token = barcodes.valueAt(0).displayValue.toString();
  92.  
  93. // verificamos que el token anterior no se igual al actual
  94. // esto es util para evitar multiples llamadas empleando el mismo token
  95. if (!token.equals(tokenanterior)) {
  96.  
  97. // guardamos el ultimo token proceado
  98. tokenanterior = token;
  99. Log.i("token", token);
  100.  
  101. if (URLUtil.isValidUrl(token)) {
  102. // si es una URL valida abre el navegador
  103.  
  104. /*
  105. Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(token));
  106. startActivity(browserIntent);*/
  107.  
  108. //AsyncTask con POST
  109. dialogo = new AlertDialog.Builder(QRActivity.this);
  110. asignarHora();
  111.  
  112. }
  113. /*else {
  114. // comparte en otras apps
  115. Intent shareIntent = new Intent();
  116. shareIntent.setAction(Intent.ACTION_SEND);
  117. shareIntent.putExtra(Intent.EXTRA_TEXT, token);
  118. shareIntent.setType("text/plain");
  119. startActivity(shareIntent);
  120. }*/
  121.  
  122. new Thread(new Runnable() {
  123. public void run() {
  124. try {
  125. synchronized (this) {
  126. wait(5000);
  127. // limpiamos el token
  128. tokenanterior = "";
  129. }
  130. } catch (InterruptedException e) {
  131. // TODO Auto-generated catch block
  132. Log.e("Error", "Waiting didnt work!!");
  133. e.printStackTrace();
  134. }
  135. }
  136. }).start();
  137.  
  138. }
  139. }
  140. }
  141. });
  142.  
  143. }
  144.  
  145. public void asignarHora()
  146. {
  147. QRActivity.AsignarHoraAsyncTask aH = new QRActivity.AsignarHoraAsyncTask();
  148. aH.execute(token);
  149. }
  150.  
  151. class AsignarHoraAsyncTask extends AsyncTask<String, Void, String>
  152. {
  153. @Override
  154. protected String doInBackground(String... urls)
  155. {
  156. return POST(token);
  157. }
  158. // onPostExecute displays the results of the AsyncTask.
  159.  
  160. protected void onPreExecute() {
  161. super.onPreExecute();
  162.  
  163. op = "embalado";
  164. obtenerHora();
  165. dialogo.setCancelable(true);
  166. dialogo.setTitle("¿Cuántas piezas van sueltas?");
  167. dialogo.setItems(opciones, new DialogInterface.OnClickListener() {
  168. public void onClick(DialogInterface dialog, int item) {
  169.  
  170. if (item == 0) {
  171. piezas = "0";
  172. asignarHora();
  173. }
  174. if (item == 1) {
  175. piezas = "1";
  176. asignarHora();
  177. }
  178. if (item == 2) {
  179. piezas = "2";
  180. asignarHora();
  181. }
  182. if (item == 3) {
  183. piezas = "3";
  184. asignarHora();
  185. }
  186. if (item == 4) {
  187. piezas = "4";
  188. asignarHora();
  189. }
  190. if (item == 5) {
  191. piezas = "5";
  192. asignarHora();
  193. }
  194. if (item == 6) {
  195. piezas = "5+";
  196. asignarHora();
  197. }
  198. }
  199.  
  200.  
  201. });
  202.  
  203. dialogo.setCancelable(true);
  204. dialogo.create();
  205. dialogo.show();
  206. }
  207. @Override
  208. protected void onPostExecute(String result)
  209. {
  210. finish();
  211. }
  212.  
  213. public String POST(String url)
  214. {
  215. InputStream inputStream = null;
  216. String result = "";
  217. try
  218. {
  219. HttpClient httpclient = new DefaultHttpClient();
  220. //make POST request to the given URL
  221. HttpPost httpPost = new HttpPost(url);
  222. // pass parameters in this way
  223.  
  224. List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
  225. nameValuePairs.add(new BasicNameValuePair("op",op));
  226. nameValuePairs.add(new BasicNameValuePair("hEnt",hora));
  227. nameValuePairs.add(new BasicNameValuePair("piezas",piezas));
  228. Log.i("Asyn", "referencia:"+ "|| op:" + op + "||piezas:" + piezas + "||hR:" + hora);
  229.  
  230. //add data
  231. httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
  232.  
  233. // 8. Execute POST request to the given URL
  234. HttpResponse httpResponse = httpclient.execute(httpPost);
  235.  
  236. // 9. receive response as inputStream
  237. inputStream = httpResponse.getEntity().getContent();
  238.  
  239. // 10. convert inputstream to string
  240. if(inputStream != null)
  241. {
  242. result = convertInputStreamToString(inputStream);
  243.  
  244. Log.i("Asyn", "result: " + result);
  245.  
  246. }
  247.  
  248. //Si pasa por aquí se va a tomar x el culo mirar!!
  249. else
  250. {
  251. result = "Did not work!";
  252. }
  253.  
  254. }
  255.  
  256. catch (Exception e)
  257. {
  258. //Log.d("InputStream", e.getLocalizedMessage());
  259. }
  260. return result;
  261. }
  262. }
  263.  
  264. private String convertInputStreamToString(InputStream inputStream) throws IOException {
  265. // TODO Auto-generated method stub
  266.  
  267. BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
  268. String line = "";
  269. String result = "";
  270. while((line = bufferedReader.readLine()) != null)
  271. result += line;
  272.  
  273. inputStream.close();
  274. return result;
  275. }
  276.  
  277. public void obtenerHora()
  278. {
  279. Calendar c = new GregorianCalendar(); // This creates a Calendar instance with the current time
  280. int mHour = c.get(Calendar.HOUR_OF_DAY);
  281. int mMinute = c.get(Calendar.MINUTE);
  282. String minuto = String.valueOf(mMinute);
  283. String mhora = String.valueOf(mHour);
  284. if (minuto.length()<2)
  285. {
  286. minuto = "0"+ minuto;
  287. }
  288. if (mhora.length()<2)
  289. {
  290. mhora = "0"+ mhora;
  291. }
  292.  
  293.  
  294. hora = mhora+"-"+minuto;
  295. }
  296.  
  297. }
Add Comment
Please, Sign In to add comment