Skip to content

Commit

Permalink
add check that if can crop correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
pqpo committed Aug 7, 2017
1 parent 4b1d885 commit d16e749
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 43 deletions.
Binary file modified aar/smartcropperlib.aar
Binary file not shown.
3 changes: 1 addition & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ android {
defaultConfig {
applicationId "me.pqpo.smartcropper"
minSdkVersion 14
targetSdkVersion 25
targetSdkVersion 22
versionCode 1
versionName "1.0"
ndk {
Expand All @@ -26,5 +26,4 @@ dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:support-v4:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
}
17 changes: 11 additions & 6 deletions app/src/main/java/me/pqpo/smartcropper/CropActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.File;
import java.io.FileNotFoundException;
Expand Down Expand Up @@ -62,14 +63,18 @@ public void onClick(View v) {
btnOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
Bitmap crop = ivCrop.crop();
if (crop != null) {
saveImage(crop, mCroppedFile);
setResult(RESULT_OK);
if (ivCrop.canRightCrop()) {
Bitmap crop = ivCrop.crop();
if (crop != null) {
saveImage(crop, mCroppedFile);
setResult(RESULT_OK);
} else {
setResult(RESULT_CANCELED);
}
finish();
} else {
setResult(RESULT_CANCELED);
Toast.makeText(CropActivity.this, "cannot crop correctly", Toast.LENGTH_SHORT).show();
}
finish();
}
});
mFromAlbum = getIntent().getBooleanExtra(EXTRA_FROM_ALBUM, true);
Expand Down
Binary file modified art/SmartCropperSample.apk
Binary file not shown.
18 changes: 0 additions & 18 deletions smartcropperlib/src/main/cpp/smart_cropper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@
#include <Scanner.h>

static const char* const kClassDocScanner = "me/pqpo/smartcropperlib/SmartCropper";
//
//static struct {
// jclass jClassArrayList;
// jmethodID jMethodAdd;
//} gArrayListInfo;

static struct {
jclass jClassPoint;
Expand All @@ -21,9 +16,6 @@ static struct {
} gPointInfo;

static void initClassInfo(JNIEnv *env) {
// gArrayListInfo.jClassArrayList = reinterpret_cast<jclass>(env -> NewGlobalRef(env -> FindClass("java/util/ArrayList")));
// gArrayListInfo.jMethodAdd = env -> GetMethodID(gArrayListInfo.jClassArrayList, "add", "(Ljava/lang/Object;)Z");

gPointInfo.jClassPoint = reinterpret_cast<jclass>(env -> NewGlobalRef(env -> FindClass("android/graphics/Point")));
gPointInfo.jMethodInit = env -> GetMethodID(gPointInfo.jClassPoint, "<init>", "(II)V");
gPointInfo.jFieldIDX = env -> GetFieldID(gPointInfo.jClassPoint, "x", "I");
Expand All @@ -34,10 +26,6 @@ static jobject createJavaPoint(JNIEnv *env, Point point_) {
return env -> NewObject(gPointInfo.jClassPoint, gPointInfo.jMethodInit, point_.x, point_.y);
}

//static void addToArrayList(JNIEnv *env, jobject arrayList, jobject item) {
// env -> CallBooleanMethod(arrayList, gArrayListInfo.jMethodAdd, item);
//}

static void native_scan(JNIEnv *env, jclass type, jobject srcBitmap, jobjectArray outPoint_) {
if (env -> GetArrayLength(outPoint_) != 4) {
return;
Expand All @@ -53,12 +41,6 @@ static void native_scan(JNIEnv *env, jclass type, jobject srcBitmap, jobjectArra
env -> SetObjectArrayElement(outPoint_, i, createJavaPoint(env, scanPoints[i]));
}
}

// if(scanPoints.size() == 4) {
// for(Point p : scanPoints) {
// addToArrayList(env, outArrayList, createJavaPoint(env, p));
// }
// }
}

static vector<Point> pointsToNative(JNIEnv *env, jobjectArray points_) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,23 +108,23 @@ public Bitmap crop(Point[] points) {
return bmp == null ? null : SmartCropper.crop(bmp, points);
}

// public boolean canRightCrop() {
// Point lt = mCropPoints[0];
// Point rt = mCropPoints[1];
// Point rb = mCropPoints[2];
// Point lb = mCropPoints[3];
// return (pointSideLine(lt, rb, lb) * pointSideLine(lt, rb, rt) < 0) && (pointSideLine(lb, rt, lt) * pointSideLine(lb, rt, rb) < 0);
// }
//
// private int pointSideLine(Point lineP1, Point lineP2, Point point) {
// int x1 = lineP1.x;
// int y1 = lineP1.y;
// int x2 = lineP2.x;
// int y2 = lineP2.y;
// int x = point.x;
// int y = point.y;
// return ((y1 - y2)*x + (x2 - x1)*y + x1 * y2 - x2 * y1);
// }
public boolean canRightCrop() {
Point lt = mCropPoints[0];
Point rt = mCropPoints[1];
Point rb = mCropPoints[2];
Point lb = mCropPoints[3];
return (pointSideLine(lt, rb, lb) * pointSideLine(lt, rb, rt) < 0) && (pointSideLine(lb, rt, lt) * pointSideLine(lb, rt, rb) < 0);
}

private long pointSideLine(Point lineP1, Point lineP2, Point point) {
long x1 = lineP1.x;
long y1 = lineP1.y;
long x2 = lineP2.x;
long y2 = lineP2.y;
long x = point.x;
long y = point.y;
return (x - x1)*(y2 - y1) - (y - y1)*(x2 - x1);
}

public Bitmap getBitmap() {
Bitmap bmp = null;
Expand Down

0 comments on commit d16e749

Please sign in to comment.