Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pqpo committed Aug 3, 2017
0 parents commit 6d3657e
Show file tree
Hide file tree
Showing 70 changed files with 26,665 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
*.iml
.gradle
.idea
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
.externalNativeBuild
1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
30 changes: 30 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "me.pqpo.smartcropper"
minSdkVersion 14
targetSdkVersion 25
versionCode 1
versionName "1.0"
ndk {
abiFilters 'armeabi'
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile project(':smartcropperlib')
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'
}
25 changes: 25 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /home/qiulinmin/Android/Sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
23 changes: 23 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="me.pqpo.smartcropper">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".CropActivity">
</activity>
</application>

</manifest>
171 changes: 171 additions & 0 deletions app/src/main/java/me/pqpo/smartcropper/CropActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
package me.pqpo.smartcropper;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.graphics.Rect;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import me.pqpo.smartcropperlib.SmartCropper;
import me.pqpo.smartcropperlib.view.CropImageView;

public class CropActivity extends AppCompatActivity {

private static final String EXTRA_FROM_ALBUM = "extra_from_album";
private static final String EXTRA_CROPPED_FILE = "extra_cropped_file";
private static final int REQUEST_CODE_TAKE_PHOTO = 100;
private static final int REQUEST_CODE_SELECT_ALBUM = 200;

CropImageView ivCrop;
Button btnCancel;
Button btnOk;

boolean mFromAlbum;
File mCroppedFile;

File tempFile;

public static Intent getJumpIntent(Context context, boolean fromAlbum, File croppedFile) {
Intent intent = new Intent(context, CropActivity.class);
intent.putExtra(EXTRA_FROM_ALBUM, fromAlbum);
intent.putExtra(EXTRA_CROPPED_FILE, croppedFile);
return intent;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crop);
ivCrop = (CropImageView) findViewById(R.id.iv_crop);
btnCancel = (Button) findViewById(R.id.btn_cancel);
btnOk = (Button) findViewById(R.id.btn_ok);
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setResult(RESULT_CANCELED);
finish();
}
});
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);
} else {
setResult(RESULT_CANCELED);
}
finish();
}
});
mFromAlbum = getIntent().getBooleanExtra(EXTRA_FROM_ALBUM, true);
mCroppedFile = (File) getIntent().getSerializableExtra(EXTRA_CROPPED_FILE);
if (mCroppedFile == null) {
setResult(RESULT_CANCELED);
finish();
return;
}

tempFile = new File(getExternalFilesDir("img"), "temp.jpg");

selectPhoto();
}

private void selectPhoto() {
if (mFromAlbum) {
Intent selectIntent = new Intent(Intent.ACTION_PICK);
selectIntent.setType("image/*");
if (selectIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(selectIntent, REQUEST_CODE_SELECT_ALBUM);
}
} else {
Intent startCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));
if (startCameraIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(startCameraIntent, REQUEST_CODE_TAKE_PHOTO);
}
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK) {
setResult(RESULT_CANCELED);
finish();
return;
}
Bitmap selectedBitmap = null;
if (requestCode == REQUEST_CODE_TAKE_PHOTO && tempFile.exists()) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(tempFile.getPath(), options);
options.inJustDecodeBounds = false;
options.inSampleSize = calculateSampleSize(options);
selectedBitmap = BitmapFactory.decodeFile(tempFile.getPath(), options);
} else if (requestCode == REQUEST_CODE_SELECT_ALBUM && data != null && data.getData() != null) {
ContentResolver cr = getContentResolver();
Uri bmpUri = data.getData();
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(cr.openInputStream(bmpUri), new Rect(), options);
options.inJustDecodeBounds = false;
options.inSampleSize = calculateSampleSize(options);
selectedBitmap = BitmapFactory.decodeStream(cr.openInputStream(bmpUri), new Rect(), options);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
if (selectedBitmap != null) {
Point[] points = SmartCropper.scan(selectedBitmap);
ivCrop.setCropPoints(points);
ivCrop.setImageBitmap(selectedBitmap);
}
}


private void saveImage(Bitmap bitmap, File saveFile) {
try {
FileOutputStream fos = new FileOutputStream(saveFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}

private int calculateSampleSize(BitmapFactory.Options options) {
int outHeight = options.outHeight;
int outWidth = options.outWidth;
int sampleSize = 1;
int destHeight = 1000;
int destWidth = 1000;
if (outHeight > destHeight || outWidth > destHeight) {
if (outHeight > outWidth) {
sampleSize = outHeight / destHeight;
} else {
sampleSize = outWidth / destWidth;
}
}
if (sampleSize < 1) {
sampleSize = 1;
}
return sampleSize;
}
}
58 changes: 58 additions & 0 deletions app/src/main/java/me/pqpo/smartcropper/MainActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package me.pqpo.smartcropper;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.File;

public class MainActivity extends AppCompatActivity {

Button btnTake;
Button btnSelect;
ImageView ivShow;

File photoFile;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnTake = (Button) findViewById(R.id.btn_take);
btnSelect = (Button) findViewById(R.id.btn_select);
ivShow = (ImageView) findViewById(R.id.iv_show);

photoFile = new File(getExternalFilesDir("img"), "scan.jpg");

btnTake.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivityForResult(CropActivity.getJumpIntent(MainActivity.this, false, photoFile), 100);
}
});

btnSelect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivityForResult(CropActivity.getJumpIntent(MainActivity.this, true, photoFile), 100);
}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK) {
return;
}
if (requestCode == 100 && photoFile.exists()) {
Bitmap bitmap = BitmapFactory.decodeFile(photoFile.getPath());
ivShow.setImageBitmap(bitmap);
}
}
}
40 changes: 40 additions & 0 deletions app/src/main/res/layout/activity_crop.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/background_dark"
android:orientation="vertical">

<me.pqpo.smartcropperlib.view.CropImageView
android:id="@+id/iv_crop"
android:layout_width="match_parent"
android:layout_margin="20dp"
android:layout_height="0dp"
android:layout_weight="1"/>

<LinearLayout
android:id="@+id/ll_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:padding="10dp"
android:orientation="horizontal">

<Button
android:id="@+id/btn_cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="cancel"/>

<Button
android:id="@+id/btn_ok"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="ok"/>

</LinearLayout>

</LinearLayout>
Loading

0 comments on commit 6d3657e

Please sign in to comment.