├── .gitignore
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── vn
│ │ └── nano
│ │ └── photocropper
│ │ └── photocropper
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── vn
│ │ │ └── nano
│ │ │ └── photocropper
│ │ │ └── photocropper
│ │ │ ├── MainActivity.java
│ │ │ └── MyApplication.java
│ └── res
│ │ ├── drawable-xxxhdpi
│ │ └── img_test.jpg
│ │ ├── layout
│ │ └── activity_main.xml
│ │ ├── mipmap-hdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-mdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxxhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ └── values
│ │ ├── colors.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── vn
│ └── nano
│ └── photocropper
│ └── photocropper
│ └── ExampleUnitTest.java
├── bintrayv1.gradle
├── build.gradle
├── demo.gif
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── installv1.gradle
├── photo-cropper
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── vn
│ │ └── nano
│ │ └── photocropper
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── vn
│ │ │ └── nano
│ │ │ └── photocropper
│ │ │ ├── CropImageView.java
│ │ │ ├── CropListener.java
│ │ │ ├── CropOverlayView.java
│ │ │ └── CropPosition.java
│ └── res
│ │ ├── layout
│ │ └── crop_image_view.xml
│ │ └── values
│ │ └── strings.xml
│ └── test
│ └── java
│ └── vn
│ └── nano
│ └── photocropper
│ └── ExampleUnitTest.java
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea
5 | .DS_Store
6 | /build
7 | app/build
8 | /captures
9 | .externalNativeBuild
10 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # PhotoPolygonCropper
2 | ### Description
3 | Crop photo using polygon
4 | ### Usage
5 |
6 |
7 |
8 | - Add to `build.gradle`
9 |
10 | ```
11 | compile 'vn.tinyhands:photo-cropper:0.0.8'
12 | ```
13 | - Declare `CropImageView` in your layout
14 | ```
15 |
20 | ```
21 | - Set bitmap to crop
22 | ```
23 | cropImageView.setImageBitmap(bitmap);
24 | ```
25 | - Call `crop()` function to get cropped bitmap
26 | ```
27 | cropImageView.crop(Croplistener cropListener, boolean needStretch);
28 | ````
29 |
30 |
31 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 27
5 | buildToolsVersion '27.0.3'
6 | defaultConfig {
7 | applicationId "vn.nano.photocropper.photocropper"
8 | minSdkVersion 16
9 | targetSdkVersion 27
10 | versionCode 1
11 | versionName "1.0"
12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile fileTree(dir: 'libs', include: ['*.jar'])
24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
25 | exclude group: 'com.android.support', module: 'support-annotations'
26 | compile 'com.android.support:appcompat-v7:27.+'
27 | })
28 | implementation 'com.android.support.constraint:constraint-layout:1.0.2'
29 | testCompile 'junit:junit:4.12'
30 |
31 | implementation 'com.jakewharton.timber:timber:4.6.0'
32 |
33 | compile project(path: ':photo-cropper')
34 | // compile 'vn.tinyhands:photo-cropper:0.0.6'
35 | }
36 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Volumes/AlexData/Android/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/vn/nano/photocropper/photocropper/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package vn.nano.photocropper.photocropper;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("vn.nano.photocropper.photocropper", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/app/src/main/java/vn/nano/photocropper/photocropper/MainActivity.java:
--------------------------------------------------------------------------------
1 | package vn.nano.photocropper.photocropper;
2 |
3 | import android.graphics.Bitmap;
4 | import android.graphics.BitmapFactory;
5 | import android.graphics.Matrix;
6 | import android.os.Bundle;
7 | import android.support.v7.app.AppCompatActivity;
8 | import android.view.View;
9 | import android.widget.ImageView;
10 |
11 | import vn.nano.photocropper.CropImageView;
12 | import vn.nano.photocropper.CropListener;
13 |
14 | public class MainActivity extends AppCompatActivity {
15 |
16 | Bitmap mBitmap;
17 |
18 | @Override
19 | protected void onCreate(Bundle savedInstanceState) {
20 | super.onCreate(savedInstanceState);
21 | setContentView(R.layout.activity_main);
22 |
23 | mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.img_test);
24 | final CropImageView cropImageView = findViewById(R.id.crop_image_view);
25 | cropImageView.setImageBitmap(mBitmap);
26 |
27 | final CropListener listener = new CropListener() {
28 | @Override
29 | public void onFinish(Bitmap bitmap) {
30 | // cropImageView.setImageBitmap(bitmap);
31 | findViewById(R.id.crop_image_view).setVisibility(View.GONE);
32 |
33 | findViewById(R.id.img_cropped).setVisibility(View.VISIBLE);
34 | ((ImageView)findViewById(R.id.img_cropped)).setImageBitmap(bitmap);
35 | }
36 | };
37 |
38 | findViewById(R.id.btn_crop).setOnClickListener(new View.OnClickListener() {
39 | @Override
40 | public void onClick(View v) {
41 | cropImageView.crop(listener, true);
42 | }
43 | });
44 |
45 | findViewById(R.id.btn_rotate).setOnClickListener(new View.OnClickListener() {
46 | @Override
47 | public void onClick(View v) {
48 | rotateBitmap();
49 | }
50 | });
51 | }
52 |
53 | private void rotateBitmap() {
54 | Matrix matrix = new Matrix();
55 | matrix.postRotate( 90);
56 |
57 | mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, false);
58 | ((CropImageView) findViewById(R.id.crop_image_view)).setImageBitmap(mBitmap);
59 | }
60 |
61 | }
62 |
--------------------------------------------------------------------------------
/app/src/main/java/vn/nano/photocropper/photocropper/MyApplication.java:
--------------------------------------------------------------------------------
1 | package vn.nano.photocropper.photocropper;
2 |
3 | import android.app.Application;
4 |
5 | import timber.log.Timber;
6 |
7 | /**
8 | * Created by alex on 12/4/17.
9 | */
10 |
11 | public class MyApplication extends Application {
12 |
13 | @Override
14 | public void onCreate() {
15 | super.onCreate();
16 | Timber.plant(new Timber.DebugTree());
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxxhdpi/img_test.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/leanh215/Photo-Cropper/95c94e6a9cf48d2ce403f7da3fe720d300504262/app/src/main/res/drawable-xxxhdpi/img_test.jpg
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
9 |
10 |
17 |
18 |
22 |
23 |
29 |
30 |
31 |
32 |
39 |
40 |
45 |
46 |
51 |
52 |
53 |
54 |
55 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/leanh215/Photo-Cropper/95c94e6a9cf48d2ce403f7da3fe720d300504262/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/leanh215/Photo-Cropper/95c94e6a9cf48d2ce403f7da3fe720d300504262/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/leanh215/Photo-Cropper/95c94e6a9cf48d2ce403f7da3fe720d300504262/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/leanh215/Photo-Cropper/95c94e6a9cf48d2ce403f7da3fe720d300504262/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/leanh215/Photo-Cropper/95c94e6a9cf48d2ce403f7da3fe720d300504262/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/leanh215/Photo-Cropper/95c94e6a9cf48d2ce403f7da3fe720d300504262/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/leanh215/Photo-Cropper/95c94e6a9cf48d2ce403f7da3fe720d300504262/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/leanh215/Photo-Cropper/95c94e6a9cf48d2ce403f7da3fe720d300504262/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/leanh215/Photo-Cropper/95c94e6a9cf48d2ce403f7da3fe720d300504262/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/leanh215/Photo-Cropper/95c94e6a9cf48d2ce403f7da3fe720d300504262/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | PhotoCropper
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/vn/nano/photocropper/photocropper/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package vn.nano.photocropper.photocropper;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/bintrayv1.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.jfrog.bintray'
2 |
3 | version = libraryVersion
4 |
5 | if (project.hasProperty("android")) { // Android libraries
6 | task sourcesJar(type: Jar) {
7 | classifier = 'sources'
8 | from android.sourceSets.main.java.srcDirs
9 | }
10 |
11 | task javadoc(type: Javadoc) {
12 | source = android.sourceSets.main.java.srcDirs
13 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
14 | }
15 | } else { // Java libraries
16 | task sourcesJar(type: Jar, dependsOn: classes) {
17 | classifier = 'sources'
18 | from sourceSets.main.allSource
19 | }
20 | }
21 |
22 | task javadocJar(type: Jar, dependsOn: javadoc) {
23 | classifier = 'javadoc'
24 | from javadoc.destinationDir
25 | }
26 |
27 | artifacts {
28 | archives javadocJar
29 | archives sourcesJar
30 | }
31 |
32 | // Bintray
33 | Properties properties = new Properties()
34 | properties.load(project.rootProject.file('local.properties').newDataInputStream())
35 |
36 | bintray {
37 | user = properties.getProperty("bintray.user")
38 | key = properties.getProperty("bintray.apikey")
39 |
40 | configurations = ['archives']
41 | pkg {
42 | repo = bintrayRepo
43 | name = bintrayName
44 | userOrg = bintrayOrg
45 | desc = libraryDescription
46 | websiteUrl = siteUrl
47 | vcsUrl = gitUrl
48 | licenses = allLicenses
49 | publish = true
50 | publicDownloadNumbers = true
51 | version {
52 | desc = libraryDescription
53 | gpg {
54 | sign = true //Determines whether to GPG sign the files. The default is false
55 | passphrase = properties.getProperty("bintray.gpg.password")
56 | //Optional. The passphrase for GPG signing'
57 | }
58 | }
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | jcenter()
6 | google()
7 | }
8 | dependencies {
9 | classpath 'com.android.tools.build:gradle:3.1.4'
10 |
11 | classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.4'
12 | classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
13 |
14 | // NOTE: Do not place your application dependencies here; they belong
15 | // in the individual module build.gradle files
16 | }
17 | }
18 |
19 | allprojects {
20 | repositories {
21 | maven { url "https://maven.google.com" }
22 | jcenter()
23 | }
24 | }
25 |
26 | task clean(type: Delete) {
27 | delete rootProject.buildDir
28 | }
29 |
30 |
--------------------------------------------------------------------------------
/demo.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/leanh215/Photo-Cropper/95c94e6a9cf48d2ce403f7da3fe720d300504262/demo.gif
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/leanh215/Photo-Cropper/95c94e6a9cf48d2ce403f7da3fe720d300504262/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Tue Jul 10 14:51:08 ICT 2018
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/installv1.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.github.dcendents.android-maven'
2 |
3 | group = publishedGroupId // Maven Group ID for the artifact
4 |
5 | install {
6 | repositories.mavenInstaller {
7 | // This generates POM.xml with proper parameters
8 | pom {
9 | project {
10 | packaging 'aar'
11 | groupId publishedGroupId
12 | artifactId artifact
13 |
14 | // Add your description here
15 | name libraryName
16 | description libraryDescription
17 | url siteUrl
18 |
19 | // Set your license
20 | licenses {
21 | license {
22 | name licenseName
23 | url licenseUrl
24 | }
25 | }
26 | developers {
27 | developer {
28 | id developerId
29 | name developerName
30 | email developerEmail
31 | }
32 | }
33 | scm {
34 | connection gitUrl
35 | developerConnection gitUrl
36 | url siteUrl
37 |
38 | }
39 | }
40 | }
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/photo-cropper/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/photo-cropper/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 27
5 | buildToolsVersion '27.0.3'
6 |
7 | defaultConfig {
8 | minSdkVersion 16
9 | targetSdkVersion 27
10 | versionCode 1
11 | versionName "0.0.8"
12 |
13 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
14 |
15 | }
16 | buildTypes {
17 | release {
18 | minifyEnabled false
19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
20 | }
21 | }
22 | }
23 |
24 | dependencies {
25 | compile fileTree(dir: 'libs', include: ['*.jar'])
26 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
27 | exclude group: 'com.android.support', module: 'support-annotations'
28 | })
29 | compile 'com.android.support:appcompat-v7:27.+'
30 | testCompile 'junit:junit:4.12'
31 | }
32 |
33 | ext {
34 | // bintray info
35 | bintrayRepo = 'maven'
36 | bintrayName = 'photo-cropper'
37 | bintrayOrg = 'tinyhands'
38 |
39 | // package info
40 | publishedGroupId = 'vn.tinyhands'
41 | artifact = 'photo-cropper'
42 | libraryVersion = '0.0.8'
43 |
44 | // description
45 | libraryName = 'PhotoCropper'
46 | libraryDescription = 'Crop photo by polygon'
47 |
48 |
49 | // project info
50 | siteUrl = 'https://github.com/leanh215/PhotoPolygonCropper'
51 | gitUrl = 'https://github.com/leanh215/PhotoPolygonCropper.git'
52 |
53 | // developer info
54 | developerId = 'leanh215'
55 | developerName = 'Alex'
56 | developerEmail = 'leanh215@gmail.com'
57 |
58 | // license info
59 | licenseName = 'The Apache Software License, Version 2.0'
60 | licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
61 | allLicenses = ["Apache-2.0"]
62 | }
63 |
64 | //apply from: 'https://raw.githubusercontent.com/leanh215/JCenter/master/installv1.gradle'
65 | //apply from: 'https://raw.githubusercontent.com/leanh215/JCenter/master/bintrayv1.gradle'
66 |
67 | apply from: '../installv1.gradle'
68 | apply from: '../bintrayv1.gradle'
69 |
--------------------------------------------------------------------------------
/photo-cropper/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Volumes/AlexData/Android/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/photo-cropper/src/androidTest/java/vn/nano/photocropper/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package vn.nano.photocropper;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("vn.nano.photocropper.test", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/photo-cropper/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/photo-cropper/src/main/java/vn/nano/photocropper/CropImageView.java:
--------------------------------------------------------------------------------
1 | package vn.nano.photocropper;
2 |
3 | import android.content.Context;
4 | import android.graphics.Bitmap;
5 | import android.graphics.Canvas;
6 | import android.support.annotation.NonNull;
7 | import android.support.annotation.Nullable;
8 | import android.util.AttributeSet;
9 | import android.view.LayoutInflater;
10 | import android.view.View;
11 | import android.widget.FrameLayout;
12 | import android.widget.ImageView;
13 |
14 | /**
15 | * Created by alex on 12/4/17.
16 | */
17 |
18 | public class CropImageView extends FrameLayout {
19 |
20 | private ImageView mImageView;
21 | private CropOverlayView mCropOverlayView;
22 |
23 | public CropImageView(@NonNull Context context) {
24 | super(context);
25 | }
26 |
27 | public CropImageView(@NonNull Context context, @Nullable AttributeSet attrs) {
28 | super(context, attrs);
29 | LayoutInflater inflater = LayoutInflater.from(context);
30 | View v = inflater.inflate(R.layout.crop_image_view, this, true);
31 | mImageView = (ImageView) v.findViewById(R.id.img_crop);
32 | mCropOverlayView = (CropOverlayView) v.findViewById(R.id.overlay_crop);
33 | }
34 |
35 | @Override
36 | protected void onDraw(Canvas canvas) {
37 | super.onDraw(canvas);
38 | }
39 |
40 | public void setImageBitmap(Bitmap bitmap) {
41 | mImageView.setImageBitmap(bitmap);
42 | mCropOverlayView.setBitmap(bitmap);
43 | }
44 |
45 | public void crop(CropListener listener, boolean needStretch) {
46 | if (listener == null) return;
47 | mCropOverlayView.crop(listener, needStretch);
48 | }
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/photo-cropper/src/main/java/vn/nano/photocropper/CropListener.java:
--------------------------------------------------------------------------------
1 | package vn.nano.photocropper;
2 |
3 | import android.graphics.Bitmap;
4 |
5 | /**
6 | * Created by alex on 12/5/17.
7 | */
8 |
9 | public interface CropListener {
10 |
11 | void onFinish(Bitmap bitmap);
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/photo-cropper/src/main/java/vn/nano/photocropper/CropOverlayView.java:
--------------------------------------------------------------------------------
1 | package vn.nano.photocropper;
2 |
3 | import android.content.Context;
4 | import android.graphics.Bitmap;
5 | import android.graphics.Canvas;
6 | import android.graphics.Color;
7 | import android.graphics.Matrix;
8 | import android.graphics.Paint;
9 | import android.graphics.Path;
10 | import android.graphics.Point;
11 | import android.graphics.PorterDuff;
12 | import android.graphics.PorterDuffXfermode;
13 | import android.graphics.Rect;
14 | import android.graphics.RectF;
15 | import android.graphics.Region;
16 | import android.support.annotation.Nullable;
17 | import android.util.AttributeSet;
18 | import android.util.Log;
19 | import android.view.MotionEvent;
20 | import android.view.View;
21 |
22 | /**
23 | * Created by alex on 12/4/17.
24 | */
25 |
26 | public class CropOverlayView extends View {
27 |
28 | private int defaultMargin = 100;
29 | private int minDistance = 100;
30 | private int vertexSize = 30;
31 | private int gridSize = 3;
32 |
33 | private Bitmap bitmap;
34 | private Point topLeft, topRight, bottomLeft, bottomRight;
35 |
36 | private float touchDownX, touchDownY;
37 | private CropPosition cropPosition;
38 |
39 | private int currentWidth = 0;
40 | private int currentHeight = 0;
41 |
42 | private int minX, maxX, minY, maxY;
43 |
44 | public CropOverlayView(Context context) {
45 | super(context);
46 | }
47 |
48 | public CropOverlayView(Context context, @Nullable AttributeSet attrs) {
49 | super(context, attrs);
50 | }
51 |
52 | public void setBitmap(Bitmap bitmap) {
53 | this.bitmap = bitmap;
54 | resetPoints();
55 | invalidate();
56 | }
57 |
58 | @Override
59 | protected void onDraw(Canvas canvas) {
60 | super.onDraw(canvas);
61 |
62 | if (getWidth() != currentWidth || getHeight() != currentHeight) {
63 | currentWidth = getWidth();
64 | currentHeight = getHeight();
65 | resetPoints();
66 | }
67 |
68 | if (bitmap == null) return;
69 |
70 | drawBackground(canvas);
71 | drawVertex(canvas);
72 | drawEdge(canvas);
73 | drawGrid(canvas);
74 | }
75 |
76 | private void resetPoints() {
77 | if (bitmap == null) return;
78 |
79 | // 1. calculate bitmap size in new canvas
80 | float scaleX = bitmap.getWidth() * 1.0f / getWidth();
81 | float scaleY = bitmap.getHeight() * 1.0f / getHeight();
82 | float maxScale = Math.max(scaleX, scaleY);
83 |
84 | // 2. determine minX , maxX if maxScale = scaleY | minY, maxY if maxScale = scaleX
85 | int minX = 0;
86 | int maxX = getWidth();
87 | int minY = 0;
88 | int maxY = getHeight();
89 |
90 | if (maxScale == scaleY) { // image very tall
91 | int bitmapInCanvasWidth = (int) (bitmap.getWidth() / maxScale);
92 | minX = (getWidth() - bitmapInCanvasWidth) / 2;
93 | maxX = getWidth() - minX;
94 | } else { // image very wide
95 | int bitmapInCanvasHeight = (int) (bitmap.getHeight() / maxScale);
96 | minY = (getHeight() - bitmapInCanvasHeight)/2;
97 | maxY = getHeight() - minY;
98 | }
99 |
100 | this.minX = minX;
101 | this.minY = minY;
102 | this.maxX = maxX;
103 | this.maxY = maxY;
104 |
105 | if (maxX - minX < defaultMargin || maxY - minY < defaultMargin)
106 | defaultMargin = 0; // remove min
107 | else
108 | defaultMargin = 100;
109 |
110 | Log.e("stk", "maxX - minX=" + (maxX - minX));
111 | Log.e("stk", "maxY - minY=" + (maxY - minY));
112 |
113 | topLeft = new Point(minX + defaultMargin, minY + defaultMargin);
114 | topRight = new Point(maxX - defaultMargin, minY + defaultMargin);
115 | bottomLeft = new Point(minX + defaultMargin, maxY - defaultMargin);
116 | bottomRight = new Point(maxX - defaultMargin, maxY - defaultMargin);
117 | }
118 |
119 | @Override
120 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
121 | super.onMeasure(widthMeasureSpec, heightMeasureSpec);
122 | }
123 |
124 | private void drawBackground(Canvas canvas) {
125 | Paint paint = new Paint();
126 | paint.setColor(Color.parseColor("#66000000"));
127 | paint.setStyle(Paint.Style.FILL);
128 |
129 | Path path = new Path();
130 | path.moveTo(topLeft.x, topLeft.y);
131 | path.lineTo(topRight.x, topRight.y);
132 | path.lineTo(bottomRight.x, bottomRight.y);
133 | path.lineTo(bottomLeft.x, bottomLeft.y);
134 | path.close();
135 |
136 | canvas.save();
137 | canvas.clipPath(path, Region.Op.DIFFERENCE);
138 | canvas.drawColor(Color.parseColor("#66000000"));
139 | canvas.restore();
140 | }
141 |
142 | private void drawVertex(Canvas canvas) {
143 | Paint paint = new Paint();
144 | paint.setColor(Color.WHITE);
145 | paint.setStyle(Paint.Style.FILL);
146 |
147 | canvas.drawCircle(topLeft.x, topLeft.y, vertexSize, paint);
148 | canvas.drawCircle(topRight.x, topRight.y, vertexSize, paint);
149 | canvas.drawCircle(bottomLeft.x, bottomLeft.y, vertexSize, paint);
150 | canvas.drawCircle(bottomRight.x, bottomRight.y, vertexSize, paint);
151 |
152 | Log.e("stk",
153 | "vertextPoints=" +
154 | topLeft.toString() + " " + topRight.toString() + " " + bottomRight.toString() + " " + bottomLeft.toString());
155 |
156 | }
157 | private void drawEdge(Canvas canvas) {
158 | Paint paint = new Paint();
159 | paint.setColor(Color.WHITE);
160 | paint.setStrokeWidth(3);
161 | paint.setAntiAlias(true);
162 |
163 | canvas.drawLine(topLeft.x, topLeft.y, topRight.x, topRight.y, paint);
164 | canvas.drawLine(topLeft.x, topLeft.y, bottomLeft.x, bottomLeft.y, paint);
165 | canvas.drawLine(bottomRight.x, bottomRight.y, topRight.x, topRight.y, paint);
166 | canvas.drawLine(bottomRight.x, bottomRight.y, bottomLeft.x, bottomLeft.y, paint);
167 | }
168 |
169 | private void drawGrid(Canvas canvas) {
170 | Paint paint = new Paint();
171 | paint.setColor(Color.WHITE);
172 | paint.setStrokeWidth(2);
173 | paint.setAntiAlias(true);
174 |
175 | for (int i = 1; i <= gridSize; i++) {
176 | int topDistanceX = Math.abs(topLeft.x - topRight.x) / (gridSize + 1) * i;
177 | int topDistanceY = Math.abs((topLeft.y - topRight.y) / (gridSize + 1) * i);
178 |
179 | Point top = new Point(
180 | topLeft.x < topRight.x ? topLeft.x + topDistanceX : topLeft.x - topDistanceX,
181 | topLeft.y < topRight.y ? topLeft.y + topDistanceY : topLeft.y - topDistanceY);
182 |
183 | int bottomDistanceX = Math.abs((bottomLeft.x - bottomRight.x) / (gridSize + 1) * i);
184 | int bottomDistanceY = Math.abs((bottomLeft.y - bottomRight.y) / (gridSize + 1) * i);
185 | Point bottom = new Point(
186 | bottomLeft.x < bottomRight.x ? bottomLeft.x + bottomDistanceX : bottomLeft.x - bottomDistanceX,
187 | bottomLeft.y < bottomRight.y ? bottomLeft.y + bottomDistanceY : bottomLeft.y - bottomDistanceY);
188 |
189 | canvas.drawLine(top.x, top.y, bottom.x, bottom.y, paint);
190 |
191 | int leftDistanceX = Math.abs((topLeft.x - bottomLeft.x) / (gridSize + 1) * i);
192 | int leftDistanceY = Math.abs((topLeft.y - bottomLeft.y) / (gridSize + 1) * i);
193 |
194 | Point left = new Point(
195 | topLeft.x < bottomLeft.x ? topLeft.x + leftDistanceX : topLeft.x - leftDistanceX,
196 | topLeft.y < bottomLeft.y ? topLeft.y + leftDistanceY : topLeft.y - leftDistanceY);
197 |
198 | int rightDistanceX = Math.abs((topRight.x - bottomRight.x) / (gridSize + 1) * i);
199 | int rightDistanceY = Math.abs((topRight.y - bottomRight.y) / (gridSize + 1) * i);
200 |
201 | Point right = new Point(
202 | topRight.x < bottomRight.x ? topRight.x + rightDistanceX : topRight.x - rightDistanceX,
203 | topRight.y < bottomRight.y ? topRight.y + rightDistanceY : topRight.y - rightDistanceY);
204 |
205 | canvas.drawLine(left.x, left.y, right.x, right.y, paint);
206 | }
207 |
208 | }
209 |
210 | @Override
211 | public boolean onTouchEvent(MotionEvent event) {
212 | switch (event.getAction()) {
213 | case MotionEvent.ACTION_UP:
214 | getParent().requestDisallowInterceptTouchEvent(false);
215 | break;
216 | case MotionEvent.ACTION_DOWN:
217 | getParent().requestDisallowInterceptTouchEvent(false);
218 | onActionDown(event);
219 | return true;
220 | case MotionEvent.ACTION_MOVE:
221 | getParent().requestDisallowInterceptTouchEvent(true);
222 | onActionMove(event);
223 | return true;
224 | }
225 | return false;
226 | }
227 |
228 | private void onActionDown(MotionEvent event) {
229 | touchDownX = event.getX();
230 | touchDownY = event.getY();
231 | Point touchPoint = new Point((int) event.getX(), (int) event.getY());
232 | int minDistance = distance(touchPoint, topLeft);
233 | cropPosition = CropPosition.TOP_LEFT;
234 | if (minDistance > distance(touchPoint, topRight)) {
235 | minDistance = distance(touchPoint, topRight);
236 | cropPosition = CropPosition.TOP_RIGHT;
237 | }
238 | if (minDistance > distance(touchPoint, bottomLeft)) {
239 | minDistance = distance(touchPoint, bottomLeft);
240 | cropPosition = CropPosition.BOTTOM_LEFT;
241 | }
242 | if (minDistance > distance(touchPoint, bottomRight)) {
243 | minDistance = distance(touchPoint, bottomRight);
244 | cropPosition = CropPosition.BOTTOM_RIGHT;
245 | }
246 | }
247 |
248 | private int distance(Point src, Point dst) {
249 | return (int) Math.sqrt(Math.pow(src.x - dst.x, 2) + Math.pow(src.y - dst.y, 2));
250 | }
251 |
252 | private void onActionMove(MotionEvent event) {
253 | int deltaX = (int) (event.getX() - touchDownX);
254 | int deltaY = (int) (event.getY() - touchDownY);
255 |
256 | switch (cropPosition) {
257 | case TOP_LEFT:
258 | adjustTopLeft(deltaX, deltaY);
259 | invalidate();
260 | break;
261 | case TOP_RIGHT:
262 | adjustTopRight(deltaX, deltaY);
263 | invalidate();
264 | break;
265 | case BOTTOM_LEFT:
266 | adjustBottomLeft(deltaX, deltaY);
267 | invalidate();
268 | break;
269 | case BOTTOM_RIGHT:
270 | adjustBottomRight(deltaX, deltaY);
271 | invalidate();
272 | break;
273 | }
274 | touchDownX = event.getX();
275 | touchDownY = event.getY();
276 | }
277 |
278 | private void adjustTopLeft(int deltaX, int deltaY) {
279 | int newX = topLeft.x + deltaX;
280 | if (newX < minX) newX = minX;
281 | if (newX > maxX) newX = maxX;
282 |
283 | int newY = topLeft.y + deltaY;
284 | if (newY < minY) newY = minY;
285 | if (newY > maxY) newY = maxY;
286 |
287 | topLeft.set(newX, newY);
288 | }
289 |
290 | private void adjustTopRight(int deltaX, int deltaY) {
291 | int newX = topRight.x + deltaX;
292 | if (newX > maxX) newX = maxX;
293 | if (newX < minX) newX = minX;
294 |
295 | int newY = topRight.y + deltaY;
296 | if (newY < minY) newY = minY;
297 | if (newY > maxY) newY = maxY;
298 |
299 | topRight.set(newX, newY);
300 | }
301 |
302 | private void adjustBottomLeft(int deltaX, int deltaY) {
303 | int newX = bottomLeft.x + deltaX;
304 | if (newX < minX) newX = minX;
305 | if (newX > maxX) newX = maxX;
306 |
307 | int newY = bottomLeft.y + deltaY;
308 | if (newY > maxY) newY = maxY;
309 | if (newY < minY) newY = minY;
310 |
311 | bottomLeft.set(newX, newY);
312 | }
313 |
314 | private void adjustBottomRight(int deltaX, int deltaY) {
315 | int newX = bottomRight.x + deltaX;
316 | if (newX > maxX) newX = maxX;
317 | if (newX < minX) newX = minX;
318 |
319 | int newY = bottomRight.y + deltaY;
320 | if (newY > maxY) newY = maxY;
321 | if (newY < minY) newY = minY;
322 |
323 | bottomRight.set(newX, newY);
324 | }
325 |
326 | public void crop(CropListener cropListener, boolean needStretch) {
327 | if (topLeft == null) return;
328 |
329 | // calculate bitmap size in new canvas
330 | float scaleX = bitmap.getWidth() * 1.0f / getWidth();
331 | float scaleY = bitmap.getHeight() * 1.0f / getHeight();
332 | float maxScale = Math.max(scaleX, scaleY);
333 |
334 | // re-calculate coordinate in original bitmap
335 | Log.e("stk", "maxScale=" + maxScale);
336 |
337 | Point bitmapTopLeft = new Point((int) ((topLeft.x - minX) * maxScale), (int) ((topLeft.y - minY) * maxScale));
338 | Point bitmapTopRight = new Point((int) ((topRight.x - minX) * maxScale), (int) ((topRight.y - minY) * maxScale));
339 | Point bitmapBottomLeft = new Point((int) ((bottomLeft.x - minX) * maxScale), (int) ((bottomLeft.y - minY) * maxScale));
340 | Point bitmapBottomRight = new Point((int) ((bottomRight.x - minX) * maxScale), (int) ((bottomRight.y - minY) * maxScale));
341 |
342 | Log.e("stk", "bitmapPoints="
343 | + bitmapTopLeft.toString() + " "
344 | + bitmapTopRight.toString() + " "
345 | + bitmapBottomRight.toString() + " "
346 | + bitmapBottomLeft.toString() + " ");
347 |
348 | Bitmap output = Bitmap.createBitmap(bitmap.getWidth()+1, bitmap.getHeight()+1, Bitmap.Config.ARGB_8888);
349 | Canvas canvas = new Canvas(output);
350 |
351 | Paint paint = new Paint();
352 | // 1. draw path
353 | Path path = new Path();
354 | path.moveTo(bitmapTopLeft.x, bitmapTopLeft.y);
355 | path.lineTo(bitmapTopRight.x, bitmapTopRight.y);
356 | path.lineTo(bitmapBottomRight.x, bitmapBottomRight.y);
357 | path.lineTo(bitmapBottomLeft.x, bitmapBottomLeft.y);
358 | path.close();
359 | canvas.drawPath(path, paint);
360 |
361 | // 2. draw original bitmap
362 | paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
363 | canvas.drawBitmap(bitmap, 0, 0, paint);
364 |
365 | // 3. cut
366 | Rect cropRect = new Rect(
367 | Math.min(bitmapTopLeft.x, bitmapBottomLeft.x),
368 | Math.min(bitmapTopLeft.y, bitmapTopRight.y),
369 | Math.max(bitmapBottomRight.x, bitmapTopRight.x),
370 | Math.max(bitmapBottomRight.y, bitmapBottomLeft.y));
371 |
372 | Bitmap cut = Bitmap.createBitmap(
373 | output,
374 | cropRect.left,
375 | cropRect.top,
376 | cropRect.width(),
377 | cropRect.height()
378 | );
379 |
380 | if (!needStretch) {
381 | cropListener.onFinish(cut);
382 | } else {
383 | // 4. re-calculate coordinate in cropRect
384 | Point cutTopLeft = new Point();
385 | Point cutTopRight = new Point();
386 | Point cutBottomLeft = new Point();
387 | Point cutBottomRight = new Point();
388 |
389 | cutTopLeft.x = bitmapTopLeft.x > bitmapBottomLeft.x ? bitmapTopLeft.x - bitmapBottomLeft.x : 0;
390 | cutTopLeft.y = bitmapTopLeft.y > bitmapTopRight.y ? bitmapTopLeft.y - bitmapTopRight.y : 0;
391 |
392 | cutTopRight.x = bitmapTopRight.x > bitmapBottomRight.x ? cropRect.width() : cropRect.width() - Math.abs(bitmapBottomRight.x - bitmapTopRight.x);
393 | cutTopRight.y = bitmapTopLeft.y > bitmapTopRight.y ? 0 : Math.abs(bitmapTopLeft.y - bitmapTopRight.y);
394 |
395 | cutBottomLeft.x = bitmapTopLeft.x > bitmapBottomLeft.x ? 0 : Math.abs(bitmapTopLeft.x - bitmapBottomLeft.x);
396 | cutBottomLeft.y = bitmapBottomLeft.y > bitmapBottomRight.y ? cropRect.height() : cropRect.height() - Math.abs(bitmapBottomRight.y - bitmapBottomLeft.y);
397 |
398 | cutBottomRight.x = bitmapTopRight.x > bitmapBottomRight.x ? cropRect.width() - Math.abs(bitmapBottomRight.x - bitmapTopRight.x) : cropRect.width();
399 | cutBottomRight.y = bitmapBottomLeft.y > bitmapBottomRight.y ? cropRect.height() - Math.abs(bitmapBottomRight.y - bitmapBottomLeft.y) : cropRect.height();
400 |
401 | Log.e("stk", cut.getWidth() + "x" + cut.getHeight());
402 |
403 | Log.e("stk", "cutPoints="
404 | + cutTopLeft.toString() + " "
405 | + cutTopRight.toString() + " "
406 | + cutBottomRight.toString() + " "
407 | + cutBottomLeft.toString() + " ");
408 |
409 | float width = cut.getWidth();
410 | float height = cut.getHeight();
411 |
412 | float[] src = new float[]{cutTopLeft.x, cutTopLeft.y, cutTopRight.x, cutTopRight.y, cutBottomRight.x, cutBottomRight.y, cutBottomLeft.x, cutBottomLeft.y};
413 | float[] dst = new float[]{0, 0, width, 0, width, height, 0, height};
414 |
415 | Matrix matrix = new Matrix();
416 | matrix.setPolyToPoly(src, 0, dst, 0, 4);
417 | Bitmap stretch = Bitmap.createBitmap(cut.getWidth(), cut.getHeight(), Bitmap.Config.ARGB_8888);
418 |
419 | Canvas stretchCanvas = new Canvas(stretch);
420 | // stretchCanvas.drawBitmap(cut, matrix, null);
421 | stretchCanvas.concat(matrix);
422 | stretchCanvas.drawBitmapMesh(cut, WIDTH_BLOCK, HEIGHT_BLOCK, generateVertices(cut.getWidth(), cut.getHeight()), 0, null, 0, null);
423 |
424 | cropListener.onFinish(stretch);
425 | }
426 | }
427 |
428 | private int WIDTH_BLOCK = 40;
429 | private int HEIGHT_BLOCK = 40;
430 |
431 | private float[] generateVertices(int widthBitmap, int heightBitmap) {
432 |
433 | float[] vertices=new float[(WIDTH_BLOCK+1)*(HEIGHT_BLOCK+1)*2];
434 |
435 | float widthBlock = (float)widthBitmap/WIDTH_BLOCK;
436 | float heightBlock = (float)heightBitmap/HEIGHT_BLOCK;
437 |
438 | for(int i=0;i<=HEIGHT_BLOCK;i++)
439 | for(int j=0;j<=WIDTH_BLOCK;j++) {
440 | vertices[i * ((HEIGHT_BLOCK+1)*2) + (j*2)] = j * widthBlock;
441 | vertices[i * ((HEIGHT_BLOCK+1)*2) + (j*2)+1] = i * heightBlock;
442 | }
443 | return vertices;
444 | }
445 |
446 |
447 | }
448 |
--------------------------------------------------------------------------------
/photo-cropper/src/main/java/vn/nano/photocropper/CropPosition.java:
--------------------------------------------------------------------------------
1 | package vn.nano.photocropper;
2 |
3 | /**
4 | * Created by alex on 12/4/17.
5 | */
6 |
7 | public enum CropPosition {
8 | TOP_LEFT,
9 | TOP_RIGHT,
10 | BOTTOM_LEFT,
11 | BOTTOM_RIGHT
12 | }
13 |
--------------------------------------------------------------------------------
/photo-cropper/src/main/res/layout/crop_image_view.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
6 |
7 |
10 |
11 |
18 |
19 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/photo-cropper/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | PhotoCropper
3 |
4 |
--------------------------------------------------------------------------------
/photo-cropper/src/test/java/vn/nano/photocropper/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package vn.nano.photocropper;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':photo-cropper'
2 |
--------------------------------------------------------------------------------