Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I imrove @sturkmen' s code.

fragment_main.xml

<FrameLayout 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"
tools:context="{your package name}.FragmentMain">

<!-- TODO: Update blank fragment layout -->

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btnCamera"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:text="Kamera" />

    <Button
        android:id="@+id/btnTest"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:text="Test" />



    <ImageView
        android:id="@+id/sampleImageView"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_centerHorizontal="true"/>
</LinearLayout>

</framelayout>

AndroidManifest.xml

Add this line for write permission.

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

FragmentMain.java

IMAGE FILE: Add Internal Storage / Android / Data / Your Package Folder / test.JPG

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View _view = inflater.inflate(R.layout.fragment_main, container, false);

    Button btnTest = (Button) _view.findViewById(R.id.btnTest);
    btnTest.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            Mat img = Imgcodecs.imread(mediaStorageDir().getPath() + "/" + "test.JPG");
            if (img.empty()) {
                Log.d("FragmentMain", "Empty Image");
            }


            Size dims = new Size (20,5);
            Mat gray = new Mat();
            Mat thresh = new Mat();

            //convert the image to black and white
            Imgproc.cvtColor(img, gray, Imgproc.COLOR_BGR2GRAY);
            storeImage(gray);

            //convert the image to black and white does (8 bit)
            Imgproc.threshold(gray, thresh, 0, 255, Imgproc.THRESH_BINARY_INV + Imgproc.THRESH_OTSU);

            storeImage(thresh);

            Mat temp = thresh.clone();
            //find the contours
            Mat hierarchy = new Mat();

            Mat corners = new Mat(4,1, CvType.CV_32FC2);
            List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
            Imgproc.findContours(temp, contours,hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
            hierarchy.release();

            for (int idx = 0; idx < contours.size(); idx++)
            {
                MatOfPoint contour = contours.get(idx);
                MatOfPoint2f contour_points = new MatOfPoint2f(contour.toArray());
                RotatedRect minRect = Imgproc.minAreaRect( contour_points );
                Point[] rect_points = new Point[4];
                minRect.points( rect_points );
                if(minRect.size.height > img.width() / 2)
                {
                    List<Point> srcPoints = new ArrayList<Point>(4);
                    srcPoints.add(rect_points[2]);
                    srcPoints.add(rect_points[3]);
                    srcPoints.add(rect_points[0]);
                    srcPoints.add(rect_points[1]);

                    corners = Converters.vector_Point_to_Mat(
                            srcPoints, CvType.CV_32F);
                }

            }
            Imgproc.erode(thresh, thresh, new Mat(), new Point(-1,-1), 10);

            storeImage(thresh);
            Imgproc.dilate(thresh, thresh, new Mat(), new Point(-1,-1), 5);

            storeImage(thresh);

            Mat results = new Mat(1000,250,CvType.CV_8UC3);
            Mat quad = new Mat(1000,250,CvType.CV_8UC1);

            List<Point> dstPoints = new ArrayList<Point>(4);
            dstPoints.add(new Point(0, 0));
            dstPoints.add(new Point(1000, 0));
            dstPoints.add(new Point(1000, 250));
            dstPoints.add(new Point(0, 250));
            Mat quad_pts = Converters.vector_Point_to_Mat(
                    dstPoints, CvType.CV_32F);

            Mat transmtx = Imgproc.getPerspectiveTransform(corners, quad_pts);
            Imgproc.warpPerspective( img, results, transmtx, new Size(1000,250));
            Imgproc.warpPerspective( thresh, quad, transmtx, new Size(1000,250));

            Imgproc.resize(quad, quad, new Size(20,5));

            Imgcodecs.imwrite("results.png",quad);

            //store image
            storeImage(quad);

            //show image
            showImage(quad);


            System.out.println( quad.dump() );

            for(int i = 0; i < quad.cols(); i++)
            {
                int size = (int) (quad.total() * quad.channels());
                byte[] tmp = new byte[size];

                String answer = "";
                double[] d = new double[0];
                d = quad.get(1, i);
                answer += d[0] == 0 ? "" : "A";
                d = quad.get(2, i);
                answer += d[0] == 0 ? "" : "B";
                d = quad.get(3, i);
                answer += d[0] == 0 ? "" : "C";
                d = quad.get(4, i);
                answer += d[0] == 0 ? "" : "D";

                if( answer.length()  > 1 ) answer = "X"; // Double mark
                int y = 0;
                if( answer.equals("A")) y = results.rows() / (int) dims.height;
                if( answer.equals("B")) y = results.rows() / (int) dims.height *2;
                if( answer.equals("C")) y = results.rows() / (int) dims.height *3;
                if( answer.equals("D")) y = results.rows() / (int) dims.height *4;
                if( answer == "" ) answer = "[-]";
                Imgproc.putText( results, answer, new Point( 50* i + 15, 30 + y), Core.FONT_HERSHEY_PLAIN, 2, new Scalar(0,0,255),2);

            }


            //store image
            storeImage(results);

            //show image
            showImage(results);

        }

    });
