package com.example.patrice.camera06;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseArray;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import static java.security.AccessController.getContext;

// http://www.c-sharpcorner.com/article/xtext-recognition/


import com.google.android.gms.vision.CameraSource;
import com.google.android.gms.vision.Frame;
import com.google.android.gms.vision.barcode.Barcode;
import com.google.android.gms.vision.barcode.BarcodeDetector;
import com.google.android.gms.vision.text.TextBlock;
import com.google.android.gms.vision.text.TextRecognizer;

/*
 <meta-data
         android:name="com.google.android.gms.vision.DEPENDENCIES"
         android:value="ocr" />
*/

//   compile 'com.google.android.gms:play-services-vision:9.4.0+'

public class MainActivity extends AppCompatActivity {

    private ImageView imageview;
    private Button btnProcess;
    private TextView txtView;
    private TextRecognizer recognizer;
    private Bitmap b;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageview  = (ImageView) findViewById(R.id.image_view);
        btnProcess = (Button) findViewById(R.id.btnProcess);
        txtView = (TextView) findViewById(R.id.txtView);


        Resources res = getResources();
        int id = R.drawable.ocr_text;
        b = BitmapFactory.decodeResource(res, id);

        imageview.setImageBitmap(b); ;

        btnProcess.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {



                recognizer = new TextRecognizer.Builder(getApplicationContext()).build();
                Log.i("Hitesh",""+recognizer);
                if (recognizer.isOperational())
                {
                    txtView.setText("Could not set up the detector!");
                    return;
                }


                TextRecognizer txtRecognizer =  new TextRecognizer.Builder(getApplicationContext()).build();
                if (!txtRecognizer.isOperational() )
                {
                    Log.i("Error", "Detector dependencies are not yet available");
                }
                else
                {

                    Frame frame = new Frame.Builder().setBitmap(b).build();
                    SparseArray<TextBlock> textBlocks = recognizer.detect(frame);

                    StringBuilder strBuilder = new StringBuilder();
                    for (int i = 0; i < textBlocks.size(); i++)
                    {
                        TextBlock item =  textBlocks.valueAt(i);
                        strBuilder.append(item.getValue());
                        strBuilder.append("/");
                    }
                    txtView.setText( strBuilder.toString() );
                }


            }
        });

        /*
        btnProcess.Click += delegate
        {
            TextRecognizer txtRecognizer = new TextRecognizer.Builder(ApplicationContext).Build();
            if (!txtRecognizer.IsOperational)
            {
                Log.Error("Error", "Detector dependencies are not yet available");
            }
            else
            {
                Frame frame = new Frame.Builder().SetBitmap(bitmap).Build();
                SparseArray items = txtRecognizer.Detect(frame);
                StringBuilder strBuilder = new StringBuilder();
                for (int i = 0; i < items.Size(); i++)
                {
                    TextBlock item = (TextBlock)items.ValueAt(i);
                    strBuilder.Append(item.Value);
                    strBuilder.Append("/");
                }
                txtView.Text = strBuilder.ToString();
            }
        };*/


//        Bitmap bitmap = BitmapFactory.DecodeResource(ApplicationContext.Resources, Resource.Drawable.csharpcorner);
//        imageview.SetImageBitmap(bitmap);

    }
}
