Skip to content

Commit

Permalink
Focus + Better Optimal Size + Some Minor Fixes
Browse files Browse the repository at this point in the history
ScanCanvasView using same params of SurfaceView to keep canvas at right position
  • Loading branch information
vinisauter committed Aug 15, 2018
1 parent 4901280 commit c1d8486
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 99 deletions.
4 changes: 4 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions liveedgedetection/build.gradle
Expand Up @@ -35,7 +35,7 @@ dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:26.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
implementation project(':openCVLibrary340')
}
Expand Up @@ -36,7 +36,6 @@
import com.adityaarora.liveedgedetection.view.PolygonView;
import com.adityaarora.liveedgedetection.view.ProgressDialogFragment;
import com.adityaarora.liveedgedetection.view.Quadrilateral;
import com.adityaarora.liveedgedetection.view.ScanCanvasView;
import com.adityaarora.liveedgedetection.view.ScanSurfaceView;

import org.opencv.android.Utils;
Expand All @@ -62,7 +61,6 @@ public class ScanActivity extends AppCompatActivity implements IScanner, View.On

private ViewGroup containerScan;
private FrameLayout cameraPreviewLayout;
private ScanCanvasView scanCanvasView;
private ScanSurfaceView mImageSurfaceView;
private boolean isPermissionNotGranted;
private static final String mOpenCvLibrary = "opencv_java3";
Expand Down Expand Up @@ -90,14 +88,13 @@ protected void onCreate(Bundle savedInstanceState) {
private void init() {
containerScan = findViewById(R.id.container_scan);
cameraPreviewLayout = findViewById(R.id.camera_preview);
scanCanvasView = findViewById(R.id.scan_canvas);
captureHintLayout = findViewById(R.id.capture_hint_layout);
captureHintText = findViewById(R.id.capture_hint_text);
polygonView = findViewById(R.id.polygon_view);
cropImageView = findViewById(R.id.crop_image_view);
cropAcceptBtn = findViewById(R.id.crop_accept_btn);
cropRejectBtn = findViewById(R.id.crop_reject_btn);
cropLayout = findViewById(R.id.crop_layout);
captureHintLayout = findViewById(R.id.capture_hint_layout);
captureHintText = findViewById(R.id.capture_hint_text);
polygonView = findViewById(R.id.polygon_view);
cropImageView = findViewById(R.id.crop_image_view);
cropAcceptBtn = findViewById(R.id.crop_accept_btn);
cropRejectBtn = findViewById(R.id.crop_reject_btn);
cropLayout = findViewById(R.id.crop_layout);

cropAcceptBtn.setOnClickListener(this);
cropRejectBtn.setOnClickListener(new View.OnClickListener() {
Expand All @@ -118,16 +115,15 @@ private void checkCameraPermissions() {
isPermissionNotGranted = true;
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.CAMERA)) {
Toast.makeText(this,"Enable camera permission from settings", Toast.LENGTH_SHORT).show();
Toast.makeText(this, "Enable camera permission from settings", Toast.LENGTH_SHORT).show();
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA},
MY_PERMISSIONS_REQUEST_CAMERA);
}
} else {
if (!isPermissionNotGranted) {
mImageSurfaceView = new ScanSurfaceView(ScanActivity.this,
scanCanvasView, this);
mImageSurfaceView = new ScanSurfaceView(ScanActivity.this, this);
cameraPreviewLayout.addView(mImageSurfaceView);
} else {
isPermissionNotGranted = false;
Expand Down Expand Up @@ -156,8 +152,7 @@ public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
mImageSurfaceView = new ScanSurfaceView(ScanActivity.this,
scanCanvasView, ScanActivity.this);
mImageSurfaceView = new ScanSurfaceView(ScanActivity.this, ScanActivity.this);
cameraPreviewLayout.addView(mImageSurfaceView);
}
});
Expand Down Expand Up @@ -196,31 +191,19 @@ public void displayHint(ScanHint scanHint) {
break;
case NO_MESSAGE:
captureHintLayout.setVisibility(GONE);
clearAndInvalidateCanvas();
break;
default:
break;
}
}

@Override
public void clearAndInvalidateCanvas() {
scanCanvasView.clear();
invalidateCanvas();
}

