Skip to content

Commit

Permalink
增加Crop功能
Browse files Browse the repository at this point in the history
  • Loading branch information
HuXiaobai committed Nov 14, 2018
1 parent 9380457 commit a73e77c
Show file tree
Hide file tree
Showing 129 changed files with 62,978 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public void itemClick(@NonNull View view, int position) {
mediaOptions.isShowVideo = true;
mediaOptions.isCompress = true;
mediaOptions.maxChooseMedia = 1;
mediaOptions.themeColor = R.color.colorAccent;
mediaOptions.isCrop = true;
// mediaOptions.themeColor = R.color.colorAccent;
MediaSelector.with(MainActivity.this).setMediaOptions(mediaOptions).openMediaActivity();

}
Expand Down
1 change: 1 addition & 0 deletions media/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ dependencies {
implementation files('libs/systembartint-1.0.4.jar')
implementation 'org.greenrobot:eventbus:3.1.1'
implementation 'com.github.Hu12037102.ImageCompress:compress:2.0.9'
implementation project(':ucrop')
}
allprojects {
repositories {
Expand Down
4 changes: 4 additions & 0 deletions media/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

</activity>
<activity android:name=".activity.PreviewActivity"/>
<activity
android:name="com.yalantis.ucrop.UCropActivity"
android:screenOrientation="portrait"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>

<provider
android:name=".provider.MediaProvider"
Expand Down
26 changes: 21 additions & 5 deletions media/src/main/java/com/example/media/MediaSelector.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,19 @@ public void openMediaActivity() {
Activity activity = mSoftActivity.get();
Intent intent = new Intent(activity, MediaActivity.class);
intent.putExtra(Contast.KEY_OPEN_MEDIA, mMediaOptions);
activity.startActivityForResult(intent,Contast.CODE_REQUEST_MEDIA);
activity.startActivityForResult(intent, Contast.CODE_REQUEST_MEDIA);
} else if (mSoftFragment != null && mSoftFragment.get() != null) {
Fragment fragment = mSoftFragment.get();
Intent intent = new Intent(fragment.getContext(), MediaActivity.class);
intent.putExtra(Contast.KEY_OPEN_MEDIA, mMediaOptions);
fragment.startActivityForResult(intent,Contast.CODE_REQUEST_MEDIA);
fragment.startActivityForResult(intent, Contast.CODE_REQUEST_MEDIA);
}
}

public static List<MediaSelectorFile> resultMediaFile( Intent data){
public static List<MediaSelectorFile> resultMediaFile(Intent data) {
if (data == null)
return null;
return data.getParcelableArrayListExtra(Contast.KEY_REQUEST_MEDIA_DATA);
return data.getParcelableArrayListExtra(Contast.KEY_REQUEST_MEDIA_DATA);
}


Expand All @@ -71,7 +71,13 @@ public MediaOptions() {
public boolean isCompress;
public boolean isShowCamera;
public boolean isShowVideo;
public @ColorRes int themeColor = R.color.colorTheme;
public @ColorRes
int themeColor = R.color.colorTheme;
public boolean isCrop;
public int scaleX = 1;
public int scaleY = 1;
public int cropWidth = 720;
public int cropHeight = 720;


protected MediaOptions(Parcel in) {
Expand All @@ -80,6 +86,11 @@ protected MediaOptions(Parcel in) {
isShowCamera = in.readByte() != 0;
isShowVideo = in.readByte() != 0;
themeColor = in.readInt();
isCrop = in.readByte() != 0;
scaleX = in.readInt();
scaleY = in.readInt();
cropWidth = in.readInt();
cropHeight = in.readInt();
}

public static final Creator<MediaOptions> CREATOR = new Creator<MediaOptions>() {
Expand All @@ -106,6 +117,11 @@ public void writeToParcel(Parcel dest, int flags) {
dest.writeByte((byte) (isShowCamera ? 1 : 0));
dest.writeByte((byte) (isShowVideo ? 1 : 0));
dest.writeInt(themeColor);
dest.writeByte((byte) (isCrop ? 1 : 0));
dest.writeInt(scaleX);
dest.writeInt(scaleY);
dest.writeInt(cropWidth);
dest.writeInt(cropHeight);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.LinearInterpolator;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.item.weight.TitleView;
import com.example.media.MediaSelector;
Expand All @@ -25,9 +27,11 @@
import com.example.media.bean.MediaSelectorFile;
import com.example.media.resolver.ActivityManger;
import com.example.media.resolver.Contast;
import com.example.media.utils.FileUtils;
import com.example.media.utils.ScreenUtils;
import com.example.media.weight.PreviewViewPager;
import com.example.media.weight.Toasts;
import com.yalantis.ucrop.UCrop;

import org.greenrobot.eventbus.EventBus;

Expand Down Expand Up @@ -84,7 +88,7 @@ protected void initData() {
mMediaFileData = intent.getParcelableArrayListExtra(Contast.KEY_PREVIEW_DATA_MEDIA);
mPreviewPosition = intent.getIntExtra(Contast.KEY_PREVIEW_POSITION, 0);
mOptions = intent.getParcelableExtra(Contast.KEY_OPEN_MEDIA);
mTvTop.mViewRoot.setBackgroundColor(ContextCompat.getColor(this,mOptions.themeColor));
mTvTop.mViewRoot.setBackgroundColor(ContextCompat.getColor(this, mOptions.themeColor));
if (mMediaFileData == null || mMediaFileData.size() == 0) {
Toasts.with().showToast(this, "没有预览媒体库文件");
finish();
Expand Down Expand Up @@ -222,7 +226,7 @@ public void onSureClick(@NonNull View view) {
}

private void sureData() {
if (mOptions.isCompress && !mOptions.isShowVideo) {
if (mOptions.isCompress && !mOptions.isShowVideo && !mOptions.isCrop) {
compressImage(mCheckMediaData, new CompressImageTask.OnImagesResult() {
@Override
public void startCompress() {
Expand All @@ -245,8 +249,26 @@ public void resultFilesError() {
}
});
} else {
EventBus.getDefault().post(mCheckMediaData);
finish();
if (mOptions.isCrop && mOptions.maxChooseMedia == 1) {
if (!mCheckMediaData.get(0).isVideo) {
UCrop.Options options = new UCrop.Options();
options.setCompressionQuality(100);
options.setToolbarColor(ContextCompat.getColor(this, mOptions.themeColor));
options.setStatusBarColor(ContextCompat.getColor(this, mOptions.themeColor));
options.setLogoColor(ContextCompat.getColor(this, mOptions.themeColor));
options.setActiveWidgetColor(ContextCompat.getColor(this, mOptions.themeColor));
UCrop.of(Uri.fromFile(new File(mCheckMediaData.get(0).filePath)), Uri.fromFile(FileUtils.resultImageFile(this, "Crop")))
.withAspectRatio(mOptions.scaleX, mOptions.scaleY)
.withMaxResultSize(mOptions.cropWidth, mOptions.cropHeight)
.withOptions(options)
.start(this);
} else {
Toasts.with().showToast(this, R.string.video_not_crop);
}
} else {
EventBus.getDefault().post(mCheckMediaData);
finish();
}
}
}

Expand Down Expand Up @@ -291,6 +313,32 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
mPreviewAdapter.mCbPlay.setChecked(false);
mPreviewAdapter.notifyDataSetChanged();
}
case RESULT_OK:
if (requestCode == UCrop.REQUEST_CROP) {
if (data == null) {
return;
}
final Uri resultUri = UCrop.getOutput(data);
if (resultUri != null && resultUri.getPath() != null) {
mCheckMediaData.clear();
File file = new File(resultUri.getPath());
if (FileUtils.existsFile(file.getAbsolutePath())) {
mCheckMediaData.add(MediaSelectorFile.checkFileToThis(file));
EventBus.getDefault().post(mCheckMediaData);
finish();
} else {
Toasts.with().showToast(this, R.string.file_not_exit, Toast.LENGTH_SHORT);
}
}

}
break;
case UCrop.RESULT_ERROR:
if (requestCode == UCrop.REQUEST_CROP) {
Toasts.with().showToast(this, R.string.crop_image_fail);
}
break;
default:
break;
}
}
Expand Down
23 changes: 19 additions & 4 deletions media/src/main/java/com/example/media/utils/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
public class FileUtils {

public static final String FILE_DIRECTOR_NAME = "HuXiaobai/Camera";
public static final String FILE_DIRECTOR_NAME = "MediaSelector";

/**
* 获取父文件夹名字
Expand Down Expand Up @@ -61,18 +61,33 @@ public static boolean existsFile(@NonNull String filePath) {
return false;
}

public static File outFileDirectory(Context context) {
public static File outCameraFileDirectory(Context context) {
String storageState = Environment.getExternalStorageState();
File rootFile = storageState.equals(Environment.MEDIA_MOUNTED) ? Environment.getExternalStorageDirectory() : context.getCacheDir();
rootFile = new File(rootFile.getAbsolutePath(), FILE_DIRECTOR_NAME);
rootFile = new File(rootFile.getAbsolutePath(), FILE_DIRECTOR_NAME.concat("/Camera"));
if (!rootFile.exists() || !rootFile.isDirectory()) {
rootFile.mkdirs();
}
return rootFile;
}


public static File outFileDirectory(Context context, String folderName) {
String storageState = Environment.getExternalStorageState();
File rootFile = storageState.equals(Environment.MEDIA_MOUNTED) ? Environment.getExternalStorageDirectory() : context.getCacheDir();
rootFile = new File(rootFile.getAbsolutePath(), FILE_DIRECTOR_NAME.concat("/").concat(folderName));
if (!rootFile.exists() || !rootFile.isDirectory()) {
rootFile.mkdirs();
}
return rootFile;
}

public static File resultImageFile(Context context) {
return new File(outFileDirectory(context).getAbsolutePath(), "hxb" + System.currentTimeMillis() + ".jpg");
return new File(outCameraFileDirectory(context).getAbsolutePath(), "hxb" + System.currentTimeMillis() + ".jpg");
}

public static File resultImageFile(Context context, String folderName) {
return new File(outFileDirectory(context, folderName).getAbsolutePath(), "crop" + System.currentTimeMillis() + ".jpg");
}

public static Uri fileToUri(@NonNull Context context, @NonNull File file, @NonNull Intent intent) {
Expand Down
3 changes: 3 additions & 0 deletions media/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@
<string name="not_gain_media_photo">没有获取到照片</string>
<string name="compress_image_fail">压缩图片失败</string>
<string name="this_video_format_not_supported">该视频格式不支持</string>
<string name="crop_image_fail">裁剪图片失败</string>
<string name="video_not_crop">视频不支持裁剪</string>
<string name="file_not_exit">文件不存在</string>

</resources>
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include ':app', ':media'
include ':app', ':media',':ucrop'
40 changes: 40 additions & 0 deletions ucrop/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 27
buildToolsVersion '27.0.3'

defaultConfig {
minSdkVersion 14
targetSdkVersion 27
versionCode 23
versionName "2.2.2-native"

vectorDrawables.useSupportLibrary = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
lintOptions {
abortOnError false
}

resourcePrefix 'ucrop_'

sourceSets.main {
jni.srcDirs = []
}

}

dependencies {
implementation 'com.android.support:appcompat-v7:27.1.0'
implementation 'com.squareup.okhttp3:okhttp:3.8.1'
}
3 changes: 3 additions & 0 deletions ucrop/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
POM_NAME=uCrop
POM_ARTIFACT_ID=ucrop
POM_PACKAGING=aar
17 changes: 17 additions & 0 deletions ucrop/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/oleksii/Library/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 *;
#}
2 changes: 2 additions & 0 deletions ucrop/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.yalantis.ucrop"/>
Loading

0 comments on commit a73e77c

Please sign in to comment.