m n
m n

Reputation: 243

Android : How to capture text from camera without taking picture?

I want to capture texts and numbers that showing with camera without taking picture using tess-two(in android and eclipse). I dont want to save image file.

something like this (it is capturing live on camera):

OCR

I have used tess-two , but i have to take picture first and then capture text. (using link : https://stackoverflow.com/questions/19533273/best-ocr-optical-character-recognition-example-in-android)

and I have used this (https://www.codeproject.com/Articles/840623/Android-Character-Recognition) to create behaviour like picture I have uploaded but it should take picture too.

so how can I achieve that?Is it possible?

Upvotes: 5

Views: 12762

Answers (3)

shouzeb hasan
shouzeb hasan

Reputation: 21

/* this is the code of main activity it will capture text without taking picture all you need to do is to make a text field in mainActivity.xml */
    import android.Manifest;
    import android.content.pm.PackageManager;
    import android.support.annotation.NonNull;
    import android.support.v4.app.ActivityCompat;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;

> Blockquote

    import android.util.Log;
    import android.util.SparseArray;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    import android.widget.TextView;

    import com.google.android.gms.vision.CameraSource;
    import com.google.android.gms.vision.Detector;
    import com.google.android.gms.vision.text.TextBlock;
    import com.google.android.gms.vision.text.TextRecognizer;

    import java.io.IOException;

    public class MainActivity extends AppCompatActivity {

        SurfaceView cameraView;
        TextView textView;
        CameraSource cameraSource;
        final int RequestCameraPermissionID = 1001;

        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            switch (requestCode)
            {
                case RequestCameraPermissionID:
                {
                    if(grantResults[0]==PackageManager.PERMISSION_GRANTED)
                    {
                        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                            return;
                        }
                        try {
                            cameraSource.start(cameraView.getHolder());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }

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

            cameraView = (SurfaceView) findViewById(R.id.surface_view);
            textView = (TextView) findViewById(R.id.text_view);

            TextRecognizer textRecognizer = new TextRecognizer.Builder(getApplicationContext()).build();
            if (!textRecognizer.isOperational()) {
                Log.w("MainActivity", "Detector dependencies are not yet available");
            } else {
                cameraSource = new CameraSource.Builder(getApplicationContext(), textRecognizer)
                        .setFacing(CameraSource.CAMERA_FACING_BACK)
                        .setRequestedPreviewSize(1280, 1024)
                        .setRequestedFps(2.0f)
                        .setAutoFocusEnabled(true)
                        .build();
                cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
                    @Override
                    public void surfaceCreated(SurfaceHolder holder) {

                        try {
                            if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {

                               ActivityCompat.requestPermissions(MainActivity.this,
                                       new String[]{Manifest.permission.CAMERA},
                                       RequestCameraPermissionID);
                                return;
                            }
                            cameraSource.start(cameraView.getHolder());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

                    }

                    @Override
                    public void surfaceDestroyed(SurfaceHolder holder) {
                        cameraSource.stop();
                    }
                });

                textRecognizer.setProcessor(new Detector.Processor<TextBlock>() {
                    @Override
                    public void release() {

                    }

                    @Override
                    public void receiveDetections(Detector.Detections<TextBlock> detections) {
                        final SparseArray<TextBlock> items = detections.getDetectedItems();
                        if(items.size()!=0){
                            textView.post(new Runnable() {
                                @Override
                                public void run() {
                                    StringBuilder stringBuilder=new StringBuilder();
                                    for(int i=0;i<items.size();i++){
                                        TextBlock item = items.valueAt(i);
                                        stringBuilder.append(item.getValue());
                                        stringBuilder.append("\n");
                                    }
                                    textView.setText(stringBuilder.toString());
                                }
                            });
                        }
                    }
                });
            }
        }
    }

Upvotes: 0

Tomato
Tomato

Reputation: 2177

Alternatively, you can use free SDK that does OCR without taking any picture: ABBYY Real-time Recognition SDK. It does all the work for manitulating video stream for you.

Disclaimer: I work for ABBYY.

Upvotes: 0

Henu
Henu

Reputation: 1742

You can get the bitmap of the thumbnail easily as given in the android documentation here but from your question it seems like you will need the full size picture bitmap, so to get the full size bitmap you have to point the camera to some file where the capture picture can be stored.

To get the full size bitmap try this

private String imagePath = "";

private void startCamera() {
    // create a collision-resistant file name
    String fileUniqueName= new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = fileUniqueName + ".jpg";

    File storageDir = Environment.getExternalStoragePublicDirectory(
        Environment.DIRECTORY_PICTURES);
    imagePath = storageDir.getAbsolutePath() + "/" + imageFileName;
    File file = new File(imagePath);
    Uri outputFileUri = Uri.fromFile(file);

    Intent cameraIntent = new     Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);               
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
    startActivityForResult(cameraIntent, 1);
}

Handle the captured image

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1) {
        File tempFile = new  File(imagePath);
        if(tempFile.exists()){        
            Bitmap requiredBitmap = BitmapFactory.decodeFile(tempFile.getAbsolutePath());
        }
    }
}

This captured image will not be shown in the gallery.

Upvotes: 0

Related Questions