@Override
public void invalidateCanvas() {
scanCanvasView.invalidate();
}

@Override
public void onPictureClicked(final Bitmap bitmap) {
try {
copyBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);

int height = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getHeight();
int width = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getWidth();
int height = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getHeight();
int width = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getWidth();

copyBitmap = ScanUtils.resizeToScreenContentSize(copyBitmap, width, height);
Mat originalMat = new Mat(copyBitmap.getHeight(), copyBitmap.getWidth(), CvType.CV_8UC1);
Expand All @@ -232,7 +215,7 @@ public void onPictureClicked(final Bitmap bitmap) {
if (null != quad) {
double resultArea = Math.abs(Imgproc.contourArea(quad.contour));
double previewArea = originalMat.rows() * originalMat.cols();
if(resultArea > previewArea * 0.08) {
if (resultArea > previewArea * 0.08) {
points = new ArrayList<>();
points.add(new PointF((float) quad.points[0].x, (float) quad.points[0].y));
points.add(new PointF((float) quad.points[1].x, (float) quad.points[1].y));
Expand Down
Expand Up @@ -10,7 +10,5 @@

public interface IScanner {
void displayHint(ScanHint scanHint);
void clearAndInvalidateCanvas();
void invalidateCanvas();
void onPictureClicked(Bitmap bitmap);
}
Expand Up @@ -46,7 +46,7 @@
public class ScanUtils {
private static final String TAG = ScanUtils.class.getSimpleName();

public static boolean compareFloats(float left, float right) {
public static boolean compareFloats(double left, double right) {
double epsilon = 0.00000001;
return Math.abs(left - right) < epsilon;
}
Expand All @@ -55,6 +55,14 @@ public static Camera.Size determinePictureSize(Camera camera, Camera.Size previe
if (camera == null) return null;
Camera.Parameters cameraParams = camera.getParameters();
List<Camera.Size> pictureSizeList = cameraParams.getSupportedPictureSizes();
Collections.sort(pictureSizeList, new Comparator<Camera.Size>() {
@Override
public int compare(Camera.Size size1, Camera.Size size2) {
Double h1 = Math.sqrt(size1.width * size1.width + size1.height * size1.height);
Double h2 = Math.sqrt(size2.width * size2.width + size2.height * size2.height);
return h2.compareTo(h1);
}
});
Camera.Size retSize = null;

// if the preview size is not supported as a picture size
Expand All @@ -78,34 +86,26 @@ public static Camera.Size determinePictureSize(Camera camera, Camera.Size previe

public static Camera.Size getOptimalPreviewSize(Camera camera, int w, int h) {
if (camera == null) return null;
final double targetRatio = (double) h / w;
Camera.Parameters cameraParams = camera.getParameters();
List<Camera.Size> previewSizeList = cameraParams.getSupportedPreviewSizes();

final double ASPECT_TOLERANCE = 0.1;
double targetRatio = (double) h / w;

Camera.Size optimalSize = null;
double minDiff = Double.MAX_VALUE;

for (Camera.Size size : previewSizeList) {
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
if (Math.abs(size.height - h) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - h);
}
}

if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Camera.Size size : previewSizeList) {
if (Math.abs(size.height - h) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - h);
Collections.sort(previewSizeList, new Comparator<Camera.Size>() {
@Override
public int compare(Camera.Size size1, Camera.Size size2) {
double ratio1 = (double) size1.width / size1.height;
double ratio2 = (double) size2.width / size2.height;
Double ratioDiff1 = Math.abs(ratio1 - targetRatio);
Double ratioDiff2 = Math.abs(ratio2 - targetRatio);
if (ScanUtils.compareFloats(ratioDiff1, ratioDiff2)) {
Double h1 = Math.sqrt(size1.width * size1.width + size1.height * size1.height);
Double h2 = Math.sqrt(size2.width * size2.width + size2.height * size2.height);
return h2.compareTo(h1);
}
return ratioDiff1.compareTo(ratioDiff2);
}
}
return optimalSize;
});

return previewSizeList.get(0);
}

public static int getDisplayOrientation(Activity activity, int cameraId) {
Expand Down

0 comments on commit c1d8486

Please sign in to comment.