Blog purpose for android basic example for android app developer. any query please my contact

Wednesday, 15 July 2020

extract text from image android source code

In this article todayt learn android studio ocr example. Ocr is a using a image to text convert for android studio app. Follow the source extract text from image android source code
Optical character recognition (OCR) refers to the process of automatically identifying from an image characters or symbols belonging to a specified alphabet. In this post we will focus on explaining how to use OCR on Android.
Optical character recognition (OCR) refers to the process of automatically identifying from an image characters or symbols belonging to a specified alphabet. In this post we will focus on explaining how to use OCR on Android.







Build.gradle dependancy:


implementation 'com.google.android.gms:play-services-mlkit-text-recognition:16.2.0'



Follow the  code extract text from image android source code :

1.First, you can create MainActivity.java:
Then The Declaire the variables:
private SurfaceView cameraView;
    private TextView txtView;
    private CameraSource cameraSource;

Overview Declaire OnCreate Method in MainActivity.java:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cameraView = findViewById(R.id.surface_view);
        txtView = findViewById(R.id.txtview);
        btnview=findViewById(R.id.btnview);
        TextRecognizer txtRecognizer = new TextRecognizer.Builder(getApplicationContext()).build();
        if (!txtRecognizer.isOperational()) {
            Log.e("Main Activity", "Detector dependencies are not yet available");
        } else {
            cameraSource = new CameraSource.Builder(getApplicationContext(), txtRecognizer)
                    .setFacing(CameraSource.CAMERA_FACING_BACK)
                    .setRequestedPreviewSize(1280, 1024)
                    .setRequestedFps(2.0f)
                    .setAutoFocusEnabled(true)
                    .build();
            cameraView.getHolder().addCallback(this);
            txtRecognizer.setProcessor(this);
        }

        btnview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str=txtView.getText().toString();
                Intent intent = new Intent(getApplicationContext(),Display_text.class);
                intent.putExtra("message",str);
                startActivity(intent);
            }
        });
    }

2.Then Follow the Design  Dispaly activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/activity_horizontal_margin">

    <SurfaceView
        android:id="@+id/surface_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/btnview"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true" />


    <TextView
        android:id="@+id/txtview"
        android:layout_width="350dp"
        android:layout_height="554dp"
        android:layout_alignParentTop="true"
        android:layout_marginStart="@dimen/activity_horizontal_margin"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginTop="76dp"
        android:text="No  Detected Text"
        android:textColor="@android:color/white"
        android:textSize="20sp" />

    <Button
        android:id="@+id/btnview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="View Text" />
</RelativeLayout>



3.After The Create a New Activity Follow Name  Display.java:

Dispaly the OCr Text Result form Image.
Display_text.java:
package com.akash.androidocr;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Display_text extends AppCompatActivity {
TextView textView1;
Button btnok;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_text);
        textView1=(TextView)findViewById(R.id.textView1);
        btnok=(Button)findViewById(R.id.btnclear);
        btnok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textView1.setText("");
            }
        });
        Intent intent = getIntent();
        String str = intent.getStringExtra("message");
        textView1.setText(str);
    }
}



4. activity_display_text.xml for Display Actvity:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/activity_horizontal_margin">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="402dp"
        android:layout_height="464dp"
        tools:layout_editor_absoluteX="3dp" />

    <Button
        android:id="@+id/btnclear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Clear Text" />
</RelativeLayout>

5. And Last to the Modyfy the  AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.akash.androidocr">

    <application
        android:allowBackup="true"
        android:icon="@drawable/ocr"
        android:label="@string/app_name"
        android:roundIcon="@drawable/ocr"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".Display_text"></activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

    <uses-permission android:name="android.permission.CAMERA" />

</manifest>

Then The Result extract text from image android source code.

6.Output:


extract text from image android source code

extract text from image android source code

3 comments:

  1. Incomplete information. Where are dependencies?

    ReplyDelete
    Replies
    1. implementation 'com.google.android.gms:play-services-mlkit-text-recognition:16.2.0'

      update post

      Delete
  2. Text Scanner OCR Mod APK crack [ Premium Full Unlocked ] Free is an Android app for extracting and scanning texts on Android phones....https://getpcrack.com/text-scanner-ocr-mod-apk-crack/

    ReplyDelete