Twitter Facebook Delicious Digg Stumbleupon Favorites More

Saturday 11 August 2018

Text Recognition using Google Vision


  1. 1android mainfest 
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  4.     package="com.example.raj.imagetowordconverter">

  5.     <application
  6.         android:allowBackup="true"
  7.         android:icon="@mipmap/ic_launcher"
  8.         android:label="@string/app_name"
  9.         android:roundIcon="@mipmap/ic_launcher_round"
  10.         android:supportsRtl="true"
  11.         android:theme="@style/AppTheme">
  12.         <activity android:name=".MainActivity">
  13.             <intent-filter>
  14.                 <action android:name="android.intent.action.MAIN" />

  15.                 <category android:name="android.intent.category.LAUNCHER" />
  16.             </intent-filter>
  17.         </activity>

  18.         <meta-data
  19.             android:name="com.google.android.gms.vision.DEPENDENCIES"
  20.             android:value="ocr">

  21.         </meta-data>


  22.     </application>

  23. </manifest>

  1. 2 xml 
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.     xmlns:app="http://schemas.android.com/apk/res-auto"
  5.     xmlns:tools="http://schemas.android.com/tools"
  6.     android:layout_width="match_parent"
  7.     android:layout_height="match_parent"
  8.     tools:context=".MainActivity"
  9.     android:orientation="vertical">


  10.     <ImageView
  11.         android:layout_gravity="center_horizontal"
  12.         android:scaleType="center"
  13.         android:layout_width="wrap_content"
  14.         android:layout_height="wrap_content"
  15.         android:id="@+id/image_view"/>

  16.     <Button
  17.         android:layout_below="@id/image_view"
  18.         android:layout_gravity="center_horizontal"
  19.         android:id="@+id/button_process"
  20.         android:layout_width="match_parent"
  21.         android:layout_height="wrap_content"
  22.         android:text="PROCESS"
  23.         />
  24.     <TextView
  25.         android:layout_below="@id/button_process"

  26.         android:id="@+id/result"
  27.         android:gravity="center_horizontal"
  28.         android:layout_width="match_parent"
  29.         android:layout_height="wrap_content"
  30.         android:text="NO TEXT"
  31.         android:textSize="24sp"
  32.         />
  33. </RelativeLayout>

  1. 2 main activity 
  2. package com.example.raj.imagetowordconverter;

  3. import android.graphics.Bitmap;
  4. import android.graphics.BitmapFactory;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8. import android.util.SparseArray;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.ImageView;
  12. import android.widget.TextView;

  13. import com.google.android.gms.vision.Frame;
  14. import com.google.android.gms.vision.text.TextBlock;
  15. import com.google.android.gms.vision.text.TextRecognizer;

  16. public class MainActivity extends AppCompatActivity {

  17.     ImageView imageView;
  18.     Button btnp_rocess;
  19.     TextView txtResult;

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

  24.     imageView=(ImageView)findViewById(R.id.image_view);
  25.     btnp_rocess=(Button)findViewById(R.id.button_process);
  26.     txtResult=(TextView)findViewById(R.id.result);

  27.         final Bitmap bitmap= BitmapFactory.decodeResource(
  28.                 getApplicationContext().getResources(),R.drawable.txt
  29.         );

  30.         imageView.setImageBitmap(bitmap);

  31.         btnp_rocess.setOnClickListener(new View.OnClickListener() {
  32.             @Override
  33.             public void onClick(View v) {
  34.                 TextRecognizer textRecognizer=new TextRecognizer.Builder(getApplicationContext()).build();

  35.             if (!textRecognizer.isOperational())
  36.                 Log.e("ERROR","DECTOR ARE OT FUNCTIONAL");
  37.             else {
  38.                 Frame frame=new Frame.Builder().setBitmap(bitmap).build();
  39.                 SparseArray<TextBlock> items=textRecognizer.detect(frame);
  40.                 StringBuilder stringBuilder=new StringBuilder();
  41.                 for (int i=0;i<items.size();++i)
  42.                 {
  43.                     TextBlock item=items.valueAt(i);
  44.                     stringBuilder.append(item.getValue());
  45.                     stringBuilder.append("\n");
  46.                 }
  47.                 txtResult.setText(stringBuilder.toString());
  48.             }
  49.             }
  50.         });
  51.     }
  52. }
Share:

0 comments:

Post a Comment

Search This Blog

Popular Posts

Pages

how to make crores from 1 lakh in stock markets in 1 year

how to make crores from 1 lakh in stock markets in 1 year

Blogger Tutorials

Blogger Templates

Sample Text

Copyright © ANDROID TUTORIAL CODE | Powered by Blogger
Design by SimpleWpThemes | Blogger Theme by NewBloggerThemes.com & Distributed By Protemplateslab