com.anadoluuniversity.tinyreader.OcrCaptureActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.anadoluuniversity.tinyreader.OcrCaptureActivity.java

Source

/*
 * Copyright (C) The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.anadoluuniversity.tinyreader;

import android.content.res.Configuration;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.SearchView;

import com.anadoluuniversity.tinyreader.model.PersistedAllergen;

/**
 * Activity for the Ocr Detecting app.  This app detects text and displays the value with the
 * rear facing camera. During detection overlay graphics are drawn to indicate the position,
 * size, and contents of each TextBlock.
 */
public final class OcrCaptureActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    private static final String TAG = "OcrCaptureActivity";
    public static String wordToSearch = "";
    private ActionBarDrawerToggle mDrawerToggle;
    private AllergensDataSource datasource;

    /**
     * Initializes the UI and creates the detector pipeline.
     */
    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.ocr_capture_activity);

        DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
                mDrawerLayout, /* DrawerLayout object */
                R.string.drawer_open, /* "open drawer" description */
                R.string.drawer_close /* "close drawer" description */
        );
        mDrawerLayout.addDrawerListener(mDrawerToggle);
        mDrawerToggle.syncState();

        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setHomeButtonEnabled(true);
        }

        getSupportActionBar()
                .setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.colorPrimary)));
        getWindow().setStatusBarColor(getResources().getColor(R.color.status_bar_color));

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        Fragment fragment = FragmentCamera.newInstance(true);
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.content_frame, fragment, "FragmentCamera");
        ft.commit();

        fillDB();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        if (getSupportFragmentManager().findFragmentByTag("FragmentCamera") != null
                && getSupportFragmentManager().findFragmentByTag("FragmentCamera").isVisible()) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.menu_search, menu);
            MenuItem item = menu.findItem(R.id.menuSearch);

            SearchView searchView = (SearchView) item.getActionView();

            searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String query) {
                    Log.d("searchbar_submit", query);
                    wordToSearch = query;
                    return false;
                }

                @Override
                public boolean onQueryTextChange(String newText) {
                    return false;
                }
            });
        }

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Pass the event to ActionBarDrawerToggle, if it returns
        // true, then it has handled the app icon touch event
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        // Handle your other action bar items...

        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        //creating fragment object
        Fragment fragment;
        String tag = "";

        //initializing the fragment object which is selected
        switch (item.getItemId()) {
        case R.id.nav_home:
            // todo search bari sifirla
            // todo search bari goster
            fragment = FragmentCamera.newInstance(true);
            tag = "FragmentCamera";
            break;
        case R.id.nav_isThisBadForMe:
            // todo search bari sifirla
            // todo search bari gizle
            fragment = FragmentCamera.newInstance(false);
            tag = "FragmentIsThisBadForMe";
            break;
        case R.id.nav_myProfile:
            fragment = new FragmentMyProfile();
            tag = "FragmentProfile";
            break;
        default:
            return false;
        }

        //replacing the fragment
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.content_frame, fragment, tag);
        ft.commit();

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);

        return true;
    }

    public void fillDB() {
        datasource = new AllergensDataSource(this);
        datasource.open();

        PersistedAllergen a = datasource.createAllergen("Gluten", "Celiac", 0);
        a = datasource.createAllergen("Grain", "Celiac", 0);
        a = datasource.createAllergen("Flour", "Celiac", 0);
        a = datasource.createAllergen("Wheat", "Celiac", 0);

        a = datasource.createAllergen("Phenylalanine", "Phenylketonuria", 0);
        a = datasource.createAllergen("Aspartame", "Phenylketonuria", 0);

        a = datasource.createAllergen("Lactose", "Lactose intolerance", 0);
        a = datasource.createAllergen("Cream", "Lactose intolerance", 0);
        a = datasource.createAllergen("Milk", "Lactose intolerance", 0);

        a = datasource.createAllergen("Butter", "Galaktosemia", 0);
        a = datasource.createAllergen("Buttermilk", "Galaktosemia", 0);
        a = datasource.createAllergen("Cheese", "Galaktosemia", 0);

        a = datasource.createAllergen("Amoxicillin", "Penicillin Allergy", 0);
        a = datasource.createAllergen("Co-amoxiclav", "Penicillin Allergy", 0);
        a = datasource.createAllergen("Flucloxacillin", "Penicillin Allergy", 0);
        a = datasource.createAllergen("Penicillin", "Penicillin Allergy", 0);

        a = datasource.createAllergen("Almond", "Nut Allergy", 0);
        a = datasource.createAllergen("Chestnut", "Nut Allergy", 0);
        a = datasource.createAllergen("Hazelnut", "Nut Allergy", 0);
        a = datasource.createAllergen("Pistachio", "Nut Allergy", 0);

        a = datasource.createAllergen("Soy", "Soy Allergy", 0);

        a = datasource.createAllergen("Trozin", "Trosinosiz", 0);

        a = datasource.createAllergen("Sodium Lauryl Sulfate", "Cosmetics", 0);
        a = datasource.createAllergen("Ammonium Lauryl Sulfate", "Cosmetics", 0);
        a = datasource.createAllergen("PEG", "Cosmetics", 0);
        a = datasource.createAllergen("Paraben", "Cosmetics", 0);
        a = datasource.createAllergen("Petroleum Jelly", "Cosmetics", 0);
        a = datasource.createAllergen("Talc", "Cosmetics", 0);

        a = datasource.createAllergen("Lard", "Lard and Pork", 0);
        a = datasource.createAllergen("Pork", "Lard and Pork", 0);

        for (PersistedAllergen al : datasource.getAllAllergens()) {
            Log.d("allergens", "id: " + al.getId() + " name: " + al.getName() + " disease: " + al.getDisease());
        }

    }
}