public void showImage (Mat img) {
    ImageView imgView = (ImageView) getActivity().findViewById(R.id.sampleImageView);
    //Mat mRgba = new Mat();

    //mRgba = Utils.loadResource(MainAct.this, R.drawable.your_image,Highgui.CV_LOAD_IMAGE_COLOR);
    Bitmap img2 = Bitmap.createBitmap(img.cols(), img.rows(),Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(img, img2);
    imgView.setImageBitmap(img2);
}

public File mediaStorageDir () {
    File _mediaStorageDir = new File(Environment.getExternalStorageDirectory()
            + "/Android/data/"
            + getActivity().getApplicationContext().getPackageName());

    return _mediaStorageDir;
}

public void storeImage(Mat matImg) {

    Bitmap bitmapImg = Bitmap.createBitmap(matImg.cols(), matImg.rows(),Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(matImg, bitmapImg);
    String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmm").format(new Date());
    File mediaFile;
    String mImageName="IMG_"+ timeStamp +".jpg";
    mediaFile = new File(mediaStorageDir().getPath() + File.separator + mImageName);

    File pictureFile = mediaFile;

    try {
        FileOutputStream fos = new FileOutputStream(pictureFile);
        bitmapImg.compress(Bitmap.CompressFormat.PNG, 90, fos);
        fos.close();
    } catch (FileNotFoundException e) {
        Log.d("FragmentMain", "File not found: " + e.getMessage());
    } catch (IOException e) {
        Log.d("FragmentMain", "Error accessing file: " + e.getMessage());
    }
}

I imrove @sturkmen' s code.

fragment_main.xml

<FrameLayout 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"
tools:context="{your package name}.FragmentMain">

<!-- TODO: Update blank fragment layout -->

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btnCamera"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:text="Kamera" />

    <Button
        android:id="@+id/btnTest"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:text="Test" />
 
    <ImageView
        android:id="@+id/sampleImageView"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_centerHorizontal="true"/>
</LinearLayout>

</framelayout>

AndroidManifest.xml

Add this line for write permission.

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

FragmentMain.java

IMAGE FILE: Add Internal Storage / Android / Data / Your Package Folder / test.JPG

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View _view = inflater.inflate(R.layout.fragment_main, container, false);

    Button btnTest = (Button) _view.findViewById(R.id.btnTest);
    btnTest.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            Mat img = Imgcodecs.imread(mediaStorageDir().getPath() + "/" + "test.JPG");
            if (img.empty()) {
                Log.d("FragmentMain", "Empty Image");
            }


            Size dims = new Size (20,5);
            Mat gray = new Mat();
            Mat thresh = new Mat();

            //convert the image to black and white
            Imgproc.cvtColor(img, gray, Imgproc.COLOR_BGR2GRAY);
            storeImage(gray);

            //convert the image to black and white does (8 bit)
            Imgproc.threshold(gray, thresh, 0, 255, Imgproc.THRESH_BINARY_INV + Imgproc.THRESH_OTSU);

            storeImage(thresh);

            Mat temp = thresh.clone();
            //find the contours
            Mat hierarchy = new Mat();

            Mat corners = new Mat(4,1, CvType.CV_32FC2);
            List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
            Imgproc.findContours(temp, contours,hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
            hierarchy.release();

            for (int idx = 0; idx < contours.size(); idx++)
            {
                MatOfPoint contour = contours.get(idx);
                MatOfPoint2f contour_points = new MatOfPoint2f(contour.toArray());
                RotatedRect minRect = Imgproc.minAreaRect( contour_points );
                Point[] rect_points = new Point[4];
                minRect.points( rect_points );
                if(minRect.size.height > img.width() / 2)
                {
                    List<Point> srcPoints = new ArrayList<Point>(4);
                    srcPoints.add(rect_points[2]);
                    srcPoints.add(rect_points[3]);
                    srcPoints.add(rect_points[0]);
                    srcPoints.add(rect_points[1]);

                    corners = Converters.vector_Point_to_Mat(
                            srcPoints, CvType.CV_32F);
                }

            }
            Imgproc.erode(thresh, thresh, new Mat(), new Point(-1,-1), 10);

            storeImage(thresh);
            Imgproc.dilate(thresh, thresh, new Mat(), new Point(-1,-1), 5);

            storeImage(thresh);

            Mat results = new Mat(1000,250,CvType.CV_8UC3);
            Mat quad = new Mat(1000,250,CvType.CV_8UC1);

            List<Point> dstPoints = new ArrayList<Point>(4);
            dstPoints.add(new Point(0, 0));
            dstPoints.add(new Point(1000, 0));
            dstPoints.add(new Point(1000, 250));
            dstPoints.add(new Point(0, 250));
            Mat quad_pts = Converters.vector_Point_to_Mat(
                    dstPoints, CvType.CV_32F);

            Mat transmtx = Imgproc.getPerspectiveTransform(corners, quad_pts);
            Imgproc.warpPerspective( img, results, transmtx, new Size(1000,250));
            Imgproc.warpPerspective( thresh, quad, transmtx, new Size(1000,250));

            Imgproc.resize(quad, quad, new Size(20,5));

            Imgcodecs.imwrite("results.png",quad);

            //store image
            storeImage(quad);

            //show image
            showImage(quad);


            System.out.println( quad.dump() );

            for(int i = 0; i < quad.cols(); i++)
            {
                int size = (int) (quad.total() * quad.channels());
                byte[] tmp = new byte[size];

                String answer = "";
                double[] d = new double[0];
                d = quad.get(1, i);
                answer += d[0] == 0 ? "" : "A";
                d = quad.get(2, i);
                answer += d[0] == 0 ? "" : "B";
                d = quad.get(3, i);
                answer += d[0] == 0 ? "" : "C";
                d = quad.get(4, i);
                answer += d[0] == 0 ? "" : "D";

                if( answer.length()  > 1 ) answer = "X"; // Double mark
                int y = 0;
                if( answer.equals("A")) y = results.rows() / (int) dims.height;
                if( answer.equals("B")) y = results.rows() / (int) dims.height *2;
                if( answer.equals("C")) y = results.rows() / (int) dims.height *3;
                if( answer.equals("D")) y = results.rows() / (int) dims.height *4;
                if( answer == "" ) answer = "[-]";
                Imgproc.putText( results, answer, new Point( 50* i + 15, 30 + y), Core.FONT_HERSHEY_PLAIN, 2, new Scalar(0,0,255),2);

            }


            //store image
            storeImage(results);

            //show image
            showImage(results);

        }

    });
