From a4c92fee604cc850014d3e42cc4d40f347c0a6e6 Mon Sep 17 00:00:00 2001 From: Brython Caley-Davies Date: Sun, 9 Feb 2020 06:21:50 +0000 Subject: [PATCH] Improving azure ui --- app/src/main/java/com/kill/bill/Item.java | 22 +- .../main/java/com/kill/bill/ItemAdapter.java | 11 +- app/src/main/java/com/kill/bill/Splash.java | 2 +- .../java/com/kill/bill/TestAzureActivity.java | 391 +++++++++--------- app/src/main/res/layout/activity_splash.xml | 18 +- .../main/res/layout/activity_test_azure.xml | 32 +- app/src/main/res/menu/action_bar_menu.xml | 8 + app/src/main/res/values/strings.xml | 3 + app/src/main/res/values/styles.xml | 2 +- 9 files changed, 258 insertions(+), 231 deletions(-) create mode 100644 app/src/main/res/menu/action_bar_menu.xml diff --git a/app/src/main/java/com/kill/bill/Item.java b/app/src/main/java/com/kill/bill/Item.java index 1713753..0da6302 100644 --- a/app/src/main/java/com/kill/bill/Item.java +++ b/app/src/main/java/com/kill/bill/Item.java @@ -3,7 +3,6 @@ import android.os.Parcel; import android.os.Parcelable; -@SuppressWarnings("unused") public class Item implements Parcelable { private String name; private double price; @@ -19,17 +18,18 @@ protected Item(Parcel in) { quantity = in.readInt(); } - public static final Creator CREATOR = new Creator() { - @Override - public Item createFromParcel(Parcel in) { - return new Item(in); - } + public static final Creator CREATOR = + new Creator() { + @Override + public Item createFromParcel(Parcel in) { + return new Item(in); + } - @Override - public Item[] newArray(int size) { - return new Item[size]; - } - }; + @Override + public Item[] newArray(int size) { + return new Item[size]; + } + }; public String getName() { return name; diff --git a/app/src/main/java/com/kill/bill/ItemAdapter.java b/app/src/main/java/com/kill/bill/ItemAdapter.java index 6aa8a93..9d6c5af 100644 --- a/app/src/main/java/com/kill/bill/ItemAdapter.java +++ b/app/src/main/java/com/kill/bill/ItemAdapter.java @@ -98,11 +98,12 @@ public View getView(int position, View convertView, @NonNull ViewGroup parent) { alertDialog.show(); }); - convertView.setOnClickListener(o -> { - Intent intent = new Intent(parent.getContext(), TransactionDetails.class); - intent.putExtra("item", item); - parent.getContext().startActivity(intent); - }); + convertView.setOnClickListener( + o -> { + Intent intent = new Intent(parent.getContext(), TransactionDetails.class); + intent.putExtra("item", item); + parent.getContext().startActivity(intent); + }); return convertView; } diff --git a/app/src/main/java/com/kill/bill/Splash.java b/app/src/main/java/com/kill/bill/Splash.java index 99a3a52..70595fb 100644 --- a/app/src/main/java/com/kill/bill/Splash.java +++ b/app/src/main/java/com/kill/bill/Splash.java @@ -17,7 +17,7 @@ protected void onCreate(Bundle savedInstanceState) { WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_splash); - ImageView loading = findViewById(R.id.imageViewReceipt); + ImageView loading = findViewById(R.id.image_view_receipt); loading.setImageResource(R.drawable.loading); //loading.setBackgroundResource(R.drawable.loading); loadingAnimation = (AnimationDrawable) loading.getDrawable(); diff --git a/app/src/main/java/com/kill/bill/TestAzureActivity.java b/app/src/main/java/com/kill/bill/TestAzureActivity.java index 4380017..68a92a7 100644 --- a/app/src/main/java/com/kill/bill/TestAzureActivity.java +++ b/app/src/main/java/com/kill/bill/TestAzureActivity.java @@ -9,11 +9,13 @@ import android.os.Bundle; import android.os.StrictMode; import android.util.Log; -import android.view.View; +import android.view.Menu; +import android.view.MenuInflater; import android.widget.Button; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.Toolbar; import com.google.android.gms.tasks.Tasks; import com.google.firebase.storage.FirebaseStorage; @@ -39,258 +41,265 @@ public class TestAzureActivity extends AppCompatActivity { - private static final String subscriptionKey = "87e56323493740c7a179825fa8cfc9ed"; - private static final String endpoint = "https://killbillcomputervision.cognitiveservices.azure.com/"; + private static final String subscriptionKey = "87e56323493740c7a179825fa8cfc9ed"; + private static final String endpoint = + "https://killbillcomputervision.cognitiveservices.azure.com/"; - private static final int REQUEST_IMAGE_CAPTURE = 1; + private static final int REQUEST_IMAGE_CAPTURE = 1; - private static Uri imageToAnalyze; + private static Uri imageToAnalyze; - private static final String readURI = endpoint + "vision/v2.1/read/core/asyncBatchAnalyze"; + private static final String readURI = endpoint + "vision/v2.1/read/core/asyncBatchAnalyze"; - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_test_azure); - - Log.e("SUBKEY", "" + subscriptionKey); - - Button takeImageButton = findViewById(R.id.take_picture_button); - takeImageButton.setOnClickListener( - view -> { - StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder(); - StrictMode.setVmPolicy(builder.build()); - Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); - this.startActivityForResult(intent, REQUEST_IMAGE_CAPTURE); - }); - - Button goToPayload = findViewById(R.id.go_to_payload); - goToPayload.setOnClickListener( - view -> { - Intent intent = new Intent(view.getContext(), PayLoadParser.class); - startActivity(intent); - }); - } + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_test_azure); - public void onClick2(View v) { - startActivity(new Intent(this, TransactionList.class)); - } + Toolbar toolbar = findViewById(R.id.azure_toolbar); + setSupportActionBar(toolbar); + Log.e("SUBKEY", "" + subscriptionKey); - @Override - protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { - super.onActivityResult(requestCode, resultCode, data); + Button takeImageButton = findViewById(R.id.take_picture_button); + takeImageButton.setOnClickListener( + view -> { + StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder(); + StrictMode.setVmPolicy(builder.build()); + Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); + this.startActivityForResult(intent, REQUEST_IMAGE_CAPTURE); + }); - if (requestCode == REQUEST_IMAGE_CAPTURE) { - Bitmap bm; + Button goToPayload = findViewById(R.id.go_to_payload); + goToPayload.setOnClickListener( + view -> { + Intent intent = new Intent(view.getContext(), PayLoadParser.class); + startActivity(intent); + }); + } - if (data == null || data.getExtras() == null) { - return; - } + @Override + protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { + super.onActivityResult(requestCode, resultCode, data); - bm = (Bitmap) Objects.requireNonNull(data.getExtras()).get("data"); + if (requestCode == REQUEST_IMAGE_CAPTURE) { + Bitmap bm; - //((ImageView) findViewById(R.id.image)).setImageBitmap(bm); + if (data == null || data.getExtras() == null) { + return; + } - SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss", Locale.ENGLISH); + bm = (Bitmap) Objects.requireNonNull(data.getExtras()).get("data"); - imageToAnalyze = Uri.parse(sdf.format(Calendar.getInstance().getTime())); + // ((ImageView) findViewById(R.id.image)).setImageBitmap(bm); - File file; - String prefix = imageToAnalyze.toString(); + SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss", Locale.ENGLISH); - try { - File outputDir = TestAzureActivity.this.getCacheDir(); // context being the Activity pointer - file = File.createTempFile(prefix, "jpg", outputDir); - } catch (IOException e) { - e.printStackTrace(); - return; - } + imageToAnalyze = Uri.parse(sdf.format(Calendar.getInstance().getTime())); - FirebaseStorage storage = FirebaseStorage.getInstance(); - StorageReference storageReference = storage.getReference(); - StorageReference imageRef = - storageReference.child("scanned/" + imageToAnalyze.toString() + ".jpg"); + File file; + String prefix = imageToAnalyze.toString(); - ByteArrayOutputStream stream = new ByteArrayOutputStream(); + try { + File outputDir = TestAzureActivity.this.getCacheDir(); // context being the Activity pointer - if (bm == null) { - return; - } + file = File.createTempFile(prefix, "jpg", outputDir); + } catch (IOException e) { + e.printStackTrace(); + return; + } - bm.compress(Bitmap.CompressFormat.PNG, 100, stream); - byte[] bytes = stream.toByteArray(); + FirebaseStorage storage = FirebaseStorage.getInstance(); + StorageReference storageReference = storage.getReference(); + StorageReference imageRef = + storageReference.child("scanned/" + imageToAnalyze.toString() + ".jpg"); - try { - FileOutputStream fos = new FileOutputStream(file); - fos.write(bytes); - } catch (IOException e) { - e.printStackTrace(); - } + ByteArrayOutputStream stream = new ByteArrayOutputStream(); - UploadTask task = imageRef.putFile(Uri.fromFile(file)); + if (bm == null) { + return; + } - task.addOnFailureListener(o -> System.out.println("Failure")) - .addOnSuccessListener(o -> { - System.out.println("Success"); + bm.compress(Bitmap.CompressFormat.PNG, 100, stream); + byte[] bytes = stream.toByteArray(); - AsyncTask.execute(() -> { - try { - Uri url = Tasks.await(imageRef.getDownloadUrl()); + try { + FileOutputStream fos = new FileOutputStream(file); + fos.write(bytes); + } catch (IOException e) { + e.printStackTrace(); + } - Log.e("imageRefURI", "" + url.toString()); + UploadTask task = imageRef.putFile(Uri.fromFile(file)); + task.addOnFailureListener(o -> System.out.println("Failure")) + .addOnSuccessListener( + o -> { + System.out.println("Success"); - new GetImageText(TestAzureActivity.this).execute(readURI, url.toString(), null, ""); + AsyncTask.execute( + () -> { + try { + Uri url = Tasks.await(imageRef.getDownloadUrl()); - } catch (ExecutionException | InterruptedException e) { - e.printStackTrace(); - } - }); + Log.e("imageRefURI", "" + url.toString()); + new GetImageText(TestAzureActivity.this) + .execute(readURI, url.toString(), null, ""); + } catch (ExecutionException | InterruptedException e) { + e.printStackTrace(); + } }); - } + }); } + } + @Override + public boolean onCreateOptionsMenu(Menu menu) { + MenuInflater inflater = getMenuInflater(); - private static class GetImageText extends AsyncTask { - private final ProgressDialog dialog; - - public GetImageText(Activity activity) { - this.dialog = new ProgressDialog(activity); - } - - @Override - protected String doInBackground(String... strings) { - try { - URL readURI = new URL(strings[0]); - HttpsURLConnection readConnection = (HttpsURLConnection) readURI.openConnection(); - - imageToAnalyze = Uri.parse(strings[1]); - - String myData = "{\"url\":\"" + imageToAnalyze + "\"}"; - - readConnection.setRequestMethod("POST"); - readConnection.setRequestProperty("Content-Type", "application/json"); - readConnection.setRequestProperty("Ocp-Apim-Subscription-Key", subscriptionKey); + inflater.inflate(R.menu.action_bar_menu, menu); - // Enable writing - readConnection.setDoOutput(true); + return true; + } - // Write the data - readConnection.getOutputStream().write(myData.getBytes()); + private static class GetImageText extends AsyncTask { + private final ProgressDialog dialog; - String outputEndpoint = ""; + public GetImageText(Activity activity) { + this.dialog = new ProgressDialog(activity); + } - int response = readConnection.getResponseCode(); - if (response == 200) { - Log.d("CHECK", "200"); - } else if (response == 202) { - Log.d("CHECK", "202"); + @Override + protected String doInBackground(String... strings) { + try { + URL readURI = new URL(strings[0]); + HttpsURLConnection readConnection = (HttpsURLConnection) readURI.openConnection(); - outputEndpoint = readConnection.getHeaderField("Operation-Location"); + imageToAnalyze = Uri.parse(strings[1]); - } else { - // Error handling code goes here - Log.e("CHECK", "Failed with " + response); - Log.e("CHECK", "Failed with " + readConnection); - } + String myData = "{\"url\":\"" + imageToAnalyze + "\"}"; - Log.d("CHECK", outputEndpoint); + readConnection.setRequestMethod("POST"); + readConnection.setRequestProperty("Content-Type", "application/json"); + readConnection.setRequestProperty("Ocp-Apim-Subscription-Key", subscriptionKey); + // Enable writing + readConnection.setDoOutput(true); - URL jsonURI = new URL(outputEndpoint); - HttpsURLConnection jsonConnection = (HttpsURLConnection) jsonURI.openConnection(); + // Write the data + readConnection.getOutputStream().write(myData.getBytes()); - jsonConnection.setRequestMethod("GET"); + String outputEndpoint = ""; - jsonConnection.setRequestProperty("Ocp-Apim-Subscription-Key", subscriptionKey); + int response = readConnection.getResponseCode(); + if (response == 200) { + Log.d("CHECK", "200"); + } else if (response == 202) { + Log.d("CHECK", "202"); - response = jsonConnection.getResponseCode(); - if (response == 200) { - boolean parsed = false; - while (!parsed) { - Log.d("CHECKOUT", "200"); + outputEndpoint = readConnection.getHeaderField("Operation-Location"); - jsonConnection = (HttpsURLConnection) jsonURI.openConnection(); + } else { + // Error handling code goes here + Log.e("CHECK", "Failed with " + response); + Log.e("CHECK", "Failed with " + readConnection); + } - jsonConnection.setRequestMethod("GET"); + Log.d("CHECK", outputEndpoint); - jsonConnection.setRequestProperty("Ocp-Apim-Subscription-Key", subscriptionKey); + URL jsonURI = new URL(outputEndpoint); + HttpsURLConnection jsonConnection = (HttpsURLConnection) jsonURI.openConnection(); - InputStream responseBody = jsonConnection.getInputStream(); - InputStreamReader responseBodyReader = - new InputStreamReader(responseBody, StandardCharsets.UTF_8); - BufferedReader br = new BufferedReader(responseBodyReader); - StringBuilder sb = new StringBuilder(); + jsonConnection.setRequestMethod("GET"); - String inputLine; - while ((inputLine = br.readLine()) != null) { - sb.append(inputLine); - System.out.println(inputLine); - } - Log.d("CHECKOUT", sb.toString()); - if (sb.substring(0, Math.min(20, sb.length())).contains("Succeeded")) { - parsed = true; - } else { - jsonConnection.disconnect(); - jsonConnection.connect(); - Thread.sleep(50); - } + jsonConnection.setRequestProperty("Ocp-Apim-Subscription-Key", subscriptionKey); - } - Log.d("CHECKOUT", "200"); + response = jsonConnection.getResponseCode(); + if (response == 200) { + boolean parsed = false; + while (!parsed) { + Log.d("CHECKOUT", "200"); - InputStream responseBody = jsonConnection.getInputStream(); - InputStreamReader responseBodyReader = - new InputStreamReader(responseBody, StandardCharsets.UTF_8); - BufferedReader br = new BufferedReader(responseBodyReader); - StringBuilder sb = new StringBuilder(); + jsonConnection = (HttpsURLConnection) jsonURI.openConnection(); - String inputLine; - while ((inputLine = br.readLine()) != null) { - sb.append(inputLine); - System.out.println(inputLine); - } + jsonConnection.setRequestMethod("GET"); - br.close(); + jsonConnection.setRequestProperty("Ocp-Apim-Subscription-Key", subscriptionKey); - // System.out.println(sb.toString()); - jsonConnection.disconnect(); + InputStream responseBody = jsonConnection.getInputStream(); + InputStreamReader responseBodyReader = + new InputStreamReader(responseBody, StandardCharsets.UTF_8); + BufferedReader br = new BufferedReader(responseBodyReader); + StringBuilder sb = new StringBuilder(); - return sb.toString(); + String inputLine; + while ((inputLine = br.readLine()) != null) { + sb.append(inputLine); + System.out.println(inputLine); + } + Log.d("CHECKOUT", sb.toString()); + if (sb.substring(0, Math.min(20, sb.length())).contains("Succeeded")) { + parsed = true; + } else { + jsonConnection.disconnect(); + jsonConnection.connect(); + Thread.sleep(50); + } + } + Log.d("CHECKOUT", "200"); + + InputStream responseBody = jsonConnection.getInputStream(); + InputStreamReader responseBodyReader = + new InputStreamReader(responseBody, StandardCharsets.UTF_8); + BufferedReader br = new BufferedReader(responseBodyReader); + StringBuilder sb = new StringBuilder(); + + String inputLine; + while ((inputLine = br.readLine()) != null) { + sb.append(inputLine); + System.out.println(inputLine); + } + + br.close(); + + // System.out.println(sb.toString()); + jsonConnection.disconnect(); + + return sb.toString(); + + } else if (response == 202) { + Log.d("CHECKOUT", "202"); + } else { + // Error handling code goes here + Log.e("CHECKOUT", "Failed with " + response); + Log.e("CHECKOUT", "Failed with " + jsonConnection); + } - } else if (response == 202) { - Log.d("CHECKOUT", "202"); + Log.d("CHECKOUTLENGTH: ", outputEndpoint); - } else { - // Error handling code goes here - Log.e("CHECKOUT", "Failed with " + response); - Log.e("CHECKOUT", "Failed with " + jsonConnection); - } + } catch (Exception e) { + e.printStackTrace(); + } - Log.d("CHECKOUTLENGTH: ", outputEndpoint); + return ""; + } - } catch (Exception e) { - e.printStackTrace(); - } - return ""; - } - @Override - protected void onPreExecute() { - super.onPreExecute(); - dialog.setMessage("Analysing your receipt"); - dialog.show(); - } + @Override + protected void onPreExecute() { + super.onPreExecute(); + dialog.setMessage("Analysing your receipt"); + dialog.show(); + } - @Override - protected void onPostExecute(String s) { - super.onPostExecute(s); - dialog.dismiss(); - System.out.println("Payload: " + s); - } + @Override + protected void onPostExecute(String s) { + super.onPostExecute(s); + dialog.dismiss(); + System.out.println("Payload: " + s); } + } } diff --git a/app/src/main/res/layout/activity_splash.xml b/app/src/main/res/layout/activity_splash.xml index 6cb9ac3..c6e5ee1 100644 --- a/app/src/main/res/layout/activity_splash.xml +++ b/app/src/main/res/layout/activity_splash.xml @@ -8,26 +8,26 @@ tools:context=".Splash"> + app:layout_constraintTop_toTopOf="parent" /> \ No newline at end of file diff --git a/app/src/main/res/layout/activity_test_azure.xml b/app/src/main/res/layout/activity_test_azure.xml index fc175d6..1dc4b5a 100644 --- a/app/src/main/res/layout/activity_test_azure.xml +++ b/app/src/main/res/layout/activity_test_azure.xml @@ -17,9 +17,9 @@ android:text="@string/take_picture" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" - app:layout_constraintHorizontal_bias="0.0" + app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" - app:layout_constraintTop_toTopOf="parent" + app:layout_constraintTop_toBottomOf="@+id/azure_toolbar" app:layout_constraintVertical_bias="0.0" />