<commit_before>package com.egroat.openlens;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfInt;
import org.opencv.core.MatOfPoint;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.ExecutionException;

import static org.opencv.core.Core.magnitude;
import static org.opencv.imgproc.Imgproc.getRotationMatrix2D;
import static org.opencv.imgproc.Imgproc.resize;
import static org.opencv.imgproc.Imgproc.warpAffine;

public class Capture extends AppCompatActivity implements View.OnClickListener,
        CameraBridgeViewBase.CvCameraViewListener2 {

    private static final String     TAG                 = "OpenLens::Capture";
    private static final double     mScalingFactor      = 3.0;

    private CameraBridgeViewBase    mOpenCvCameraView   = null;
    private Mat                     mCaptureImage       = null;
    private MatOfPoint2f            mRect               = null;
    private processImage            mProcessImage       = new processImage();

    private static double angle(Point p1, Point p2, Point p0) {
        double dx1 = p1.x - p0.x;
        double dy1 = p1.y - p0.y;
        double dx2 = p2.x - p0.x;
        double dy2 = p2.y - p0.y;
        return (dx1 * dx2 + dy1 * dy2) / Math.sqrt((dx1 * dx1 + dy1 * dy1) * (dx2 * dx2 + dy2 * dy2) + 1e-10);
    }
    private ArrayList<Point> sortCorners(ArrayList<Point> corners, Point center)
    {
        ArrayList<Point> top = new ArrayList<>(), bot= new ArrayList<>();

        for (int i = 0; i < corners.size(); i++)
        {
            if (corners.get(i).y < center.y)
                top.add(corners.get(i));
            else
                bot.add(corners.get(i));
        }

        Point tl = top.get(0).x > top.get(1).x ? top.get(1) : top.get(0);
        Point tr = top.get(0).x > top.get(1).x ? top.get(0) : top.get(1);
        Point bl = bot.get(0).x > bot.get(1).x ? bot.get(1) : bot.get(0);
        Point br = bot.get(0).x > bot.get(1).x ? bot.get(0) : bot.get(1);

        corners.clear();
        corners.add(tl);
        corners.add(tr);
        corners.add(br);
        corners.add(bl);
        return corners;
    }

    private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status) {
            switch (status) {
                case LoaderCallbackInterface.SUCCESS:
                {
                    Log.i(TAG, "OpenCV loaded successfully");
                    mOpenCvCameraView.enableView();
                } break;
                default:
                {
                    super.onManagerConnected(status);
                } break;
            }
        }
    };

    @Override
    public void onClick(View view) {
        Log.d(TAG, "TOUCH");
        if (mCaptureImage != null) {
            mProcessImage.cancel(true);
            mProcessImage = new processImage();
            mProcessImage.execute(new processImageParams(mCaptureImage,true));
            try {
                mRect = mProcessImage.get();
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }

            if (mRect != null) {
                List<MatOfPoint> temp = new ArrayList<>();
                temp.add(new MatOfPoint(mRect.toArray()));
                Imgproc.drawContours(mCaptureImage, temp, 0, new Scalar(255, 0, 0));
                int i = 1;
                for(Point p: mRect.toArray()) {
                    Imgproc.circle(mCaptureImage, p, 8, new Scalar(255, 0, 0));
                    Imgproc.putText(mCaptureImage,"Point " + i++,p,Core.FONT_HERSHEY_PLAIN,2,new Scalar(255,255,255));
                }
                i=1;
                {
                    Imgproc.putText(mCaptureImage,"Point " + i++,new Point(mCaptureImage.size().width-200,100),Core.FONT_HERSHEY_PLAIN,2,new Scalar(255,255,255));
                    Imgproc.putText(mCaptureImage,"Point " + i++,new Point(200,200),Core.FONT_HERSHEY_PLAIN,2,new Scalar(255,255,255));
                    Imgproc.putText(mCaptureImage,"Point " + i++,new Point(100,mCaptureImage.size().height-200),Core.FONT_HERSHEY_PLAIN,2,new Scalar(255,255,255));
                    Imgproc.putText(mCaptureImage,"Point " + i++,new Point(mCaptureImage.size().width-200,mCaptureImage.size().height-200),Core.FONT_HERSHEY_PLAIN,2,new Scalar(255,255,255));
                }
            }
            //magnitude();
            Point[] corners = new Point[] {new Point(0.0,0.0),
                    new Point(mCaptureImage.size().width,0.0),
                    new Point(0.0,mCaptureImage.size().height),
                    new Point(mCaptureImage.size().width,mCaptureImage.size().height)};
            Point[] corners2 = mRect.toArray();


            //TODO: Figure out how to wrap image
            Mat dst = mCaptureImage.clone();
            Mat src_mat=new Mat(4,1,CvType.CV_32FC2);
            Mat dst_mat=new Mat(4,1,CvType.CV_32FC2);
            src_mat.put(0,0,corners2[0].x,corners2[0].y,
                    corners2[1].x,corners2[1].y,
                    corners2[2].x,corners2[2].y,
                    corners2[3].x,corners2[3].y);
            dst_mat.put(0, 0, corners[0].x, corners[0].y,
                    corners[1].x, corners[1].y,
                    corners[2].x, corners[2].y,
                    corners[3].x, corners[3].y);
            Mat perspectiveTransform = Imgproc.getPerspectiveTransform(src_mat,dst_mat);<commit_msg>Temp work for sorting corners<commit_after>package com.egroat.openlens;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfInt;
import org.opencv.core.MatOfPoint;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.ExecutionException;

import static org.opencv.core.Core.magnitude;
import static org.opencv.imgproc.Imgproc.getRotationMatrix2D;
import static org.opencv.imgproc.Imgproc.resize;
import static org.opencv.imgproc.Imgproc.warpAffine;

public class Capture extends AppCompatActivity implements View.OnClickListener,
        CameraBridgeViewBase.CvCameraViewListener2 {

    private static final String     TAG                 = "OpenLens::Capture";
    private static final double     mScalingFactor      = 3.0;

    private CameraBridgeViewBase    mOpenCvCameraView   = null;
    private Mat                     mCaptureImage       = null;
    private MatOfPoint2f            mRect               = null;
    private processImage            mProcessImage       = new processImage();

    private Point[] sortCorners()
    {
        Point[] captureImageCorners = new Point[] {new Point(0.0,0.0),
                new Point(mCaptureImage.size().width,0.0),
                new Point(0.0,mCaptureImage.size().height),
                new Point(mCaptureImage.size().width,mCaptureImage.size().height)};
        Point[] rectCorners = mRect.toArray();

        // Find Center
        final Point centerPoint = new Point(mCaptureImage.size().height/2, mCaptureImage.size().width/2);

        // http://stackoverflow.com/questions/6989100/sort-points-in-clockwise-order

        Arrays.sort(captureImageCorners, new Comparator<Point>() {
            @Override
            public int compare(Point lhs, Point rhs) {
                if(lhs.x >= 0 && rhs.x < 0)
                    return 1;
                else if(lhs.x == 0 && rhs.x == 0)
                    return (lhs.y > rhs.y);

                double det = (lhs.x - centerPoint.x) * (rhs.y - centerPoint.y)
                        - (rhs.x - centerPoint.x) * (lhs.y - centerPoint.y);
                if(det < 0)
                    return 1;
                else if(det > 0)
                    return -1;

                return 0;
            }
        });

        return corners;
    }

    private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status) {
            switch (status) {
                case LoaderCallbackInterface.SUCCESS:
                {
                    Log.i(TAG, "OpenCV loaded successfully");
                    mOpenCvCameraView.enableView();
                } break;
                default:
                {
                    super.onManagerConnected(status);
                } break;
            }
        }
    };

    @Override
    public void onClick(View view) {
        Log.d(TAG, "TOUCH");
        if (mCaptureImage != null) {
            mProcessImage.cancel(true);
            mProcessImage = new processImage();
            mProcessImage.execute(new processImageParams(mCaptureImage,true));
            try {
                mRect = mProcessImage.get();
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }

            if (mRect != null) {
                List<MatOfPoint> temp = new ArrayList<>();
                temp.add(new MatOfPoint(mRect.toArray()));
                Imgproc.drawContours(mCaptureImage, temp, 0, new Scalar(255, 0, 0));
                int i = 1;
                for(Point p: mRect.toArray()) {
                    Imgproc.circle(mCaptureImage, p, 8, new Scalar(255, 0, 0));
                    Imgproc.putText(mCaptureImage,"Point " + i++,p,Core.FONT_HERSHEY_PLAIN,2,new Scalar(255,255,255));
                }
                i=1;
                {
                    Imgproc.putText(mCaptureImage,"Point " + i++,new Point(mCaptureImage.size().width-200,100),Core.FONT_HERSHEY_PLAIN,2,new Scalar(255,255,255));
                    Imgproc.putText(mCaptureImage,"Point " + i++,new Point(200,200),Core.FONT_HERSHEY_PLAIN,2,new Scalar(255,255,255));
                    Imgproc.putText(mCaptureImage,"Point " + i++,new Point(100,mCaptureImage.size().height-200),Core.FONT_HERSHEY_PLAIN,2,new Scalar(255,255,255));
                    Imgproc.putText(mCaptureImage,"Point " + i++,new Point(mCaptureImage.size().width-200,mCaptureImage.size().height-200),Core.FONT_HERSHEY_PLAIN,2,new Scalar(255,255,255));
                }
            }
            //magnitude();
            Point[] corners2 = mRect.toArray();


            //TODO: Figure out how to wrap image
            Mat dst = mCaptureImage.clone();
            Mat src_mat=new Mat(4,1,CvType.CV_32FC2);
            Mat dst_mat=new Mat(4,1,CvType.CV_32FC2);
            src_mat.put(0,0,corners2[0].x,corners2[0].y,
                    corners2[1].x,corners2[1].y,
                    corners2[2].x,corners2[2].y,
                    corners2[3].x,corners2[3].y);
            dst_mat.put(0, 0, captureImageCorners[0].x, captureImageCorners[0].y,
                    captureImageCorners[1].x, captureImageCorners[1].y,
                    captureImageCorners[2].x, captureImageCorners[2].y,
                    captureImageCorners[3].x, captureImageCorners[3].y);
            Mat perspectiveTransform = Imgproc.getPerspectiveTransform(src_mat,dst_mat);
{'fraction_non_alphanumeric': 0.1190736973869504, 'fraction_numerical': 0.027382256297918947, 'mean_word_length': 2.1981486114585937, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2488868', 'n_tokens_mistral': 4178, 'n_tokens_neox': 3875, 'n_words': 689}