为什么我的if语句在我的其余代码之前运行?

时间:2019-07-17 15:34:12

标签: java android firebase

我试图识别条形码,并在成功识别条形码后对图像进行颜色分析。

所有图像处理工作正常,但是当我尝试输入if语句来中继成功或失败时,if语句将在其余代码之前运行。这将导致吐司弹出,并以“ null”代替应该存在的字符串。

我将使用***标识重要部分。

detector.detectInImage(image)
 .addOnSuccessListener(new OnSuccessListener < List < FirebaseVisionBarcode >> () {
   @Override
   public void onSuccess(List < FirebaseVisionBarcode > firebaseVisionBarcodes) {

    if (firebaseVisionBarcodes.isEmpty()) {
     Toast.makeText(Camera2.this, "No dressing recognised", Toast.LENGTH_SHORT).show();
     graphicOverlay.clear();
    } else {
     for (FirebaseVisionBarcode item: firebaseVisionBarcodes) {
      final String qrID = item.getRawValue();
      StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_SCAN_DRESSING,
       new Response.Listener < String > () {
        @Override
        public void onResponse(String response) {
          try {
           JSONObject jsonObject = new JSONObject(response);
           String success = jsonObject.getString("success");
           JSONArray jsonArray = jsonObject.getJSONArray("read");

           if (success.equals("1")) {
            for (int i = 0; i < jsonArray.length(); i++) {
             JSONObject object = jsonArray.getJSONObject(i);
             strPosition = object.getString("position").trim();
             strDate = object.getString("dateApplied").trim();
//*** The if statement is running before the above strings are given a value and therefore the toast's values are displayed as null?

            }
           } catch (JSONException e) {
            e.printStackTrace();
            Toast.makeText(Camera2.this, "Scan Error!" + e.toString(), Toast.LENGTH_SHORT).show();
           }
          }
         },
         new Response.ErrorListener() {
          @Override
          public void onErrorResponse(VolleyError error) {
           Toast.makeText(Camera2.this, "Scan Error!" + error.toString(), Toast.LENGTH_SHORT).show();
          }
         }) {
        @Override
        protected Map < String, String > getParams() throws AuthFailureError {
         Map < String, String > params = new HashMap < > ();
         params.put("dressingCode", qrID);
         params.put("patientID", getID);
         return params;
        }
       }; RequestQueue requestQueue = Volley.newRequestQueue(Camera2.this); requestQueue.add(stringRequest);



       Rect rectBound = item.getBoundingBox(); RectOverlay r1 = new RectOverlay(graphicOverlay, rectBound); graphicOverlay.add(r1);


       int xyz = bitmap.getHeight();
       //Centre coordinates of the QR code
       int centerX = item.getBoundingBox().centerX(); // x Coordinate of black square
       int centerY = item.getBoundingBox().centerY();

       //Height and width of the QR code
       int heightQR = item.getBoundingBox().height(); int widthQR = item.getBoundingBox().width();

       int y = heightQR / 2; int x = widthQR / 2;


       // x Coordinate of the blue square
       int blueSquareCoord = centerX - x;
       // x Coordinate of the red square
       int redSquareCoord = centerX + x;

       // y coordinate of all squares
       int yCoord = (centerY + (3 * y));


       // RGB values of the pixels at the blue square
       int red1 = Color.red(bitmap.getPixel(blueSquareCoord, yCoord)); int green1 = Color.green(bitmap.getPixel(blueSquareCoord, yCoord)); int blue1 = Color.blue(bitmap.getPixel(blueSquareCoord, yCoord));

       // RGB values of the pixels at the black square
       int red2 = Color.red(bitmap.getPixel(centerX, yCoord)); int green2 = Color.green(bitmap.getPixel(centerX, yCoord)); int blue2 = Color.blue(bitmap.getPixel(centerX, yCoord));

       // RGB values of the pixels at the red square
       int red3 = Color.red(bitmap.getPixel(redSquareCoord, yCoord)); int green3 = Color.green(bitmap.getPixel(redSquareCoord, yCoord)); int blue3 = Color.blue(bitmap.getPixel(redSquareCoord, yCoord));

       boolean blue = (red1 < 17) && (green1 > 50 && green1 < 124) && (blue1 > 150); boolean black = (red2 < 33) && (green2 < 33) && (blue2 < 33); boolean red = (red3 > 100) && (green3 < 35) && (blue3 < 35); boolean green = (red3 < 17) && (blue3 > 50 && blue3 < 124) && (green3 > 150);

       if (blue == true && black == true && red == true) {
//*** This toast is called before the strings highlighted above have been set for some reason, and this causes the toast to display 'null' in the place of each of the values..
        Toast.makeText(Camera2.this, "Dressing Position: " + strPosition + "\nDressing Added: " + strDate + "\nScan Good!", Toast.LENGTH_LONG).show();
        new Handler().postDelayed(new Runnable() {
         @Override
         public void run() {

          Intent i = new Intent(Camera2.this, Home.class);
          startActivity(i);
         }
        }, 10000);
       } else if (blue == true && black == true && green == true) {
        Toast.makeText(Camera2.this, "Dressing Position: " + strPosition + "\nDressing Added: " + strDate + "\nPlease contact your physician", Toast.LENGTH_LONG).show();

       }
      }
     }
    }

   }


  )
  .addOnFailureListener(new OnFailureListener() {
   @Override
   public void onFailure(@NonNull Exception e) {
    Toast.makeText(Camera2.this, "No dressing recognised", Toast.LENGTH_SHORT).show();
    Log.i("INFO", "FAIL");

   }
  });

0 个答案:

没有答案