Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,13 @@ dependencies {
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'org.json:json:20180813'
implementation 'org.apache.commons:commons-lang3:3.8.1'
implementation 'org.apache.commons:commons-text:1.6'
implementation 'org.apache.commons:commons-collections4:4.4'
implementation 'org.apache.commons:commons-lang3:3.9'
implementation 'org.apache.commons:commons-lang3:3.12.0'


}
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
<activity
android:name=".MainActivity"
android:exported="false" />
<activity
android:name=".ManualInputActivity"
android:exported="false" />
<activity
android:name=".RegisterActivity"
android:exported="true">
Expand Down
14 changes: 12 additions & 2 deletions app/src/main/java/com/example/snackscan/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class MainActivity extends AppCompatActivity {
private ImageView iview;
private Button btn;
private Button nextBtn;
private Button manualBtn;
private String fileName = "";
private Button CaptureImageBtn,detectTextBtn;
private ImageView imageView;
Expand All @@ -54,12 +55,12 @@ protected void onCreate(Bundle savedInstanceState) {
iview = (ImageView) findViewById(R.id.imageview);
btn = (Button) findViewById(R.id.button);
nextBtn = (Button) findViewById(R.id.nextBtn);

manualBtn = (Button) findViewById(R.id.manualBtn);

nextBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
System.out.println("Button working");

Intent sendText = new Intent(getApplicationContext(), TextRecognitionActivity.class);
sendText.putExtra("scanned_text", resultText);
startActivity(sendText);
Expand All @@ -68,6 +69,7 @@ public void onClick(View v){
}

});

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand All @@ -80,6 +82,14 @@ public void onClick(View v) {

}
});

manualBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(), ManualInputActivity.class));
finish();
}
});
}

public void logout(View view) {
Expand Down
82 changes: 82 additions & 0 deletions app/src/main/java/com/example/snackscan/ManualInputActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.example.snackscan;

import static android.content.ContentValues.TAG;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.FirebaseFirestore;

public class ManualInputActivity extends AppCompatActivity {
EditText mCal, mCarb, mFat, mProtein;
Button mSubmit;
FirebaseAuth fAuth;
FirebaseFirestore fStore;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manual_input);
// TODO: add 4 elements, click submit btn then upload to the user db to firebase
mCal = findViewById(R.id.calMnumber);
mCarb = findViewById(R.id.carbMnumber);
mFat = findViewById(R.id.fatMnumber);
mProtein = findViewById(R.id.proteinMnumber);
mSubmit = (Button) findViewById(R.id.submitButton);
fStore = FirebaseFirestore.getInstance();
fAuth = FirebaseAuth.getInstance();

mSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = getIntent();
String id = intent.getStringExtra("user_id");
String cal = mCal.getText().toString();
String carb = mCarb.getText().toString();
String fat = mFat.getText().toString();
String protein = mProtein.getText().toString();
String uid = fAuth.getCurrentUser().getUid();

DocumentReference documentReference = fStore.collection("users").document(uid);
documentReference.update("Calories", cal).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void unused) {
Log.d(TAG, "onSuccess: user calories added for " + uid);
}
});
documentReference.update("Carbs", carb).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void unused) {
Log.d(TAG, "onSuccess: user carbs added for " + uid);
}
});
documentReference.update("Fat", fat).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void unused) {
Log.d(TAG, "onSuccess: user fat added for " + uid);
}
});
documentReference.update("Proteins", protein).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void unused) {
Log.d(TAG, "onSuccess: user proteins added for " + uid);
}
});
Toast.makeText(getApplicationContext(),"Inserted Successfully",Toast.LENGTH_LONG).show();
startActivity(new Intent(getApplicationContext(), MainActivity.class));
finish();
}
});
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,115 @@
package com.example.snackscan;
//package org.apache.commons.text.similarity;

import androidx.appcompat.app.AppCompatActivity;
import org.apache.commons.text.similarity.CosineDistance;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class TextRecognitionActivity extends AppCompatActivity {
TextView Ingredients;
TextView Calories;
TextView Protein;
TextView Carb;
TextView Fat;
private int caloriesIndex;
private int proteinIndex;
private int carbIndex;
private int fatIndex;
private Boolean foundCal=false;
private Boolean foundProtein=false;
private Boolean foundCarb =false;
private Boolean foundFat=false;

public static double findSimilarity(String x, String y) {

double maxLength = Math.max(x.length(), y.length());
if (maxLength > 0) {
// optionally ignore case if needed
return (maxLength - StringUtils.getLevenshteinDistance(x, y)) / maxLength;
}
return 1.0;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_recognition);
// Ingredients = (TextView)findViewById(R.id.ingredients);
Calories = (TextView)findViewById((R.id.calnumber));
Protein = (TextView)findViewById(R.id.proteinnumber);
Carb = (TextView)findViewById((R.id.carbnumber));
Fat = (TextView)findViewById(R.id.fatnumber);





// Function to get similarity




Ingredients = (TextView)findViewById(R.id.ingredients);
Intent intent = getIntent();
String id = intent.getStringExtra("scanned_text");
Ingredients.setText(id);
System.out.println(id);
id.replace("\n", " ");
String [] food_info = id.split("\\s+");
//String [] food_info = {"CElurios", "560", "prOton", "34","fut", "23", "Sagar","9859"};

System.out.println("TESTING!!!!!!!");
// System.out.println(food_info.length);
// System.out.println();

for(int i=0; i< food_info.length; i++){
if(findSimilarity("calories", food_info[i].toLowerCase())>= 0.5 && !foundCal){

caloriesIndex = i+1;
foundCal = true;
}
if(findSimilarity("protein", food_info[i].toLowerCase())>= 0.5 && !foundProtein){

proteinIndex = i+1;
foundProtein = true;

}
if(findSimilarity("carbohydrates", food_info[i].toLowerCase())>= 0.5 && !foundCarb){

carbIndex = i+1;
foundCarb = true;

}
if(findSimilarity("fat", food_info[i].toLowerCase())>= 0.8 && !foundFat){

fatIndex = i+1;
foundFat = true;

}

}
System.out.println(caloriesIndex);
System.out.println(proteinIndex);
System.out.println(carbIndex);
System.out.println(fatIndex);

System.out.println(Arrays.deepToString(food_info));
// System.out.println("printing Array of food");

Calories.setText(food_info[caloriesIndex]);
Protein.setText(food_info[proteinIndex]);
Carb.setText(food_info[carbIndex]);
Fat.setText(food_info[fatIndex]);
//Ingredients.setText(id);



//System.out.println(Arrays.deepToString(food_info));



}
}
Loading