public void showImage (Mat img) {
    ImageView imgView = (ImageView) getActivity().findViewById(R.id.sampleImageView);
    //Mat mRgba = new Mat();

    //mRgba = Utils.loadResource(MainAct.this, R.drawable.your_image,Highgui.CV_LOAD_IMAGE_COLOR);
    Bitmap img2 = Bitmap.createBitmap(img.cols(), img.rows(),Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(img, img2);
    imgView.setImageBitmap(img2);
}

public File mediaStorageDir () {
    File _mediaStorageDir = new File(Environment.getExternalStorageDirectory()
            + "/Android/data/"
            + getActivity().getApplicationContext().getPackageName());

    return _mediaStorageDir;
}

public void storeImage(Mat matImg) {

    Bitmap bitmapImg = Bitmap.createBitmap(matImg.cols(), matImg.rows(),Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(matImg, bitmapImg);
    String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmm").format(new Date());
    File mediaFile;
    String mImageName="IMG_"+ timeStamp +".jpg";
    mediaFile = new File(mediaStorageDir().getPath() + File.separator + mImageName);

    File pictureFile = mediaFile;

    try {
        FileOutputStream fos = new FileOutputStream(pictureFile);
        bitmapImg.compress(Bitmap.CompressFormat.PNG, 90, fos);
        fos.close();
    } catch (FileNotFoundException e) {
        Log.d("FragmentMain", "File not found: " + e.getMessage());
    } catch (IOException e) {
        Log.d("FragmentMain", "Error accessing file: " + e.getMessage());
    }
}