Hi zdenko! Thank you for the reply! It is in Bitmap object. I've uploaded a 
text file of my code to let you see what is the logical error is. I quite 
in the edge now. xD


On Monday, April 17, 2017 at 4:13:45 PM UTC+8, zdenop wrote:
>
> Is image (in "mTess.setImage(image)" pix object?
>
> Zdenko
>
> On Mon, Apr 17, 2017 at 7:56 AM, NewbProgrammer <[email protected] 
> <javascript:>> wrote:
>
>> I am currently developing an app in android studio. It seems that my app 
>> is crashing at "mTess.setImage(image)"  I don't know why and I'm still 
>> learning. Can anyone help me? Btw, the image that was set was taken from a 
>> camera and the will be cropped out. After cropping it out the image will be 
>> convert into greyscaled image and then binary image to increase accuracy of 
>> tesseract. 
>>
>> The reason why I said that the error was in mTess.setImage() was because 
>> everytime I comment it out the app runs fine without saying "unfortunately 
>> blah blah", so there is where I get my basis from. :/
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "tesseract-ocr" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to [email protected] <javascript:>.
>> To post to this group, send email to [email protected] 
>> <javascript:>.
>> Visit this group at https://groups.google.com/group/tesseract-ocr.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/tesseract-ocr/9659ee2e-0496-4784-b729-62a129c47c1d%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/tesseract-ocr/9659ee2e-0496-4784-b729-62a129c47c1d%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"tesseract-ocr" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/tesseract-ocr.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/tesseract-ocr/f13b4a98-e4bd-42ef-9e9e-20eefe98ad8e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.googlecode.tesseract.android.TessBaseAPI;

import org.w3c.dom.Text;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class MainActivity extends AppCompatActivity {

    private TessBaseAPI mTess;
    private String datapath = "";
    private Uri picUri;
    Button button;
    ImageView imageView;
    TextView textView;
    private String storageDir = Environment.getExternalStorageDirectory() + 
"/LPAMSolver";
    static final int CAM_REQUEST = 1;
    static final int CROP_PIC = 2;

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

        button = (Button) findViewById(R.id.button);
        imageView = (ImageView) findViewById(R.id.imageView);
        textView = (TextView) findViewById(R.id.textView);

        //initialize Tesseract API
        String language = "amh";
        datapath = getFilesDir() + "assets/";
        mTess = new TessBaseAPI();

        checkFile(new File(datapath + "tessdata/"));

        mTess.init(datapath, language);


        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent camera_intent = new 
Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File file = getFile();
                picUri = Uri.fromFile(file);
                camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, 
Uri.fromFile(file));
                startActivityForResult(camera_intent, CAM_REQUEST);



            }
        });
    }



    //to check traindata if existing
    private void checkFile(File dir) {
        if (!dir.exists() && dir.mkdirs()) {
            copyFiles();
        }
        if (dir.exists()) {
            String datafilepath = datapath + "/tessdata/amh.traineddata";
            File datafile = new File(datafilepath);

            if (!datafile.exists()) {
                copyFiles();
            }
        }
    }

    //to initialize tesseract traindata
    private void copyFiles() {
        try {
            String filepath = datapath + "/tessdata/amh.traineddata";
            AssetManager assetManager = getAssets();

            InputStream instream = 
assetManager.open("tessdata/amh.traineddata");
            OutputStream outstream = new FileOutputStream(filepath);

            byte[] buffer = new byte[1024];
            int read;
            while ((read = instream.read(buffer)) != -1) {
                outstream.write(buffer, 0, read);
            }


            outstream.flush();
            outstream.close();
            instream.close();

            File file = new File(filepath);
            if (!file.exists()) {
                throw new FileNotFoundException();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    //to create directory for the application and make image file
    private File getFile() {

        File folder = new File(Environment.getExternalStorageDirectory() + 
"/LPAMSolver");


        if (!folder.exists()) {
            folder.mkdir();
        }

        File image_file = new File(folder, "cam_image.png");

        return image_file;
    }

    //transform image to grayscale
    private Bitmap getGrayscale(Bitmap src){

        //Custom color matrix to convert to GrayScale
        float[] matrix = new float[]{
                0.3f, 0.59f, 0.11f, 0, 0,
                0.3f, 0.59f, 0.11f, 0, 0,
                0.3f, 0.59f, 0.11f, 0, 0,
                0, 0, 0, 1, 0,};

        Bitmap dest = Bitmap.createBitmap(
                src.getWidth(),
                src.getHeight(),
                src.getConfig());

        Canvas canvas = new Canvas(dest);
        Paint paint = new Paint();
        ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
        paint.setColorFilter(filter);
        canvas.drawBitmap(src, 0, 0, paint);

        return dest;
    }

    //transform image to binary from grayscale
    public Bitmap getBinary(Bitmap bmpOriginal) {
        int width, height, threshold;
        height = bmpOriginal.getHeight();
        width = bmpOriginal.getWidth();
        threshold = 100;

        Bitmap bmpBinary = Bitmap.createBitmap(bmpOriginal);

        for(int x = 0; x < width; ++x) {
            for(int y = 0; y < height; ++y) {
                // get one pixel color
                int pixel = bmpOriginal.getPixel(x, y);
                int red = Color.red(pixel);
                int green = Color.green(pixel);
                int blue = Color.blue(pixel);

                //get grayscale value
                int gray = (int)(red * 0.3 + green * 0.59 + blue *0.11);

                //get binary value
                if(gray < threshold){
                    bmpBinary.setPixel(x, y, 0xFF000000);
                } else{
                    bmpBinary.setPixel(x, y, 0xFFFFFFFF);
                }

            }
        }
        return bmpBinary;
    }

    //calls the intent for crop
    public void crop(){

        Intent CropIntent = new Intent("com.android.camera.action.CROP");
        File file = getFile();
        picUri = Uri.fromFile(file);
        CropIntent.setDataAndType(picUri, "image/*");

        CropIntent.putExtra("crop", "true");
        CropIntent.putExtra("outputX", 200);
        CropIntent.putExtra("outputY", 200);
        CropIntent.putExtra("aspectX", 0);
        CropIntent.putExtra("aspectY", 0);
        CropIntent.putExtra("scaleUpIfNeeded", true);
        CropIntent.putExtra("return-data", true);


        CropIntent.putExtra(MediaStore.EXTRA_OUTPUT, picUri);


        startActivityForResult(CropIntent, CROP_PIC);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent 
data) {

        if (requestCode == CAM_REQUEST) {

            crop();

        }
        if (requestCode == CROP_PIC){

            Bundle extras = data.getExtras();
            Bitmap selectedBitmap = extras.getParcelable("data");

            Bitmap convertedimage = getBinary(getGrayscale(selectedBitmap));
            
            String OCRresult = "";
            mTess.setImage(convertedimage);
            OCRresult = mTess.getUTF8Text();
            textView.setText(OCRresult);
            imageView.setImageBitmap(convertedimage);

            Intent intent = new 
Intent(getApplicationContext(),Main2Activity.class);
            intent.putExtra("OCRresult",OCRresult);
            intent.putExtra("convertedimage",convertedimage);
            startActivity(intent);

            }


        }
}

Reply via email to