content: package com.rahulsoni0.image2text.tools;
  
  import androidx.annotation.NonNull;
  import androidx.appcompat.app.AppCompatActivity;
  
  import android.Manifest;
  import android.app.ActionBar;
  import android.app.Activity;
  import android.content.ClipData;
  import android.content.ClipboardManager;
  import android.content.Intent;
  import android.content.pm.PackageManager;
  import android.graphics.Bitmap;
  import android.graphics.Point;
  import android.graphics.Rect;
  import android.os.Build;
  import android.os.Bundle;
  import android.provider.MediaStore;
  import android.view.ContextMenu;
  import android.view.View;
  import android.view.WindowManager;
  import android.widget.Toast;
  
  import com.google.android.gms.tasks.OnFailureListener;
  import com.google.android.gms.tasks.OnSuccessListener;
  import com.google.android.gms.tasks.Task;
  import com.google.mlkit.vision.common.InputImage;
  import com.google.mlkit.vision.text.Text;
  import com.google.mlkit.vision.text.TextRecognition;
  import com.google.mlkit.vision.text.TextRecognizer;
  import com.google.mlkit.vision.text.latin.TextRecognizerOptions;
  import com.rahulsoni0.image2text.R;
  import com.rahulsoni0.image2text.databinding.ActivityScannerBinding;
  
  public class ScannerActivity extends AppCompatActivity {
      private Bitmap imageBitmap;
      private static final int CAMERA_REQUEST = 1888;
      private static final int MY_CAMERA_PERMISSION_CODE = 100;
      private ActivityScannerBinding binding;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          binding = ActivityScannerBinding.inflate(getLayoutInflater());
          setContentView(binding.getRoot());
          if (getSupportActionBar() != null) {
              getSupportActionBar().hide();
          }
          binding.btnSnap.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                      if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                          requestPermissions(new String[]{Manifest.permission.CAMERA}, MY_CAMERA_PERMISSION_CODE);
                      } else {
                          Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                          startActivityForResult(cameraIntent, CAMERA_REQUEST);
                      }
                  }
              }
          });
  
          binding.btnScntext.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
  
                  detectText();
              }
          });
  
          registerForContextMenu(binding.tvResult);
  
  
      }
  
  
      @Override
      public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
          super.onRequestPermissionsResult(requestCode, permissions, grantResults);
          if (requestCode == MY_CAMERA_PERMISSION_CODE) {
              if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                  Toast.makeText(this, "camera permission granted", Toast.LENGTH_LONG).show();
                  Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                  startActivityForResult(cameraIntent, CAMERA_REQUEST);
              } else {
                  Toast.makeText(this, "camera permission denied", Toast.LENGTH_LONG).show();
              }
          }
      }
  
      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
          if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
              Bitmap photo = (Bitmap) data.getExtras().get("data");
              binding.ivResult.setImageBitmap(photo);
              imageBitmap = photo;
          }
      }
  
      private void detectText() {
  
          InputImage image = InputImage.fromBitmap(imageBitmap, 0);
          TextRecognizer recognizer = TextRecognition.getClient(TextRecognizerOptions.DEFAULT_OPTIONS);
          Task<Text> result = recognizer.process(image).addOnSuccessListener(new OnSuccessListener<Text>() {
              @Override
              public void onSuccess(Text text) {
  
                  StringBuilder result = new StringBuilder();
                  for (Text.TextBlock block : text.getTextBlocks()) {
                      String blockText = block.getText();
                      Point[] blockCornerpoint = block.getCornerPoints();
                      Rect blockFrame = block.getBoundingBox();
                      for (Text.Line line : block.getLines()) {
                          String lineText = line.getText();
                          Point[] lineCornerPoint = line.getCornerPoints();
                          Rect lineRect = line.getBoundingBox();
                          for (Text.Element element : line.getElements()) {
                              String elementText = element.getText();
                              result.append(elementText + " ");
                          }
                          //Toast.makeText(getApplicationContext(), result.toString()+" hello", Toast.LENGTH_SHORT).show();
                          binding.tvResult.setText(result.toString());
                      }
  
                  }
              }
          }).addOnFailureListener(new OnFailureListener() {
              @Override
              public void onFailure(@NonNull Exception e) {
                  Toast.makeText(getApplicationContext(), "Failed to detect text From image ....!" + e.getMessage(), Toast.LENGTH_SHORT).show();
              }
          });
      }
  
      @Override
      public void onCreateContextMenu(ContextMenu menu, View v,
                                      ContextMenu.ContextMenuInfo menuInfo) {
  
          menu.add(0, v.getId(), 0, "Copy");
          menu.setHeaderTitle("Copy text"); //setting header title for menu
          ClipboardManager manager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
          ClipData clipData = ClipData.newPlainText("text", binding.tvResult.getText());
          manager.setPrimaryClip(clipData);
          Toast.makeText(ScannerActivity.this, "Text Copied to clipboard", Toast.LENGTH_SHORT).show();
      }
  }